Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,55 @@
|
|
| 1 |
-
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Initialize FastAPI app
|
| 6 |
-
app = FastAPI()
|
| 7 |
-
|
| 8 |
# Load API key from Hugging Face Secrets
|
| 9 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 10 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
# Load API key from Hugging Face Secrets
|
| 6 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 8 |
|
| 9 |
+
def respond(
|
| 10 |
+
message,
|
| 11 |
+
history: list[tuple[str, str]],
|
| 12 |
+
system_message,
|
| 13 |
+
max_tokens,
|
| 14 |
+
temperature,
|
| 15 |
+
top_p,
|
| 16 |
+
):
|
| 17 |
+
messages = [{"role": "system", "content": system_message}]
|
| 18 |
+
|
| 19 |
+
for val in history:
|
| 20 |
+
if val[0]: # User message
|
| 21 |
+
messages.append({"role": "user", "content": val[0]})
|
| 22 |
+
if val[1]: # Assistant response
|
| 23 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 24 |
+
|
| 25 |
+
messages.append({"role": "user", "content": message})
|
| 26 |
+
|
| 27 |
+
# Call Gemini API
|
| 28 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 29 |
+
response = model.generate_content(message, stream=True)
|
| 30 |
+
|
| 31 |
+
output = ""
|
| 32 |
+
for chunk in response:
|
| 33 |
+
if chunk.text:
|
| 34 |
+
output += chunk.text
|
| 35 |
+
yield output # Stream response in real-time
|
| 36 |
+
|
| 37 |
+
# Gradio Interface
|
| 38 |
+
demo = gr.ChatInterface(
|
| 39 |
+
respond,
|
| 40 |
+
additional_inputs=[
|
| 41 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 42 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 43 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 44 |
+
gr.Slider(
|
| 45 |
+
minimum=0.1,
|
| 46 |
+
maximum=1.0,
|
| 47 |
+
value=0.95,
|
| 48 |
+
step=0.05,
|
| 49 |
+
label="Top-p (nucleus sampling)",
|
| 50 |
+
),
|
| 51 |
+
],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.launch()
|