Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
-
client = OpenAI()
|
| 5 |
|
| 6 |
def chat(user_input, history=[]):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def respond(message, chat_history):
|
| 30 |
-
return chat(message, chat_history)
|
| 31 |
-
|
| 32 |
-
msg.submit(respond, [msg, chatbot], [chatbot, chatbot])
|
| 33 |
-
|
| 34 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 5 |
|
| 6 |
def chat(user_input, history=[]):
|
| 7 |
+
try:
|
| 8 |
+
messages = [{"role": "system", "content": "You are Jarvis, a smart AI assistant."}]
|
| 9 |
+
|
| 10 |
+
for h in history:
|
| 11 |
+
messages.append({"role": "user", "content": h[0]})
|
| 12 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 13 |
+
|
| 14 |
+
messages.append({"role": "user", "content": user_input})
|
| 15 |
+
|
| 16 |
+
response = client.chat.completions.create(
|
| 17 |
+
model="gpt-4o-mini",
|
| 18 |
+
messages=messages
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
reply = response.choices[0].message.content
|
| 22 |
+
history.append((user_input, reply))
|
| 23 |
+
|
| 24 |
+
return history, history
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return [(user_input, "Error: " + str(e))], history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|