File size: 993 Bytes
8cb5e9c
 
 
 
 
66420bf
8cb5e9c
 
 
 
 
 
 
 
 
 
 
 
66420bf
8cb5e9c
 
 
 
 
 
5e6fde1
 
 
 
 
 
 
8cb5e9c
5e6fde1
 
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
import openai
import gradio as gr

openai.api_key = "sk-" # Replace this with your API key: https://beta.openai.com/docs/quickstart/add-your-api-key

# openai
def openai_chat(prompt):
    completions = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        temperature=0.5,
    )

    message = completions.choices[0].text
    return message.strip()

# gradio
def chatbot(key, input, history=[]):
    openai.api_key = key
    output = openai_chat(input)
    history.append((input, output))
    return history, history

keyTxt = gr.Textbox(
                        show_label=True,
                        placeholder=f"Your API-key...",
                        type="password",
                        visible=True,
                        label="API-Key",
                    )
gr.Interface(fn = chatbot,
             inputs = [keyTxt,"text",'state'],
             outputs = ["chatbot",'state']).launch(debug = True)