Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# chat_app.py
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
import google.generativeai as genai
|
5 |
+
|
6 |
+
# 1️⃣ Configure the SDK – read the key from an env-var
|
7 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
8 |
+
|
9 |
+
# 2️⃣ Create a model and a stateful chat session
|
10 |
+
model = genai.GenerativeModel("gemini-pro") # change model name if you like
|
11 |
+
chat = model.start_chat() # history handled for you
|
12 |
+
|
13 |
+
# 3️⃣ Callback that Gradio calls each turn
|
14 |
+
def respond(message, history):
|
15 |
+
reply = chat.send_message(message) # send the new user turn
|
16 |
+
return reply.text # only the assistant reply
|
17 |
+
|
18 |
+
# 4️⃣ Build the UI
|
19 |
+
iface = gr.ChatInterface(
|
20 |
+
fn=respond,
|
21 |
+
title="Gemini Chatbot",
|
22 |
+
chatbot=gr.Chatbot(height=600),
|
23 |
+
textbox=gr.Textbox(placeholder="Ask anything…"),
|
24 |
+
retry_btn="🔄 Retry",
|
25 |
+
clear_btn="🗑️ Clear",
|
26 |
+
)
|
27 |
+
|
28 |
+
# 5️⃣ Launch the app
|
29 |
+
if __name__ == "__main__":
|
30 |
+
iface.launch()
|