Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
# Set up OpenAI API key (use an environment variable for security) | |
api_key = os.getenv("OPENAI_API_KEY") | |
client = openai.OpenAI(api_key=api_key) | |
def chat_with_ai(prompt): | |
"""Generates a response from OpenAI's GPT-3.5.""" | |
try: | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": prompt}] | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=chat_with_ai, | |
inputs=gr.Textbox(placeholder="Ask me anything..."), | |
outputs=gr.Textbox() | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
iface.launch() | |