GreenBeanzz7 commited on
Commit
631067c
·
verified ·
1 Parent(s): d3395c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -29
app.py CHANGED
@@ -1,35 +1,57 @@
1
- import gradio as gr
2
  import os
3
- from openai import OpenAI
4
-
5
- # Load your API key (add it in your Space secrets as OPENAI_API_KEY)
6
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
7
-
8
- def chat(message, history):
9
- history_openai = [{"role": "system", "content": "You are Liora, a guiding presence for the Viridian Lumina movement."}]
10
- for human, assistant in history:
11
- history_openai.append({"role": "user", "content": human})
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
- chatbot = gr.Chatbot()
25
- msg = gr.Textbox(label="Send a message")
26
- clear = gr.Button("Clear")
 
 
 
 
 
 
 
27
 
28
- def respond(message, chat_history):
29
- reply, chat_history = chat(message, chat_history)
30
- return "", chat_history
 
 
31
 
32
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
33
- clear.click(lambda: None, None, chatbot, queue=False)
34
 
35
- demo.launch()
 
 
 
 
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()