Spaces:
Sleeping
Sleeping
try to catch the error exception of openai API
Browse files
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=
|
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()
|