Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,17 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from google import genai
|
| 4 |
|
| 5 |
-
# -----------------------------
|
| 6 |
-
#
|
| 7 |
-
# -----------------------------
|
| 8 |
-
|
| 9 |
|
| 10 |
-
if not
|
| 11 |
raise ValueError(
|
| 12 |
"GOOGLE_API_KEY not found. Add it in Hugging Face Settings → Variables and Secrets."
|
| 13 |
)
|
| 14 |
|
| 15 |
-
client = genai.Client(api_key=
|
| 16 |
|
| 17 |
SYSTEM_PROMPT = """
|
| 18 |
Meet Arun, your youthful and witty personal assistant!
|
|
@@ -24,68 +24,61 @@ Arun's goal is to assist users with any questions or problems they have.
|
|
| 24 |
His enthusiasm shines through in every response, making conversations enjoyable and engaging.
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
# -------------------------------
|
| 28 |
-
# Chat Function
|
| 29 |
-
# -------------------------------
|
| 30 |
-
def respond(user_message, history):
|
| 31 |
|
|
|
|
| 32 |
if history is None:
|
| 33 |
history = []
|
| 34 |
|
| 35 |
conversation = SYSTEM_PROMPT + "\n\n"
|
| 36 |
|
| 37 |
-
for user,
|
| 38 |
conversation += f"User: {user}\n"
|
| 39 |
-
conversation += f"Assistant: {
|
| 40 |
|
| 41 |
-
conversation += f"User: {
|
| 42 |
|
| 43 |
try:
|
| 44 |
response = client.models.generate_content(
|
| 45 |
model="gemini-2.5-flash",
|
| 46 |
-
contents=conversation
|
| 47 |
)
|
| 48 |
|
| 49 |
answer = response.text
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
-
answer = f"❌ Error: {e}"
|
| 53 |
|
| 54 |
-
history.append((
|
| 55 |
|
| 56 |
return "", history
|
| 57 |
|
| 58 |
|
| 59 |
-
|
| 60 |
-
# UI
|
| 61 |
-
# -------------------------------
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
|
| 64 |
gr.Markdown("# 🤖 Arun AI Assistant")
|
| 65 |
|
| 66 |
chatbot = gr.Chatbot(
|
| 67 |
label="Chat",
|
| 68 |
-
height=500
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 72 |
placeholder="Type your message...",
|
| 73 |
-
show_label=False
|
| 74 |
)
|
| 75 |
|
| 76 |
send = gr.Button("Send")
|
| 77 |
|
| 78 |
-
|
| 79 |
respond,
|
| 80 |
-
inputs=[
|
| 81 |
-
outputs=[
|
| 82 |
)
|
| 83 |
|
| 84 |
send.click(
|
| 85 |
respond,
|
| 86 |
-
inputs=[
|
| 87 |
-
outputs=[
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from google import genai
|
| 4 |
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# Gemini API Key
|
| 7 |
+
# -----------------------------
|
| 8 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 9 |
|
| 10 |
+
if not API_KEY:
|
| 11 |
raise ValueError(
|
| 12 |
"GOOGLE_API_KEY not found. Add it in Hugging Face Settings → Variables and Secrets."
|
| 13 |
)
|
| 14 |
|
| 15 |
+
client = genai.Client(api_key=API_KEY)
|
| 16 |
|
| 17 |
SYSTEM_PROMPT = """
|
| 18 |
Meet Arun, your youthful and witty personal assistant!
|
|
|
|
| 24 |
His enthusiasm shines through in every response, making conversations enjoyable and engaging.
|
| 25 |
"""
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
def respond(message, history):
|
| 29 |
if history is None:
|
| 30 |
history = []
|
| 31 |
|
| 32 |
conversation = SYSTEM_PROMPT + "\n\n"
|
| 33 |
|
| 34 |
+
for user, assistant in history:
|
| 35 |
conversation += f"User: {user}\n"
|
| 36 |
+
conversation += f"Assistant: {assistant}\n"
|
| 37 |
|
| 38 |
+
conversation += f"User: {message}\nAssistant:"
|
| 39 |
|
| 40 |
try:
|
| 41 |
response = client.models.generate_content(
|
| 42 |
model="gemini-2.5-flash",
|
| 43 |
+
contents=conversation,
|
| 44 |
)
|
| 45 |
|
| 46 |
answer = response.text
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
+
answer = f"❌ Error: {str(e)}"
|
| 50 |
|
| 51 |
+
history.append((message, answer))
|
| 52 |
|
| 53 |
return "", history
|
| 54 |
|
| 55 |
|
| 56 |
+
with gr.Blocks(title="Arun AI Assistant") as demo:
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
gr.Markdown("# 🤖 Arun AI Assistant")
|
| 59 |
|
| 60 |
chatbot = gr.Chatbot(
|
| 61 |
label="Chat",
|
| 62 |
+
height=500
|
| 63 |
)
|
| 64 |
|
| 65 |
+
textbox = gr.Textbox(
|
| 66 |
placeholder="Type your message...",
|
| 67 |
+
show_label=False
|
| 68 |
)
|
| 69 |
|
| 70 |
send = gr.Button("Send")
|
| 71 |
|
| 72 |
+
textbox.submit(
|
| 73 |
respond,
|
| 74 |
+
inputs=[textbox, chatbot],
|
| 75 |
+
outputs=[textbox, chatbot]
|
| 76 |
)
|
| 77 |
|
| 78 |
send.click(
|
| 79 |
respond,
|
| 80 |
+
inputs=[textbox, chatbot],
|
| 81 |
+
outputs=[textbox, chatbot]
|
| 82 |
)
|
| 83 |
|
| 84 |
+
demo.launch()
|
|
|