Karim0111 commited on
Commit
eebecd0
·
verified ·
1 Parent(s): eab6fbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -44
app.py CHANGED
@@ -13,12 +13,11 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  class EnhancedGAIAAgentFree:
14
  """
15
  GAIA Agent for free HuggingFace models.
16
- Avoids PRO credits and multi-modal content.
17
  """
18
 
19
  def __init__(self):
20
  print("🚀 GAIAAgent initializing... (FREE version)")
21
-
22
  hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
23
  if not hf_token:
24
  print("⚠️ HF_TOKEN not found! Add it to Space secrets.")
@@ -27,64 +26,48 @@ class EnhancedGAIAAgentFree:
27
  return
28
 
29
  self.client = InferenceClient(token=hf_token)
30
- self.model = "TheBloke/guanaco-7B-GPTQ" # free HF model
31
  print(f"✅ Model loaded: {self.model}")
32
 
33
  def __call__(self, question: str) -> str:
34
  """
35
- Answer a question with free LLM.
36
  """
37
- print(f"\nQ: {question[:150]}...")
38
- if not self.client or not self.model:
39
- return "ERROR: HF_TOKEN not configured"
40
-
41
- # Skip unsupported questions
42
- if any(word in question.lower() for word in ["image", "video", "file", "attached", "excel", "code"]):
43
- return "unknown"
44
-
45
  try:
46
- answer = self._generate_answer(question)
47
- print(f"A: {answer[:150]}...")
48
- return answer
49
- except Exception as e:
50
- print(f"❌ Agent error: {e}")
51
- return "unknown"
52
 
53
- def _generate_answer(self, question: str) -> str:
54
- """
55
- Free LLM answer generator with safe prompt.
56
- """
57
- prompt = f"""
 
58
  You are an expert for GAIA benchmark.
59
  Answer concisely. ONLY provide the final answer.
60
  If you cannot determine the answer, write "unknown".
61
  Question: {question}
62
  FINAL ANSWER:"""
63
 
64
- response = self.client.text_generation(
65
- model=self.model,
66
- prompt=prompt,
67
- max_new_tokens=128,
68
- temperature=0.1,
69
- do_sample=False,
70
- return_full_text=False
71
- )
72
- return self._clean_answer(response)
 
 
 
73
 
74
- def _clean_answer(self, text: str) -> str:
75
- """
76
- Keep only the final answer text.
77
- """
78
- if not text:
79
  return "unknown"
80
 
81
- # Remove prefixes
82
- prefixes = ["Answer:", "The answer is", "A:", "FINAL ANSWER:", "Result:"]
83
- for p in prefixes:
84
- if text.lower().startswith(p.lower()):
85
- text = text[len(p):].strip()
86
- return text.strip() if text.strip() else "unknown"
87
-
88
 
89
  def run_and_submit_all(profile: gr.OAuthProfile | None):
90
  """
 
13
  class EnhancedGAIAAgentFree:
14
  """
15
  GAIA Agent for free HuggingFace models.
16
+ Returns 'unknown' for images, videos, code, Excel, or unsupported questions.
17
  """
18
 
19
  def __init__(self):
20
  print("🚀 GAIAAgent initializing... (FREE version)")
 
21
  hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
22
  if not hf_token:
23
  print("⚠️ HF_TOKEN not found! Add it to Space secrets.")
 
26
  return
27
 
28
  self.client = InferenceClient(token=hf_token)
29
+ self.model = "TheBloke/guanaco-7B-GPTQ" # free model
30
  print(f"✅ Model loaded: {self.model}")
31
 
32
  def __call__(self, question: str) -> str:
33
  """
34
+ Answer a question or return 'unknown' for unsupported content.
35
  """
 
 
 
 
 
 
 
 
36
  try:
37
+ # Filter unsupported content
38
+ unsupported = ["image", "video", "file", "attached", "excel", "python code", "chart", "csv"]
39
+ if any(word in question.lower() for word in unsupported):
40
+ return "unknown"
 
 
41
 
42
+ # Very short or empty questions
43
+ if not question.strip() or len(question.strip()) < 5:
44
+ return "unknown"
45
+
46
+ # Free text generation for supported questions
47
+ prompt = f"""
48
  You are an expert for GAIA benchmark.
49
  Answer concisely. ONLY provide the final answer.
50
  If you cannot determine the answer, write "unknown".
51
  Question: {question}
52
  FINAL ANSWER:"""
53
 
54
+ response = self.client.text_generation(
55
+ model=self.model,
56
+ prompt=prompt,
57
+ max_new_tokens=128,
58
+ temperature=0.1,
59
+ do_sample=False,
60
+ return_full_text=False
61
+ )
62
+
63
+ # Clean response
64
+ answer = response.strip() if response else "unknown"
65
+ return answer
66
 
67
+ except Exception as e:
68
+ print(f"❌ Agent error: {e}")
 
 
 
69
  return "unknown"
70
 
 
 
 
 
 
 
 
71
 
72
  def run_and_submit_all(profile: gr.OAuthProfile | None):
73
  """