Spaces:
No application file
No application file
Update app.py
Browse files
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
|
| 6 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 7 |
if not GEMINI_API_KEY:
|
| 8 |
-
raise ValueError("❌ GEMINI_API_KEY not found
|
| 9 |
|
| 10 |
-
# --- Configure
|
| 11 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 12 |
-
client = genai.Client()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
# --- Define
|
| 18 |
def chat_with_gemini(user_input, history):
|
| 19 |
try:
|
| 20 |
-
response =
|
| 21 |
history.append((user_input, response.text))
|
| 22 |
return history, history
|
| 23 |
except Exception as e:
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
# --- Gradio UI ---
|
| 27 |
with gr.Blocks(title="Gemini 2.5 Flash Chatbot") as demo:
|
| 28 |
-
gr.Markdown("
|
| 29 |
chatbot = gr.Chatbot()
|
| 30 |
-
|
| 31 |
-
clear = gr.Button("Clear
|
| 32 |
state = gr.State([])
|
| 33 |
|
| 34 |
-
|
| 35 |
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 36 |
|
| 37 |
-
# --- Launch
|
| 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()
|