File size: 1,353 Bytes
a380611
453dbcd
a380611
453dbcd
0aeece4
15e2bf5
 
 
 
453dbcd
8adb407
0aeece4
453dbcd
8adb407
453dbcd
 
 
 
8adb407
453dbcd
 
 
 
 
a380611
453dbcd
 
0aeece4
 
 
 
 
453dbcd
 
a380611
453dbcd
 
0aeece4
453dbcd
 
 
ade71a8
453dbcd
 
 
 
 
cc82464
15e2bf5
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import openai
import gradio as gr

# Define the chat function
def chat(api_key, model, message):
    # Check if an API key has been provided
    if api_key is None:
        return "Please enter your OpenAI API key and try again."
    
    # Set up the OpenAI API request
    response = openai.Completion.create(
        engine=model,
        prompt=message,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
        api_key=api_key,
    )
    
    # Extract the bot's response from the API request
    bot_response = response.choices[0].text.strip()
    
    return bot_response

# Define the Gradio interface
api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None)
model_input = gr.inputs.Dropdown(
    label="Select model",
    choices=["text-davinci-003", "text-davinci-002"],
    default="text-davinci-003",
)
message_input = gr.inputs.Textbox(label="Enter your message here")
output = gr.outputs.Textbox(label="Bot response")

chat_button = gr.Interface(
    fn=chat,
    inputs=[api_key_input, model_input, message_input],
    outputs=output,
    title="OpenAI Chatbot",
    description="Enter your message below to chat with an AI",
    theme="huggingface",
    layout="vertical",
    allow_flagging=False,
    allow_screenshot=False,
    allow_share=False,
)

# Launch the app
chat_button.launch()