Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,41 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
# Get API key from Hugging Face secrets
|
| 6 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
|
|
|
|
|
|
| 8 |
if not api_key:
|
| 9 |
raise ValueError("OPENAI_API_KEY not found. Please add it in Hugging Face Space secrets.")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
|
| 14 |
# Chat function
|
| 15 |
-
def
|
| 16 |
-
messages
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
messages
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
response = openai.chat.completions.create(
|
| 27 |
-
model="gpt-4o-mini",
|
| 28 |
-
messages=messages
|
| 29 |
-
)
|
| 30 |
-
reply = response.choices[0].message.content
|
| 31 |
-
return reply
|
| 32 |
-
except Exception as e:
|
| 33 |
-
return f"Error: {str(e)}"
|
| 34 |
-
|
| 35 |
-
# Gradio Chatbot
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown("## 🌌 SilentCurrent
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
history.append((user_message, reply))
|
| 45 |
-
return history, ""
|
| 46 |
|
| 47 |
-
|
| 48 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 49 |
|
| 50 |
-
# Launch
|
| 51 |
if __name__ == "__main__":
|
| 52 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load API key from Hugging Face Space secrets
|
| 6 |
+
api_key = os.environ.get("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 |
+
# Initialize OpenAI client
|
| 11 |
+
client = OpenAI(api_key=api_key)
|
| 12 |
|
| 13 |
# Chat function
|
| 14 |
+
def chat_with_openai(messages, user_input):
|
| 15 |
+
messages.append({"role": "user", "content": user_input})
|
| 16 |
+
response = client.chat.completions.create(
|
| 17 |
+
model="gpt-4o-mini",
|
| 18 |
+
messages=messages
|
| 19 |
+
)
|
| 20 |
+
reply = response.choices[0].message.content
|
| 21 |
+
messages.append({"role": "assistant", "content": reply})
|
| 22 |
+
return messages, messages
|
| 23 |
+
|
| 24 |
+
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
+
gr.Markdown("## 🌌 SilentCurrent – Powered by OpenAI")
|
| 27 |
+
|
| 28 |
+
chatbot = gr.Chatbot(type="messages")
|
| 29 |
+
state = gr.State([])
|
| 30 |
+
|
| 31 |
+
with gr.Row():
|
| 32 |
+
txt = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
| 33 |
|
| 34 |
+
def respond(user_input, state):
|
| 35 |
+
return chat_with_openai(state, user_input)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
txt.submit(respond, [txt, state], [chatbot, state])
|
|
|
|
| 38 |
|
| 39 |
+
# Launch app
|
| 40 |
if __name__ == "__main__":
|
| 41 |
demo.launch()
|