File size: 1,098 Bytes
e68bea8
 
 
 
 
 
 
 
 
 
de15b75
e68bea8
51ac10a
e68bea8
be4164d
 
 
 
 
 
 
 
 
 
64839fb
e68bea8
be4164d
e68bea8
 
 
 
 
51ac10a
f6622c4
51ac10a
 
de15b75
e68bea8
 
 
 
be4164d
 
e68bea8
 
1344c2d
 
e68bea8
 
de15b75
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 gradio as gr

# def greet(name):
#     return "Hello there, " + name + "!"

# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
# iface.launch()



import gradio as gr
import openai
import json

gpt_models = [
    "gpt-4",
    "gpt-4-0314",
    "gpt-4-32k",
    "gpt-4-32k-0314",
    "gpt-3.5-turbo",
    "gpt-3.5-turbo-0301"
]

def call_openai(msg, api_key, model):
    openai.api_key = api_key
    response = openai.ChatCompletion.create(
      model=model,
      messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": msg}
        ]
    )
    return [
        response["choices"][0]["message"]["content"],
        json.dumps(response, indent=4)
    ]

iface = gr.Interface(
    fn = call_openai,
    inputs = [
        gr.Textbox(placeholder="Message"),
        gr.Textbox(placeholder="OpenAI API key"),
        gr.Dropdown(choices=gpt_models, value=gpt_models[4])
    ],
    outputs = [
        gr.Textbox(label="Assistant message"),
        gr.Textbox(label="Response JSON")
    ]
    )
iface.launch()