Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
# Gemini API Key
|
|
@@ -9,13 +9,11 @@ API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
| 9 |
|
| 10 |
if not API_KEY:
|
| 11 |
raise ValueError(
|
| 12 |
-
"GOOGLE_API_KEY not found
|
| 13 |
)
|
| 14 |
|
| 15 |
-
client = genai.Client(api_key=API_KEY)
|
| 16 |
-
|
| 17 |
SYSTEM_PROMPT = """
|
| 18 |
-
Meet Arun, your youthful and witty personal assistant
|
| 19 |
|
| 20 |
At 21 years old, he is full of energy and always eager to help.
|
| 21 |
|
|
@@ -24,6 +22,11 @@ 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 |
def respond(message, history):
|
| 29 |
if history is None:
|
|
@@ -31,22 +34,44 @@ def respond(message, history):
|
|
| 31 |
|
| 32 |
conversation = SYSTEM_PROMPT + "\n\n"
|
| 33 |
|
| 34 |
-
for user,
|
| 35 |
-
conversation += f"User: {user}\n"
|
| 36 |
-
conversation += f"Assistant: {assistant}\n"
|
| 37 |
|
| 38 |
conversation += f"User: {message}\nAssistant:"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
response =
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
-
answer = f"❌ Error: {
|
| 50 |
|
| 51 |
history.append((message, answer))
|
| 52 |
|
|
@@ -58,27 +83,28 @@ with gr.Blocks(title="Arun AI Assistant") as demo:
|
|
| 58 |
gr.Markdown("# 🤖 Arun AI Assistant")
|
| 59 |
|
| 60 |
chatbot = gr.Chatbot(
|
| 61 |
-
label="
|
| 62 |
-
height=500
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
placeholder="Type your message...",
|
| 67 |
-
show_label=False
|
| 68 |
)
|
| 69 |
|
| 70 |
send = gr.Button("Send")
|
| 71 |
|
| 72 |
-
|
| 73 |
respond,
|
| 74 |
-
inputs=[
|
| 75 |
-
outputs=[
|
| 76 |
)
|
| 77 |
|
| 78 |
send.click(
|
| 79 |
respond,
|
| 80 |
-
inputs=[
|
| 81 |
-
outputs=[
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
# Gemini 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 |
SYSTEM_PROMPT = """
|
| 16 |
+
Meet Arun, your youthful and witty personal assistant.
|
| 17 |
|
| 18 |
At 21 years old, he is full of energy and always eager to help.
|
| 19 |
|
|
|
|
| 22 |
His enthusiasm shines through in every response, making conversations enjoyable and engaging.
|
| 23 |
"""
|
| 24 |
|
| 25 |
+
API_URL = (
|
| 26 |
+
"https://generativelanguage.googleapis.com/v1beta/models/"
|
| 27 |
+
"gemini-flash-latest:generateContent"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
|
| 31 |
def respond(message, history):
|
| 32 |
if history is None:
|
|
|
|
| 34 |
|
| 35 |
conversation = SYSTEM_PROMPT + "\n\n"
|
| 36 |
|
| 37 |
+
for user, bot in history:
|
| 38 |
+
conversation += f"User: {user}\nAssistant: {bot}\n"
|
|
|
|
| 39 |
|
| 40 |
conversation += f"User: {message}\nAssistant:"
|
| 41 |
|
| 42 |
+
headers = {
|
| 43 |
+
"Content-Type": "application/json",
|
| 44 |
+
"X-goog-api-key": API_KEY,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
payload = {
|
| 48 |
+
"contents": [
|
| 49 |
+
{
|
| 50 |
+
"parts": [
|
| 51 |
+
{
|
| 52 |
+
"text": conversation
|
| 53 |
+
}
|
| 54 |
+
]
|
| 55 |
+
}
|
| 56 |
+
]
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
try:
|
| 60 |
+
response = requests.post(
|
| 61 |
+
API_URL,
|
| 62 |
+
headers=headers,
|
| 63 |
+
json=payload,
|
| 64 |
+
timeout=60,
|
| 65 |
)
|
| 66 |
|
| 67 |
+
response.raise_for_status()
|
| 68 |
+
|
| 69 |
+
data = response.json()
|
| 70 |
+
|
| 71 |
+
answer = data["candidates"][0]["content"]["parts"][0]["text"]
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
answer = f"❌ Error: {e}"
|
| 75 |
|
| 76 |
history.append((message, answer))
|
| 77 |
|
|
|
|
| 83 |
gr.Markdown("# 🤖 Arun AI Assistant")
|
| 84 |
|
| 85 |
chatbot = gr.Chatbot(
|
| 86 |
+
label="Arun AI Assistant",
|
| 87 |
+
height=500,
|
| 88 |
)
|
| 89 |
|
| 90 |
+
msg = gr.Textbox(
|
| 91 |
placeholder="Type your message...",
|
| 92 |
+
show_label=False,
|
| 93 |
)
|
| 94 |
|
| 95 |
send = gr.Button("Send")
|
| 96 |
|
| 97 |
+
msg.submit(
|
| 98 |
respond,
|
| 99 |
+
inputs=[msg, chatbot],
|
| 100 |
+
outputs=[msg, chatbot],
|
| 101 |
)
|
| 102 |
|
| 103 |
send.click(
|
| 104 |
respond,
|
| 105 |
+
inputs=[msg, chatbot],
|
| 106 |
+
outputs=[msg, chatbot],
|
| 107 |
)
|
| 108 |
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|