Data_request / app.py
Rathapoom's picture
Update app.py
71a90c9 verified
raw
history blame
1.06 kB
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()