VACInc commited on
Commit
48e649f
·
1 Parent(s): 38ccb28
Files changed (2) hide show
  1. app.py +80 -26
  2. requirements.txt +2 -2
app.py CHANGED
@@ -29,7 +29,16 @@ from smolagents import (
29
  )
30
 
31
  # Set your HuggingFace token here
32
- HF_TOKEN = os.getenv("HF_TOKEN", "your_hf_token_here")
 
 
 
 
 
 
 
 
 
33
 
34
  @dataclass
35
  class Task:
@@ -242,11 +251,19 @@ class PersonalProductivityAgent:
242
  """Main productivity agent class"""
243
 
244
  def __init__(self):
245
- # Initialize the model
246
- self.model = InferenceClientModel(
247
- model="Qwen/Qwen2.5-Coder-32B-Instruct",
248
- token=HF_TOKEN
249
- )
 
 
 
 
 
 
 
 
250
 
251
  # Initialize tools
252
  self.search_tool = DuckDuckGoSearchTool()
@@ -263,7 +280,24 @@ class PersonalProductivityAgent:
263
  create_meeting_summary
264
  ]
265
 
266
- # Initialize the CodeAgent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  self.agent = CodeAgent(
268
  tools=[self.search_tool] + self.custom_tools,
269
  model=self.model,
@@ -271,37 +305,45 @@ class PersonalProductivityAgent:
271
  planning_interval=3 # Plan every 3 steps
272
  )
273
 
274
- # System prompt customization
275
- self.agent.system_prompt = """You are an advanced Personal Productivity Assistant named Alfred.
276
-
277
- You help users manage their daily tasks, schedule, communications, and information needs.
278
 
279
- Key capabilities:
280
- - Task management (add, list, complete tasks)
281
- - Time and date calculations
282
- - Weather information
283
- - Web search and research
284
- - Email composition (mock)
285
- - Meeting planning and templates
286
- - General calculations and code execution
287
-
288
- Always be helpful, concise, and proactive in suggesting productivity improvements.
289
- When users ask for help, offer multiple options and explain your reasoning.
290
- """
291
 
292
  def run(self, query: str, reset_memory: bool = False) -> str:
293
  """Run the agent with a user query"""
 
 
 
294
  try:
295
  response = self.agent.run(query, reset=reset_memory)
296
  return str(response)
297
  except Exception as e:
298
- return f"I apologize, but I encountered an error: {str(e)}"
 
 
 
 
 
 
299
 
300
- # Initialize the global agent
301
- productivity_agent = PersonalProductivityAgent()
 
 
 
 
302
 
303
  def chat_interface(message, history, reset_conversation):
304
  """Gradio chat interface function"""
 
 
 
 
 
305
  if reset_conversation:
306
  # Reset the conversation memory
307
  response = productivity_agent.run(message, reset_memory=True)
@@ -408,8 +450,20 @@ def create_gradio_interface():
408
  return demo
409
 
410
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
411
  # Create and launch the interface
412
  demo = create_gradio_interface()
 
 
 
413
  demo.launch(
414
  share=True,
415
  debug=True,
 
29
  )
30
 
31
  # Set your HuggingFace token here
32
+ # In HuggingFace Spaces, this will be automatically loaded from secrets
33
+ HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN")
34
+
35
+ # Validate HF token
36
+ if not HF_TOKEN or HF_TOKEN == "your_hf_token_here":
37
+ print("⚠️ WARNING: HF_TOKEN not set!")
38
+ print("Set it with: export HF_TOKEN=your_actual_token")
39
+ print("Get token from: https://huggingface.co/settings/tokens")
40
+ print("Continuing with limited functionality...")
41
+ HF_TOKEN = None
42
 
43
  @dataclass
44
  class Task:
 
251
  """Main productivity agent class"""
252
 
253
  def __init__(self):
254
+ # Initialize the model with fallback
255
+ try:
256
+ self.model = InferenceClientModel(
257
+ model="Qwen/Qwen2.5-Coder-32B-Instruct",
258
+ token=HF_TOKEN
259
+ )
260
+ except Exception as e:
261
+ print(f"⚠️ Primary model failed, using fallback: {e}")
262
+ # Fallback to a smaller, more reliable model
263
+ self.model = InferenceClientModel(
264
+ model="microsoft/DialoGPT-medium",
265
+ token=HF_TOKEN
266
+ )
267
 
268
  # Initialize tools
269
  self.search_tool = DuckDuckGoSearchTool()
 
