jarguello76 commited on
Commit
eeb9694
·
verified ·
1 Parent(s): 9ce73ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -132
app.py CHANGED
@@ -1,25 +1,21 @@
1
- import os
2
- import gradio as gr
3
- import requests
4
- import pandas as pd
5
- import re
6
-
7
- # --- Constants ---
8
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
-
10
- # --- LangChain Agent Definition ---
11
  from langchain_huggingface import HuggingFaceEndpoint
12
  from langchain.agents import initialize_agent, Tool
13
  from langchain.agents.agent_types import AgentType
14
  from langchain.tools import tool
15
  from duckduckgo_search import DDGS
 
16
 
17
  @tool
18
  def search_web(query: str) -> str:
19
  """Search the web and return a relevant text snippet."""
20
- with DDGS() as ddgs:
21
- results = list(ddgs.text(query, max_results=3))
22
- return results[0]["body"] if results else "No result"
 
 
 
 
 
23
 
24
  @tool
25
  def run_code_snippet(code: str) -> str:
@@ -30,15 +26,15 @@ def run_code_snippet(code: str) -> str:
30
  result = [v for v in local_env.values() if isinstance(v, (int, float, str))]
31
  return str(result[0]) if result else "0"
32
  except Exception as e:
33
- return f"Error: {str(e)}"
34
 
35
  class GAIAAgent:
36
  def __init__(self):
37
  print("Initializing LangChain agent with Hugging FaceEndpoint...")
38
  self.llm = HuggingFaceEndpoint(
39
- repo_id="google/flan-t5-xl", # You can replace this with another model
40
  temperature=0.3,
41
- # model_kwargs={"max_length": 256} # ✅ Fix: pass max_length via model_kwargs
42
  )
43
  self.tools = [search_web, run_code_snippet]
