Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,32 @@
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import google.generativeai as genai
|
4 |
|
5 |
-
#
|
6 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
|
|
|
|
7 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
8 |
-
chat
|
9 |
|
|
|
10 |
def respond(message, history):
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
|
|
|
|
14 |
iface = gr.ChatInterface(
|
15 |
-
fn
|
16 |
-
title
|
17 |
-
|
18 |
-
|
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()
|
|
|
1 |
+
# app.py
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
import google.generativeai as genai
|
5 |
|
6 |
+
# Configure the Gemini client using an environment variable
|
7 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
8 |
+
|
9 |
+
# Create the model and start a stateful chat session
|
10 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
11 |
+
chat = model.start_chat(history=[])
|
12 |
|
13 |
+
# Define the function that Gradio will call on each user message
|
14 |
def respond(message, history):
|
15 |
+
# Send the user's message to the chat session
|
16 |
+
response = chat.send_message(message)
|
17 |
+
# Return only the model's text response
|
18 |
+
return response.text
|
19 |
|
20 |
+
# Build the Gradio UI
|
21 |
+
# The retry_btn, undo_btn, and clear_btn arguments have been removed
|
22 |
+
# as this functionality is now integrated into the Chatbot component.
|
23 |
iface = gr.ChatInterface(
|
24 |
+
fn=respond,
|
25 |
+
title="Gemini Chatbot",
|
26 |
+
chatbot=gr.Chatbot(height=600, type="messages"),
|
27 |
+
textbox=gr.Textbox(placeholder="Ask anything…", container=False, scale=7),
|
|
|
|
|
|
|
28 |
)
|
29 |
|
30 |
+
# Launch the application
|
31 |
if __name__ == "__main__":
|
32 |
iface.launch()
|