Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,25 @@
|
|
1 |
-
# chat_app.py
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
import google.generativeai as genai
|
5 |
|
6 |
-
#
|
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-2.0-flash") # 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 |
-
|
16 |
-
return reply.text # only the assistant reply
|
17 |
|
18 |
-
#
|
19 |
iface = gr.ChatInterface(
|
20 |
-
fn=respond,
|
21 |
-
title="Gemini Chatbot",
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
)
|
27 |
|
28 |
-
# 5️⃣ Launch the app
|
29 |
if __name__ == "__main__":
|
30 |
iface.launch()
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import google.generativeai as genai
|
4 |
|
5 |
+
# --- Gemini -------------------------------------------------------
|
6 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
7 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
8 |
+
chat = model.start_chat() # keeps its own history
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
def respond(message, history):
|
11 |
+
return chat.send_message(message).text # the reply only
|
|
|
12 |
|
13 |
+
# --- Gradio -------------------------------------------------------
|
14 |
iface = gr.ChatInterface(
|
15 |
+
fn = respond,
|
16 |
+
title = "Gemini Chatbot",
|
17 |
+
description = "Ask anything – powered by Google Gemini",
|
18 |
+
chatbot = gr.Chatbot(height=600, type="messages"), # <- new type
|
19 |
+
textbox = gr.Textbox(placeholder="Type a message…"),
|
20 |
+
retry_btn = "🔄 Retry", # works on Gradio >= 4.19
|
21 |
+
clear_btn = "🗑️ Clear",
|
22 |
)
|
23 |
|
|
|
24 |
if __name__ == "__main__":
|
25 |
iface.launch()
|