44
  self.agent = initialize_agent(
@@ -50,127 +46,22 @@ class GAIAAgent:
50
 
51
  def __call__(self, question: str) -> str:
52
  try:
 
53
  result = self.agent.invoke(question)
 
 
 
 
 
54
  return f"FINAL ANSWER: {self.clean_answer(result)}"
 
 
 
 
55
  except Exception as e:
 
56
  return f"FINAL ANSWER: Error: {e}"
57
 
58
  def clean_answer(self, ans: str) -> str:
59
  ans = re.sub(r"[\$,%]", "", ans)
60
  return ans.strip()
61
-
62
- # --- Evaluation & Submission Pipeline ---
63
- def run_and_submit_all(profile: gr.OAuthProfile | None):
64
- space_id = os.getenv("SPACE_ID")
65
-
66
- if profile:
67
- username = f"{profile.username}"
68
- print(f"User logged in: {username}")
69
- else:
70
- return "Please Login to Hugging Face with the button.", None
71
-
72
- api_url = DEFAULT_API_URL
73
- questions_url = f"{api_url}/questions"
74
- submit_url = f"{api_url}/submit"
75
-
76
- try:
77
- agent = GAIAAgent()
78
- except Exception as e:
79
- return f"Error initializing agent: {e}", None
80
-
81
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
82
- print(agent_code)
83
-
84
- try:
85
- response = requests.get(questions_url, timeout=15)
86
- response.raise_for_status()
87
- questions_data = response.json()
88
- if not questions_data:
89
- return "Fetched questions list is empty or invalid format.", None
90
- print(f"Fetched {len(questions_data)} questions.")
91
- except Exception as e:
92
- return f"Error fetching questions: {e}", None
93
-
94
- results_log = []
95
- answers_payload = []
96
- for item in questions_data:
97
- task_id = item.get("task_id")
98
- question_text = item.get("question")
99
- if not task_id or question_text is None:
100
- continue
101
- try:
102
- submitted_answer = agent(question_text)
103
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
104
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
105
- except Exception as e:
106
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
107
-
108
- if not answers_payload:
109
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
110
-
111
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
112
- try:
113
- response = requests.post(submit_url, json=submission_data, timeout=60)
114
- response.raise_for_status()
115
- result_data = response.json()
116
- final_status = (
117
- f"Submission Successful!\n"
118
- f"User: {result_data.get('username')}\n"
119
- f"Overall Score: {result_data.get('score', 'N/A')}% "
120
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
121
- f"Message: {result_data.get('message', 'No message received.')}"
122
- )
123
- return final_status, pd.DataFrame(results_log)
124
- except Exception as e:
125
- return f"Submission Failed: {e}", pd.DataFrame(results_log)
126
-
127
- # --- Gradio UI ---
128
- with gr.Blocks() as demo:
129
- gr.Markdown("# GAIA Agent Evaluation Runner")
130
-
131
- gr.Markdown("""
132
- **Instructions:**
133
- 1. Set your Hugging Face token in the Space Settings → Secrets as `HUGGINGFACEHUB_API_TOKEN`.
134
- 2. Log in to Hugging Face below to associate your username.
135
- 3. Run the agent to fetch, answer, and submit GAIA benchmark questions.
136
- """)
137
-
138
- gr.LoginButton()
139
-
140
- gr.Markdown("## 🔍 Try a Question Preview")
141
- preview_input = gr.Textbox(label="Your GAIA-style question")
142
- preview_output = gr.Textbox(label="Agent's Response", lines=2)
143
- preview_button = gr.Button("Preview Answer")
144
- preview_button.click(lambda q: GAIAAgent()(q), inputs=preview_input, outputs=preview_output)
145
-
146
- run_button = gr.Button("Run Evaluation & Submit All Answers")
147
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
148
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
149
-
150
- run_button.click(
151
- fn=run_and_submit_all,
152
- outputs=[status_output, results_table]
153
- )
154
-
155
- # --- Main Entry ---
156
- if __name__ == "__main__":
157
- print("\n" + "-"*30 + " App Starting " + "-"*30)
158
- space_host_startup = os.getenv("SPACE_HOST")
159
- space_id_startup = os.getenv("SPACE_ID")
160
-
161
- if space_host_startup:
162
- print(f"✅ SPACE_HOST found: {space_host_startup}")
163
- print(f" Runtime URL: https://{space_host_startup}.hf.space")
164
- else:
165
- print("ℹ️ SPACE_HOST not set.")
166
-
167
- if space_id_startup:
168
- print(f"✅ SPACE_ID found: {space_id_startup}")
169
- print(f" Repo: https://huggingface.co/spaces/{space_id_startup}")
170
- print(f" Code: https://huggingface.co/spaces/{space_id_startup}/tree/main")
171
- else:
172
- print("ℹ️ SPACE_ID not set.")
173
-
174
- print("-"*(60 + len(" App Starting ")) + "\n")
175
- print("Launching Gradio Interface for GAIA Agent Evaluation...")
176
- demo.launch(debug=True, share=False)
 
 
 
 
 
 
 
 
 
 
 
1
  from langchain_huggingface import HuggingFaceEndpoint
2
  from langchain.agents import initialize_agent, Tool
3
  from langchain.agents.agent_types import AgentType
4
  from langchain.tools import tool
5
  from duckduckgo_search import DDGS
6
+ import re
7
 
8
  @tool
9
  def search_web(query: str) -> str:
10
  """Search the web and return a relevant text snippet."""
11
+ try:
12
+ with DDGS() as ddgs:
13
+ results = list(ddgs.text(query, max_results=3))
14
+ if not results or not results[0].get("body"):
15
+ return "No result found"
16
+ return results[0]["body"]
17
+ except Exception as e:
18
+ return f"Web search failed: {e}"
19
 
20
  @tool
21
  def run_code_snippet(code: str) -> str:
 
26
  result = [v for v in local_env.values() if isinstance(v, (int, float, str))]
27
  return str(result[0]) if result else "0"
28
  except Exception as e:
29
+ return f"Error in code: {str(e)}"
30
 
31
  class GAIAAgent:
32
  def __init__(self):
33
  print("Initializing LangChain agent with Hugging FaceEndpoint...")
34
  self.llm = HuggingFaceEndpoint(
35
+ repo_id="google/flan-t5-xl",
36
  temperature=0.3,
37
+ model_kwargs={"max_length": 256} # ✅ fix: max_length passed safely
38
  )
39
  self.tools = [search_web, run_code_snippet]
40
  self.agent = initialize_agent(
 
46
 
47
  def __call__(self, question: str) -> str:
48
  try:
49
+ print(f"[Agent] Invoking on question: {question}")
50
  result = self.agent.invoke(question)
51
+ print(f"[Agent] Raw result: {result}")
52
+
53
+ if not result or not isinstance(result, str):
54
+ raise StopIteration("Model returned empty or non-string result.")
55
+
56
  return f"FINAL ANSWER: {self.clean_answer(result)}"
57
+
58
+ except StopIteration as stop_err:
59
+ print(f"[Agent] StopIteration: {stop_err}")
60
+ return "FINAL ANSWER: Error: model or tool returned no result"
61
  except Exception as e:
62
+ print(f"[Agent] Exception: {e}")
63
  return f"FINAL ANSWER: Error: {e}"
64
 
65
  def clean_answer(self, ans: str) -> str:
66
  ans = re.sub(r"[\$,%]", "", ans)
67
  return ans.strip()