File size: 1,249 Bytes
41e22e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import gradio as gr

def chat_with_openai(api_key, user_input):
    """
    Function to send user input to OpenAI’s Chat Completion API and return the response.
    """
    openai.api_key = api_key
    response = openai.ChatCompletion.create(
        model='gpt-3.5-turbo',  # You can change the model as needed
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": user_input},
        ]
    )
    
    return response.choices[0].message['content']

def main():
    description = "This is a simple interface to interact with OpenAI’s Chat Completion API. Please enter your API key and your message."
    with gr.Blocks() as demo:
        with gr.Row():
            api_key_input = gr.Textbox(label="API Key", placeholder="Enter your OpenAI API key here", show_label=True, type="password")
            user_input = gr.Textbox(label="Your Message", placeholder="Enter your message here")
            submit_btn = gr.Button("Submit")
        output = gr.Textbox(label="Chatbot Response")
        
        submit_btn.click(fn=chat_with_openai, inputs=[api_key_input, user_input], outputs=output)

    demo.launch()

if __name__ == "__main__":
    main()