Spaces:
Sleeping
Sleeping
File size: 1,062 Bytes
e739911 049e1fb 9336a25 e739911 71a90c9 e2aceb6 71a90c9 e2aceb6 db3aeff e739911 e2aceb6 71a90c9 e2aceb6 71a90c9 e2aceb6 71a90c9 e739911 71a90c9 e739911 71a90c9 e739911 71a90c9 e739911 71a90c9 |
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 |
import gradio as gr
import os
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
def generate_response(prompt):
try:
response = client.chat.completions.create(
model="gpt-4o-mini", # Note: "gpt-4o-mini" is not a valid model name
messages=[
{
"role": "user",
"content": prompt, # Use the actual prompt instead of hardcoded text
}
]
)
# The correct way to access the response content
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# Create Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## Test OpenAI GPT API")
user_input = gr.Textbox(label="Enter your prompt")
output = gr.Textbox(label="GPT Response")
submit = gr.Button("Generate Response")
submit.click(generate_response, inputs=user_input, outputs=output)
if __name__ == "__main__":
demo.launch() |