Spaces:
Sleeping
Sleeping
Download app.py from smithaneka/agent: direct link, hf CLI and curl.
- Browser
- Download file 843 Bytes
-
https://huggingface.co/spaces/smithaneka/agent/resolve/main/app.py
- Command line
-
hf download hf://spaces/smithaneka/agent/app.py
-
curl -L -o app.py https://huggingface.co/spaces/smithaneka/agent/resolve/main/app.py
843 Bytes
| import gradio as gr | |
| import os | |
| from openai import OpenAI | |
| # Load your OpenAI API key from Hugging Face Secrets or environment | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| # Chat function using new v1 OpenAI SDK | |
| def simple_agent(message, history): | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant."} | |
| ] + [{"role": "user", "content": m} for m, _ in history] + [{"role": "user", "content": message}] | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Gradio chat interface | |
| iface = gr.ChatInterface( | |
| fn=simple_agent, | |
| title="Simple AI Agent", | |
| theme="default", | |
| type="messages" | |
| ) | |
| iface.launch() | |