Abs6187 commited on
Commit
f1a0058
·
verified ·
1 Parent(s): 95b68e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
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
- # 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-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
- 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()
 
 
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()