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