Spaces:
Sleeping
Sleeping
adding error handling
Browse files
README.md
CHANGED
@@ -45,6 +45,27 @@ This app is configured for easy deployment on Hugging Face Spaces. The Docker co
|
|
45 |
|
46 |
Remember to add your OpenAI API key as a secret in the Hugging Face Space settings.
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
## License
|
49 |
|
50 |
MIT
|
|
|
45 |
|
46 |
Remember to add your OpenAI API key as a secret in the Hugging Face Space settings.
|
47 |
|
48 |
+
## Hugging Face Spaces Deployment
|
49 |
+
|
50 |
+
To deploy this app on Hugging Face Spaces:
|
51 |
+
|
52 |
+
1. Fork this repository to your GitHub account
|
53 |
+
2. Create a new Space on Hugging Face:
|
54 |
+
- Choose "Docker" as the SDK
|
55 |
+
- Connect to your GitHub repository
|
56 |
+
|
57 |
+
3. **Important**: Add your OpenAI API key to the Space:
|
58 |
+
- Go to your Space's Settings
|
59 |
+
- Find "Repository Secrets"
|
60 |
+
- Add a new secret:
|
61 |
+
- Name: `OPENAI_API_KEY`
|
62 |
+
- Value: Your OpenAI API key
|
63 |
+
- Click "Add Secret"
|
64 |
+
|
65 |
+
4. The Space will automatically rebuild with your API key configured
|
66 |
+
|
67 |
+
Note: Make sure your OpenAI API key has sufficient credits and proper permissions.
|
68 |
+
|
69 |
## License
|
70 |
|
71 |
MIT
|
app.py
CHANGED
@@ -21,27 +21,43 @@ SYSTEM_PROMPT = "You are a helpful, friendly AI assistant. Provide clear and con
|
|
21 |
@cl.on_chat_start
|
22 |
async def start():
|
23 |
"""
|
24 |
-
Initialize the chat session
|
25 |
-
- Create OpenAI client
|
26 |
-
- Set up message history with system prompt
|
27 |
-
- Configure model settings
|
28 |
-
- Send welcome message
|
29 |
"""
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
@cl.on_message
|
|
|
21 |
@cl.on_chat_start
|
22 |
async def start():
|
23 |
"""
|
24 |
+
Initialize the chat session
|
|
|
|
|
|
|
|
|
25 |
"""
|
26 |
+
try:
|
27 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
28 |
+
if not api_key:
|
29 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
30 |
+
|
31 |
+
# Initialize OpenAI client
|
32 |
+
client = AsyncOpenAI(api_key=api_key)
|
33 |
+
# Test the API key with a simple request
|
34 |
+
await client.chat.completions.create(
|
35 |
+
model="gpt-3.5-turbo",
|
36 |
+
messages=[{"role": "system", "content": "Test"}],
|
37 |
+
max_tokens=5
|
38 |
+
)
|
39 |
+
|
40 |
+
cl.user_session.set("client", client)
|
41 |
+
|
42 |
+
# Initialize message history with system prompt
|
43 |
+
message_history = [{"role": "system", "content": SYSTEM_PROMPT}]
|
44 |
+
cl.user_session.set("message_history", message_history)
|
45 |
+
|
46 |
+
# Save model settings
|
47 |
+
cl.user_session.set("settings", DEFAULT_SETTINGS)
|
48 |
+
|
49 |
+
await cl.Message(
|
50 |
+
content="Hello! I'm your AI assistant powered by OpenAI. How can I help you today?"
|
51 |
+
).send()
|
52 |
+
|
53 |
+
except ValueError as e:
|
54 |
+
await cl.Message(
|
55 |
+
content=f"⚠️ Configuration Error: {str(e)}\nPlease make sure OPENAI_API_KEY is set in the environment variables."
|
56 |
+
).send()
|
57 |
+
except Exception as e:
|
58 |
+
await cl.Message(
|
59 |
+
content=f"⚠️ Error initializing chat: {str(e)}\nPlease check your API key and try again."
|
60 |
+
).send()
|
61 |
|
62 |
|
63 |
@cl.on_message
|