Spaces:
Running
Running
import gradio as gr | |
import openai | |
# Function to call OpenAI API | |
def get_openai_response(api_key, model, prompt): | |
openai.api_key = api_key | |
response = openai.ChatCompletion.create( | |
model=model, | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
return response['choices'][0]['message']['content'] | |
def generate_responses(api_key, prompt): | |
response_gpt3 = get_openai_response(api_key, "gpt-3.5-turbo", prompt) | |
response_gpt4 = get_openai_response(api_key, "gpt-4", prompt) | |
return response_gpt3, response_gpt4 | |
# Gradio Interface | |
inputs = [ | |
gr.Textbox(label="OpenAI API Key"), | |
gr.Textbox(label="Your Question") | |
] | |
outputs = [ | |
gr.Textbox(label="GPT-3.5 Turbo Response"), | |
gr.Textbox(label="GPT-4 Response") | |
] | |
gr.Interface( | |
fn=generate_responses, | |
inputs=inputs, | |
outputs=outputs, | |
title="AI Prompt Challenge", | |
description="Enter your OpenAI API Key and your question to get responses from GPT-3.5 Turbo and GPT-4 models." | |
).launch() | |