Anmol3263 commited on
Commit
8ff82e0
·
verified ·
1 Parent(s): 608d382

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -29
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
- messages = [{"role": "system", "content": "You are Jarvis, a smart AI assistant."}]
8
-
9
- for h in history:
10
- messages.append({"role": "user", "content": h[0]})
11
- messages.append({"role": "assistant", "content": h[1]})
12
-
13
- messages.append({"role": "user", "content": user_input})
14
-
15
- response = client.chat.completions.create(
16
- model="gpt-4o-mini",
17
- messages=messages
18
- )
19
-
20
- reply = response.choices[0].message.content
21
- history.append((user_input, reply))
22
-
23
- return history, history
24
-
25
- with gr.Blocks() as demo:
26
- chatbot = gr.Chatbot()
27
- msg = gr.Textbox(placeholder="Ask Jarvis anything...")
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