Spaces:
Sleeping
Sleeping
| # 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() |