Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
-
import openai
|
3 |
import os
|
4 |
from openai import OpenAI
|
5 |
|
6 |
-
#
|
7 |
client = OpenAI(
|
8 |
-
api_key=os.environ.get("OPENAI_API_KEY"),
|
9 |
)
|
10 |
|
11 |
-
|
12 |
def generate_response(prompt):
|
13 |
try:
|
14 |
response = client.chat.completions.create(
|
|
|
15 |
messages=[
|
16 |
{
|
17 |
"role": "user",
|
18 |
-
"content":
|
19 |
}
|
20 |
-
]
|
21 |
-
model="gpt-4o-mini",
|
22 |
)
|
23 |
-
|
|
|
24 |
except Exception as e:
|
25 |
return f"Error: {str(e)}"
|
26 |
|
27 |
-
#
|
28 |
with gr.Blocks() as demo:
|
29 |
gr.Markdown("## Test OpenAI GPT API")
|
30 |
user_input = gr.Textbox(label="Enter your prompt")
|
31 |
output = gr.Textbox(label="GPT Response")
|
32 |
submit = gr.Button("Generate Response")
|
|
|
33 |
submit.click(generate_response, inputs=user_input, outputs=output)
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
from openai import OpenAI
|
4 |
|
5 |
+
# Initialize OpenAI client
|
6 |
client = OpenAI(
|
7 |
+
api_key=os.environ.get("OPENAI_API_KEY"),
|
8 |
)
|
9 |
|
|
|
10 |
def generate_response(prompt):
|
11 |
try:
|
12 |
response = client.chat.completions.create(
|
13 |
+
model="gpt-4o-mini", # Note: "gpt-4o-mini" is not a valid model name
|
14 |
messages=[
|
15 |
{
|
16 |
"role": "user",
|
17 |
+
"content": prompt, # Use the actual prompt instead of hardcoded text
|
18 |
}
|
19 |
+
]
|
|
|
20 |
)
|
21 |
+
# The correct way to access the response content
|
22 |
+
return response.choices[0].message.content
|
23 |
except Exception as e:
|
24 |
return f"Error: {str(e)}"
|
25 |
|
26 |
+
# Create Gradio interface
|
27 |
with gr.Blocks() as demo:
|
28 |
gr.Markdown("## Test OpenAI GPT API")
|
29 |
user_input = gr.Textbox(label="Enter your prompt")
|
30 |
output = gr.Textbox(label="GPT Response")
|
31 |
submit = gr.Button("Generate Response")
|
32 |
+
|
33 |
submit.click(generate_response, inputs=user_input, outputs=output)
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
+
demo.launch()
|