Jack7510 commited on
Commit
72de8a7
1 Parent(s): 5a8975b

try to catch the error exception of openai API

Browse files
Files changed (1) hide show
  1. app.py +10 -1
app.py CHANGED
@@ -16,8 +16,17 @@ def chatbot(text):
16
  # Extract the generated response and return it
17
  return completion.choices[0].message.content
18
 
 
 
 
 
 
 
 
 
 
19
  # Create the Gradio interface
20
- iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="GPT-3.5 Turbo Chatbot")
21
 
22
  # Launch the interface
23
  iface.launch()
 
16
  # Extract the generated response and return it
17
  return completion.choices[0].message.content
18
 
19
+
20
+ def ask_gpt(prompt):
21
+ try:
22
+ completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}])
23
+ return completion.choices[0].message.content
24
+ except openai.Error as e:
25
+ return f"OpenAI API error: {e}"
26
+
27
+
28
  # Create the Gradio interface
29
+ iface = gr.Interface(fn=ask_gpt, inputs="text", outputs="text", title="GPT-3.5 Turbo Chatbot")
30
 
31
  # Launch the interface
32
  iface.launch()