MyChatGPTTurbo / app.py
Gopala Krishna
added try except
2f5ca58
raw
history blame
899 Bytes
import os
import openai
import gradio as gr
try:
openai.api_key = os.environ["OPENAPI_API_KEY"]
except KeyError:
error_message = "System is at capacity right now.Please try again later"
print(error_message)
def chatbot(input):
return error_message
else:
messages = [
{"role": "system", "content": "My AI Assistant"},
]
def chatbot(input):
if input:
messages.append({"role": "user", "content": input})
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply
gr.Interface(
fn=chatbot,
inputs=gr.inputs.Textbox(lines=7, label="Query"),
outputs=gr.outputs.Textbox(label="Response"),
theme="compact"
).launch()