admin08077 commited on
Commit
a0168bf
·
verified ·
1 Parent(s): 8188521

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -1,39 +1,40 @@
1
  import os
2
  import gradio as gr
3
- from google import genai
4
 
5
- # --- Load Gemini API key from Hugging Face secret ---
6
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
7
  if not GEMINI_API_KEY:
8
- raise ValueError("❌ GEMINI_API_KEY not found in environment. Add it in Hugging Face Secrets.")
9
 
10
- # --- Configure Gemini client ---
11
  genai.configure(api_key=GEMINI_API_KEY)
12
- client = genai.Client()
13
 
14
- # Create a persistent chat session
15
- chat = client.chats.create(model="gemini-2.5-flash")
 
16
 
17
- # --- Define chatbot function ---
18
  def chat_with_gemini(user_input, history):
19
  try:
20
- response = chat.send_message(user_input)
21
  history.append((user_input, response.text))
22
  return history, history
23
  except Exception as e:
24
- return history + [(user_input, f"⚠️ Error: {str(e)}")], history
 
25
 
26
  # --- Gradio UI ---
27
  with gr.Blocks(title="Gemini 2.5 Flash Chatbot") as demo:
28
- gr.Markdown("# 🤖 Gemini 2.5 Flash Chatbot\nAsk me anything!")
29
  chatbot = gr.Chatbot()
30
- message = gr.Textbox(placeholder="Type your message and press Enter")
31
- clear = gr.Button("Clear Chat")
32
  state = gr.State([])
33
 
34
- message.submit(chat_with_gemini, inputs=[message, state], outputs=[chatbot, state])
35
  clear.click(lambda: ([], []), None, [chatbot, state])
36
 
37
- # --- Launch app ---
38
  if __name__ == "__main__":
39
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ from google import generativeai as genai
4
 
5
+ # --- Load Gemini API key securely ---
6
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
7
  if not GEMINI_API_KEY:
8
+ raise ValueError("❌ GEMINI_API_KEY not found. Set it in Hugging Face Secrets.")
9
 
10
+ # --- Configure GenAI ---
11
  genai.configure(api_key=GEMINI_API_KEY)
 
12
 
13
+ # --- Initialize Gemini model ---
14
+ model = genai.GenerativeModel("gemini-2.5-flash")
15
+ chat_session = model.start_chat()
16
 
17
+ # --- Define chat function ---
18
  def chat_with_gemini(user_input, history):
19
  try:
20
+ response = chat_session.send_message(user_input)
21
  history.append((user_input, response.text))
22
  return history, history
23
  except Exception as e:
24
+ history.append((user_input, f"⚠️ Error: {str(e)}"))
25
+ return history, history
26
 
27
  # --- Gradio UI ---
28
  with gr.Blocks(title="Gemini 2.5 Flash Chatbot") as demo:
29
+ gr.Markdown("## 🤖 Gemini 2.5 Flash Chatbot\nPowered by Google Generative AI")
30
  chatbot = gr.Chatbot()
31
+ msg = gr.Textbox(placeholder="Type your message...", label="Your Message")
32
+ clear = gr.Button("🧹 Clear")
33
  state = gr.State([])
34
 
35
+ msg.submit(chat_with_gemini, inputs=[msg, state], outputs=[chatbot, state])
36
  clear.click(lambda: ([], []), None, [chatbot, state])
37
 
38
+ # --- Launch ---
39
  if __name__ == "__main__":
40
  demo.launch()