File size: 959 Bytes
f1667c0
1756b81
 
f1667c0
1756b81
5e6e9bb
 
54fb2a7
f1667c0
1756b81
 
54fb2a7
 
 
1756b81
 
54fb2a7
1756b81
72de8a7
 
 
 
 
 
 
 
 
1756b81
72de8a7
1756b81
 
c3e79ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr
import openai
import os

# Set up the OpenAI API credentials
# openai.api_key = os.environ["OPENAI_API_KEY"]
openai.api_key = os.getenv("OPENAI_API_KEY")
model = "gpt-3.5-turbo"

# Define the chatbot function
def chatbot(text):

    # Call OpenAI API to generate text completion
    completion = openai.ChatCompletion.create(model=model, messages=[{"role": "user", "content": text}])

    # Extract the generated response and return it
    return completion.choices[0].message.content


def ask_gpt(prompt):
    try:
        completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}])
        return completion.choices[0].message.content
    except openai.Error as e:
        return f"OpenAI API error: {e}"
        
    
# Create the Gradio interface
iface = gr.Interface(fn=ask_gpt, inputs="text", outputs="text", title="GPT-3.5 Turbo Chatbot")

# Launch the interface
iface.launch()