280
  create_meeting_summary
281
  ]
282
 
283
+ # Custom system prompt
284
+ custom_system_prompt = """You are an advanced Personal Productivity Assistant named Alfred.
285
+
286
+ You help users manage their daily tasks, schedule, communications, and information needs.
287
+
288
+ Key capabilities:
289
+ - Task management (add, list, complete tasks)
290
+ - Time and date calculations
291
+ - Weather information
292
+ - Web search and research
293
+ - Email composition (mock)
294
+ - Meeting planning and templates
295
+ - General calculations and code execution
296
+
297
+ Always be helpful, concise, and proactive in suggesting productivity improvements.
298
+ When users ask for help, offer multiple options and explain your reasoning."""
299
+
300
+ # Initialize the CodeAgent with custom prompt
301
  self.agent = CodeAgent(
302
  tools=[self.search_tool] + self.custom_tools,
303
  model=self.model,
 
305
  planning_interval=3 # Plan every 3 steps
306
  )
307
 
308
+ # Set system prompt using prompt_templates (proper way)
309
+ if hasattr(self.agent, 'prompt_templates'):
310
+ self.agent.prompt_templates['system_prompt'] = custom_system_prompt
 
311
 
312
+ print("✅ Personal Productivity Agent initialized successfully!")
313
+ print(f"🤖 Model: {self.model.model if hasattr(self.model, 'model') else 'Unknown'}")
314
+ print(f"🔧 Tools available: {len(self.custom_tools) + 1} custom + base tools")
 
 
 
 
 
 
 
 
 
315
 
316
  def run(self, query: str, reset_memory: bool = False) -> str:
317
  """Run the agent with a user query"""
318
+ if not HF_TOKEN:
319
+ return "❌ Cannot run agent: HuggingFace token not set. Please set HF_TOKEN environment variable."
320
+
321
  try:
322
  response = self.agent.run(query, reset=reset_memory)
323
  return str(response)
324
  except Exception as e:
325
+ error_msg = str(e)
326
+ if "401" in error_msg or "authentication" in error_msg.lower():
327
+ return "❌ Authentication error: Please check your HuggingFace token permissions."
328
+ elif "timeout" in error_msg.lower():
329
+ return "⏱️ Request timed out. The model might be busy. Please try again."
330
+ else:
331
+ return f"❌ I encountered an error: {error_msg}"
332
 
333
+ # Initialize the global agent with error handling
334
+ try:
335
+ productivity_agent = PersonalProductivityAgent()
336
+ except Exception as e:
337
+ print(f"❌ Failed to initialize agent: {e}")
338
+ productivity_agent = None
339
 
340
  def chat_interface(message, history, reset_conversation):
341
  """Gradio chat interface function"""
342
+ if not productivity_agent:
343
+ response = "❌ Agent not initialized. Please check your HuggingFace token and restart the application."
344
+ history.append([message, response])
345
+ return history, ""
346
+
347
  if reset_conversation:
348
  # Reset the conversation memory
349
  response = productivity_agent.run(message, reset_memory=True)
 
450
  return demo
451
 
452
  if __name__ == "__main__":
453
+ # Startup validation
454
+ if not productivity_agent:
455
+ print("\n❌ STARTUP FAILED")
456
+ print("Possible solutions:")
457
+ print("1. Set HuggingFace token: export HF_TOKEN=your_token")
458
+ print("2. Install dependencies: pip install smolagents gradio huggingface_hub")
459
+ print("3. Check internet connection")
460
+ exit(1)
461
+
462
  # Create and launch the interface
463
  demo = create_gradio_interface()
464
+ print("\n🚀 Launching Gradio interface...")
465
+ print("📍 Local URL will be displayed below")
466
+
467
  demo.launch(
468
  share=True,
469
  debug=True,
requirements.txt CHANGED
@@ -1,8 +1,8 @@
1
  smolagents>=0.4.0
2
  gradio>=4.0.0
3
- huggingface_hub>=0.19.0
4
  transformers>=4.35.0
5
  torch>=2.0.0
6
  requests>=2.31.0
7
  python-dateutil>=2.8.2
8
- duckduckgo_search
 
1
  smolagents>=0.4.0
2
  gradio>=4.0.0
3
+ huggingface_hub>=0.20.0
4
  transformers>=4.35.0
5
  torch>=2.0.0
6
  requests>=2.31.0
7
  python-dateutil>=2.8.2
8
+ duckduckgo_search