vissutagunawan commited on
Commit
638d347
·
verified ·
1 Parent(s): 9a7d309

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -5
app.py CHANGED
@@ -4,6 +4,10 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
 
 
 
 
7
  # smolagents imports
8
  from smolagents import CodeAgent, InferenceClientModel, tool
9
  import re
@@ -230,21 +234,65 @@ def count_items(text: str, item_type: str = "words") -> str:
230
  except Exception as e:
231
  return f"Error counting items: {str(e)}"
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  # --- Enhanced Agent Definition ---
234
  class GAIAAgent:
235
  def __init__(self):
236
  print("GAIAAgent initializing with smolagents...")
237
 
238
- # Initialize the model (using HuggingFace Inference Client)
239
  try:
240
- # Use a powerful model for better reasoning on GAIA benchmark
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  model_id = "meta-llama/Llama-3.3-70B-Instruct"
242
  self.model = InferenceClientModel(model_id=model_id)
243
  print(f"✅ Model initialized successfully: {model_id}")
244
  except Exception as e:
245
- print(f" Error initializing model: {e}")
246
- # Fallback to default model
247
- self.model = InferenceClientModel()
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
  # Initialize tools (custom tools + base tools from smolagents)
250
  self.custom_tools = [
@@ -534,6 +582,10 @@ if __name__ == "__main__":
534
  print("🤖 ENHANCED GAIA AGENT STARTING UP")
535
  print("="*60)
536
 
 
 
 
 
537
  # Check for SPACE_HOST and SPACE_ID at startup for information
538
  space_host_startup = os.getenv("SPACE_HOST")
539
  space_id_startup = os.getenv("SPACE_ID")
@@ -543,6 +595,10 @@ if __name__ == "__main__":
543
  print(f" 🌐 Runtime URL: https://{space_host_startup}.hf.space")
544
  else:
545
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
 
 
 
 
546
 
547
  if space_id_startup:
548
  print(f"✅ SPACE_ID found: {space_id_startup}")
 
4
  import inspect
5
  import pandas as pd
6
 
7
+ # HuggingFace authentication
8
+ from huggingface_hub import login
9
+ import warnings
10
+
11
  # smolagents imports
12
  from smolagents import CodeAgent, InferenceClientModel, tool
13
  import re
 
234
  except Exception as e:
235
  return f"Error counting items: {str(e)}"
236
 
237
+ def setup_authentication():
238
+ """Setup HuggingFace authentication for the app."""
239
+ try:
240
+ # Try to get HF token from environment variables
241
+ hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
242
+
243
+ if hf_token:
244
+ login(token=hf_token)
245
+ print("✅ Authenticated with HuggingFace using environment token")
246
+ return True
247
+ else:
248
+ print("ℹ️ No HF token found in environment")
249
+ print("💡 If running locally, please set HF_TOKEN environment variable")
250
+ print("💡 For Spaces deployment, this should work automatically")
251
+ return False
252
+ except Exception as e:
253
+ print(f"⚠️ Authentication issue: {e}")
254
+ return False
255
+
256
  # --- Enhanced Agent Definition ---
257
  class GAIAAgent:
258
  def __init__(self):
259
  print("GAIAAgent initializing with smolagents...")
260
 
261
+ # Handle HuggingFace authentication
262
  try:
263
+ # Try to get HF token from environment (for Spaces)
264
+ hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
265
+ if hf_token:
266
+ login(token=hf_token)
267
+ print("✅ Authenticated with HuggingFace using environment token")
268
+ else:
269
+ # In Spaces, authentication might already be handled
270
+ print("ℹ️ No HF token found in environment, proceeding without explicit login")
271
+ except Exception as e:
272
+ print(f"⚠️ Authentication warning: {e}")
273
+
274
+ # Initialize the model with fallback options
275
+ try:
276
+ # Try powerful model first
277
  model_id = "meta-llama/Llama-3.3-70B-Instruct"
278
  self.model = InferenceClientModel(model_id=model_id)
279
  print(f"✅ Model initialized successfully: {model_id}")
280
  except Exception as e:
281
+ print(f"⚠️ Error with primary model: {e}")
282
+ try:
283
+ # Fallback to a different model
284
+ fallback_model = "microsoft/DialoGPT-medium"
285
+ self.model = InferenceClientModel(model_id=fallback_model)
286
+ print(f"✅ Fallback model initialized: {fallback_model}")
287
+ except Exception as e2:
288
+ print(f"⚠️ Error with fallback model: {e2}")
289
+ try:
290
+ # Last resort - use default
291
+ self.model = InferenceClientModel()
292
+ print("✅ Default model initialized")
293
+ except Exception as e3:
294
+ print(f"❌ Critical error - could not initialize any model: {e3}")
295
+ raise e3
296
 
297
  # Initialize tools (custom tools + base tools from smolagents)
298
  self.custom_tools = [
 
582
  print("🤖 ENHANCED GAIA AGENT STARTING UP")
583
  print("="*60)
584
 
585
+ # Setup authentication
586
+ print("🔐 Setting up HuggingFace authentication...")
587
+ auth_success = setup_authentication()
588
+
589
  # Check for SPACE_HOST and SPACE_ID at startup for information
590
  space_host_startup = os.getenv("SPACE_HOST")
591
  space_id_startup = os.getenv("SPACE_ID")
 
595
  print(f" 🌐 Runtime URL: https://{space_host_startup}.hf.space")
596
  else:
597
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
598
+ if not auth_success:
599
+ print("💡 For local testing, you may need to run:")
600
+ print(" from huggingface_hub import notebook_login")
601
+ print(" notebook_login()")
602
 
603
  if space_id_startup:
604
  print(f"✅ SPACE_ID found: {space_id_startup}")