fix: updated for Gradio messages API
Browse files
app.py
CHANGED
|
@@ -2,35 +2,32 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
#
|
| 6 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
-
def chat_with_openai(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
response = client.chat.completions.create(
|
| 15 |
model="gpt-4o-mini",
|
| 16 |
-
messages=
|
| 17 |
)
|
| 18 |
reply = response.choices[0].message.content
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
response = client.chat.completions.create(
|
| 23 |
-
model="gpt-4o-mini",
|
| 24 |
-
messages=messages,
|
| 25 |
-
)
|
| 26 |
-
reply = response.choices[0].message.content
|
| 27 |
-
history = history + [(message, reply)]
|
| 28 |
-
return history + [{"role": "assistant", "content": reply}]
|
| 29 |
|
| 30 |
demo = gr.ChatInterface(
|
| 31 |
fn=chat_with_openai,
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
+
# Create client using key from Hugging Face → Settings → Variables
|
| 6 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
+
def chat_with_openai(messages, history):
|
| 9 |
+
"""
|
| 10 |
+
messages: list of dicts [{"role": "user"/"assistant"/"system", "content": "..."}]
|
| 11 |
+
history: ignored; Gradio passes messages now.
|
| 12 |
+
"""
|
| 13 |
+
# prepend system prompt
|
| 14 |
+
context = [{"role": "system", "content": "You are Neuro, a concise helpful AI assistant."}]
|
| 15 |
+
context.extend(messages)
|
| 16 |
|
| 17 |
response = client.chat.completions.create(
|
| 18 |
model="gpt-4o-mini",
|
| 19 |
+
messages=context,
|
| 20 |
)
|
| 21 |
reply = response.choices[0].message.content
|
| 22 |
+
# Append assistant message for the next turn
|
| 23 |
+
messages.append({"role": "assistant", "content": reply})
|
| 24 |
+
return messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
demo = gr.ChatInterface(
|
| 27 |
fn=chat_with_openai,
|
| 28 |
+
type="messages", # REQUIRED to use new message schema
|
| 29 |
+
title="🧠 Neuro Chat Demo",
|
| 30 |
+
description="OpenAI-powered chat demo using the new Gradio message API."
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|