Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|