Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,33 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import google.generativeai as genai # Connect to Google Gemini API
|
| 5 |
|
| 6 |
-
|
| 7 |
-
# 🛠️ Gemini API Setup
|
| 8 |
-
# -------------------------
|
| 9 |
|
| 10 |
-
|
| 11 |
-
GOOGLE_API_KEY
|
| 12 |
|
| 13 |
-
# Configure Gemini API
|
| 14 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 15 |
|
| 16 |
-
# -------------------------
|
| 17 |
-
# 💬 Chat Function
|
| 18 |
-
# -------------------------
|
| 19 |
-
|
| 20 |
-
# Main function for conversation with Gemini
|
| 21 |
def chat_with_gemini(history, message):
|
| 22 |
-
# Convert Gradio’s history to a simple prompt for Gemini
|
| 23 |
conversation = ""
|
| 24 |
for user_msg, bot_msg in history:
|
| 25 |
-
conversation += f"User: {user_msg}\n"
|
| 26 |
-
|
| 27 |
-
conversation += f"Assistant: {bot_msg}\n"
|
| 28 |
-
|
| 29 |
-
# Add the new user message
|
| 30 |
conversation += f"User: {message}\nAssistant:"
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 35 |
response = model.generate_content(conversation)
|
| 36 |
bot_reply = response.text.strip()
|
| 37 |
except Exception as e:
|
| 38 |
-
|
| 39 |
-
bot_reply = f"⚠️ Error: {str(e)}"
|
| 40 |
|
| 41 |
-
# Add to history
|
| 42 |
history.append((message, bot_reply))
|
| 43 |
-
|
| 44 |
-
# Return for Gradio
|
| 45 |
return history, history
|
| 46 |
|
| 47 |
-
# -------------------------
|
| 48 |
-
# 🖼️ Gradio Chat Interface (UI)
|
| 49 |
-
# -------------------------
|
| 50 |
-
|
| 51 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 52 |
-
gr.Markdown(
|
| 53 |
-
"""
|
| 54 |
-
# 🤖 EchoForge Chatbot
|
| 55 |
-
Chat with Google's Gemini AI! Type below to start.
|
| 56 |
-
"""
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
chatbot = gr.Chatbot([], height=400)
|
| 60 |
msg = gr.Textbox(placeholder="Type your message...")
|
| 61 |
clear = gr.Button("Clear Chat")
|
|
@@ -63,9 +35,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 63 |
msg.submit(chat_with_gemini, [chatbot, msg], [chatbot, chatbot])
|
| 64 |
clear.click(lambda: [], outputs=[chatbot])
|
| 65 |
|
| 66 |
-
|
| 67 |
-
# 🚀 Launch the App
|
| 68 |
-
# -------------------------
|
| 69 |
-
|
| 70 |
-
if __name__ == "__main__":
|
| 71 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import google.generativeai as genai
|
|
|
|
| 4 |
|
| 5 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
if not GOOGLE_API_KEY:
|
| 8 |
+
raise ValueError("GOOGLE_API_KEY is missing. Add it in HF Secrets.")
|
| 9 |
|
|
|
|
| 10 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def chat_with_gemini(history, message):
|
|
|
|
| 13 |
conversation = ""
|
| 14 |
for user_msg, bot_msg in history:
|
| 15 |
+
conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
| 17 |
conversation += f"User: {message}\nAssistant:"
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
|
|
|
| 21 |
response = model.generate_content(conversation)
|
| 22 |
bot_reply = response.text.strip()
|
| 23 |
except Exception as e:
|
| 24 |
+
bot_reply = f"⚠️ Error: {e}"
|
|
|
|
| 25 |
|
|
|
|
| 26 |
history.append((message, bot_reply))
|
|
|
|
|
|
|
| 27 |
return history, history
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 30 |
+
gr.Markdown("# 🤖 EchoForge Chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
chatbot = gr.Chatbot([], height=400)
|
| 32 |
msg = gr.Textbox(placeholder="Type your message...")
|
| 33 |
clear = gr.Button("Clear Chat")
|
|
|
|
| 35 |
msg.submit(chat_with_gemini, [chatbot, msg], [chatbot, chatbot])
|
| 36 |
clear.click(lambda: [], outputs=[chatbot])
|
| 37 |
|
| 38 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|