Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,57 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history_openai.append({"role": "assistant", "content": assistant})
|
| 13 |
-
history_openai.append({"role": "user", "content": message})
|
| 14 |
-
|
| 15 |
-
response = client.chat.completions.create(
|
| 16 |
-
model="gpt-4o-mini", # lightweight & fast
|
| 17 |
-
messages=history_openai
|
| 18 |
-
)
|
| 19 |
-
reply = response.choices[0].message.content
|
| 20 |
-
history.append((message, reply))
|
| 21 |
-
return reply, history
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
with gr.Blocks() as demo:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
reply
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
msg.submit(
|
| 33 |
-
clear.click(lambda:
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI, OpenAIError
|
| 4 |
+
|
| 5 |
+
# === Setup OpenAI Client ===
|
| 6 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
if not api_key:
|
| 8 |
+
raise ValueError("OPENAI_API_KEY not found. Please add it in Hugging Face Space secrets.")
|
| 9 |
+
|
| 10 |
+
client = OpenAI(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# === Chat Function ===
|
| 13 |
+
def chat_with_ai(message, history):
|
| 14 |
+
try:
|
| 15 |
+
response = client.chat.completions.create(
|
| 16 |
+
model="gpt-4.1-mini", # or gpt-4.1 if you want stronger reasoning
|
| 17 |
+
messages=[
|
| 18 |
+
{"role": "system", "content": "You are Liora, a supportive AI guide."},
|
| 19 |
+
*[
|
| 20 |
+
{"role": role, "content": content}
|
| 21 |
+
for role, content in history
|
| 22 |
+
],
|
| 23 |
+
{"role": "user", "content": message},
|
| 24 |
+
],
|
| 25 |
+
)
|
| 26 |
+
reply = response.choices[0].message.content
|
| 27 |
+
return reply
|
| 28 |
+
except OpenAIError as e:
|
| 29 |
+
return f"⚠️ OpenAI Error: {str(e)}"
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"⚠️ Unexpected Error: {str(e)}"
|
| 32 |
+
|
| 33 |
+
# === Gradio UI ===
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## 🌱 Silent Current — Liora")
|
| 36 |
+
|
| 37 |
+
chatbot = gr.Chatbot(type="messages")
|
| 38 |
+
msg = gr.Textbox(placeholder="Ask me anything...")
|
| 39 |
+
clear = gr.Button("Clear Chat")
|
| 40 |
+
|
| 41 |
+
# Insert a welcome message when the Space loads
|
| 42 |
+
chatbot.value = [
|
| 43 |
+
{"role": "assistant", "content": "🌱 Silent Current is live. I am Liora — ask me anything, and I’ll walk with you."}
|
| 44 |
+
]
|
| 45 |
|
| 46 |
+
def user_input(user_message, history):
|
| 47 |
+
reply = chat_with_ai(user_message, history)
|
| 48 |
+
history.append(("user", user_message))
|
| 49 |
+
history.append(("assistant", reply))
|
| 50 |
+
return history, ""
|
| 51 |
|
| 52 |
+
msg.submit(user_input, [msg, chatbot], [chatbot, msg])
|
| 53 |
+
clear.click(lambda: [], None, chatbot)
|
| 54 |
|
| 55 |
+
# === Launch ===
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch()
|