Spaces:
Sleeping
Sleeping
Update app.py with api calls to OpenAI
Browse files
app.py
CHANGED
@@ -1,7 +1,39 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
import gradio as gr
|
3 |
+
import openai
|
4 |
|
5 |
+
# Function to call OpenAI API
|
6 |
+
def get_openai_response(api_key, model, prompt):
|
7 |
+
openai.api_key = api_key
|
8 |
+
response = openai.ChatCompletion.create(
|
9 |
+
model=model,
|
10 |
+
messages=[
|
11 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
12 |
+
{"role": "user", "content": prompt}
|
13 |
+
]
|
14 |
+
)
|
15 |
+
return response['choices'][0]['message']['content']
|
16 |
|
17 |
+
def generate_responses(api_key, prompt):
|
18 |
+
response_gpt3 = get_openai_response(api_key, "gpt-3.5-turbo", prompt)
|
19 |
+
response_gpt4 = get_openai_response(api_key, "gpt-4o", prompt)
|
20 |
+
return response_gpt3, response_gpt4
|
21 |
+
|
22 |
+
# Gradio Interface
|
23 |
+
inputs = [
|
24 |
+
gr.inputs.Textbox(label="OpenAI API Key"),
|
25 |
+
gr.inputs.Textbox(label="Input")
|
26 |
+
]
|
27 |
+
|
28 |
+
outputs = [
|
29 |
+
gr.outputs.Textbox(label="GPT-3.5-Turbo Output"),
|
30 |
+
gr.outputs.Textbox(label="GPT-4o Output")
|
31 |
+
]
|
32 |
+
|
33 |
+
gr.Interface(
|
34 |
+
fn=generate_responses,
|
35 |
+
inputs=inputs,
|
36 |
+
outputs=outputs,
|
37 |
+
title="Compare GPT Models Challenge",
|
38 |
+
description="Enter your OpenAI API Key and your question to get responses from GPT-3.5-Turbo and GPT-4o, compare their results."
|
39 |
+
).launch()
|