leofltt commited on
Commit
cc70c39
·
1 Parent(s): ba6c035

now should be working

Browse files
Files changed (2) hide show
  1. app.py +53 -15
  2. requirements.txt +25 -13
app.py CHANGED
@@ -10,7 +10,7 @@ import numexpr
10
  from typing import TypedDict, Annotated
11
 
12
  # --- Langchain & HF Imports ---
13
- from langchain_community.llms import HuggingFaceHub
14
  from langchain_community.tools import DuckDuckGoSearchRun
15
  from langchain_core.prompts import PromptTemplate
16
  from langchain_core.output_parsers import StrOutputParser
@@ -118,13 +118,20 @@ class GaiaAgent:
118
  image_analyzer,
119
  youtube_transcript_reader,
120
  ]
121
- logging.info("Initializing LLM via HuggingFaceHub...")
122
- llm = HuggingFaceHub(
123
- repo_id="mistralai/Mistral-7B-Instruct-v0.2", # Using a well-supported model
124
- model_kwargs={"temperature": 0.1, "max_new_tokens": 1024},
125
- huggingfacehub_api_token=os.getenv("HUGGING_FACE_HUB_TOKEN"),
 
 
 
 
126
  )
 
127
  logging.info("LLM initialized successfully.")
 
 
128
  prompt = PromptTemplate(
129
  template=SYSTEM_PROMPT
130
  + "\nHere is the current conversation:\n{messages}\n\nQuestion: {question}",
@@ -219,23 +226,37 @@ class GaiaAgent:
219
  return f"Error during agent invocation: {e}"
220
 
221
 
 
 
 
 
 
222
  def run_and_submit_all(profile: gr.OAuthProfile | None):
223
  if not profile:
224
  return "Please Login to Hugging Face with the button.", None
225
  username = profile.username
226
  logging.info(f"User logged in: {username}")
 
227
  space_id = os.getenv("SPACE_ID")
 
 
 
 
228
  if not space_id:
229
  return "CRITICAL ERROR: SPACE_ID environment variable is not set.", None
 
230
  api_url = DEFAULT_API_URL
231
  questions_url = f"{api_url}/questions"
232
  submit_url = f"{api_url}/submit"
 
233
  try:
234
  agent = GaiaAgent()
235
  except Exception as e:
236
  logging.critical(f"Fatal error instantiating agent: {e}", exc_info=True)
237
  return f"Fatal error initializing agent: {e}", None
 
238
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
239
  logging.info(f"Fetching questions from: {questions_url}")
240
  try:
241
  response = requests.get(questions_url, timeout=20)
@@ -243,18 +264,26 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
243
  questions_data = response.json()
244
  if not questions_data:
245
  return "Fetched questions list is empty.", None
246
- logging.info(f"Fetched {len(questions_data)} questions.")
247
  except Exception as e:
248
  return f"Error fetching questions: {e}", None
 
 
 
 
 
 
 
 
 
249
  results_log = []
250
  answers_payload = []
251
- logging.info(f"Running agent on {len(questions_data)} questions...")
252
- for i, item in enumerate(questions_data):
 
253
  task_id = item.get("task_id")
254
  question_text = item.get("question")
255
- logging.info(
256
- f"--- Processing question {i+1}/{len(questions_data)} (Task ID: {task_id}) ---"
257
- )
258
  if not task_id or question_text is None:
259
  continue
260
  try:
@@ -278,20 +307,29 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
278
  "Submitted Answer": f"AGENT ERROR: {e}",
279
  }
280
  )
 
 
 
 
 
281
  if not answers_payload:
282
- return "Agent did not produce any answers.", pd.DataFrame(results_log)
 
 
 
283
  submission_data = {
284
  "username": username.strip(),
285
  "agent_code": agent_code,
286
  "answers": answers_payload,
287
  }
288
- logging.info(f"Submitting {len(answers_payload)} answers for user '{username}'...")
 
289
  try:
290
  response = requests.post(submit_url, json=submission_data, timeout=60)
291
  response.raise_for_status()
292
  result_data = response.json()
293
  final_status = (
294
- f"Submission Successful!\n"
295
  f"User: {result_data.get('username')}\n"
296
  f"Overall Score: {result_data.get('score', 'N/A')}% "
297
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
 
10
  from typing import TypedDict, Annotated
11
 
12
  # --- Langchain & HF Imports ---
13
+ from langchain_huggingface import HuggingFaceEndpoint
14
  from langchain_community.tools import DuckDuckGoSearchRun
15
  from langchain_core.prompts import PromptTemplate
16
  from langchain_core.output_parsers import StrOutputParser
 
118
  image_analyzer,
119
  youtube_transcript_reader,
120
  ]
121
+
122
+ # --- THIS IS THE CORRECTED LLM INITIALIZATION ---
123
+ logging.info("Initializing LLM via modern HuggingFaceEndpoint...")
124
+
125
+ llm = HuggingFaceEndpoint(
126
+ repo_id="HuggingFaceH4/zephyr-7b-beta",
127
+ temperature=0.1,
128
+ max_new_tokens=1024,
129
+ huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN"),
130
  )
131
+
132
  logging.info("LLM initialized successfully.")
133
+
134
+ # The rest of the class remains the same
135
  prompt = PromptTemplate(
136
  template=SYSTEM_PROMPT
137
  + "\nHere is the current conversation:\n{messages}\n\nQuestion: {question}",
 
226
  return f"Error during agent invocation: {e}"
227
 
228
 
229
+ # In app.py
230
+
231
+ # ... (keep all the code above this function)
232
+
233
+
234
  def run_and_submit_all(profile: gr.OAuthProfile | None):
235
  if not profile:
236
  return "Please Login to Hugging Face with the button.", None
237
  username = profile.username
238
  logging.info(f"User logged in: {username}")
239
+
240
  space_id = os.getenv("SPACE_ID")
241
+ if not space_id:
242
+ space_id = "leofltt/HF_Agents_Final_Assignment"
243
+ logging.warning(f"SPACE_ID not found, using fallback for local run: {space_id}")
244
+
245
  if not space_id:
246
  return "CRITICAL ERROR: SPACE_ID environment variable is not set.", None
247
+
248
  api_url = DEFAULT_API_URL
249
  questions_url = f"{api_url}/questions"
250
  submit_url = f"{api_url}/submit"
251
+
252
  try:
253
  agent = GaiaAgent()
254
  except Exception as e:
255
  logging.critical(f"Fatal error instantiating agent: {e}", exc_info=True)
256
  return f"Fatal error initializing agent: {e}", None
257
+
258
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
259
+
260
  logging.info(f"Fetching questions from: {questions_url}")
261
  try:
262
  response = requests.get(questions_url, timeout=20)
 
264
  questions_data = response.json()
265
  if not questions_data:
266
  return "Fetched questions list is empty.", None
267
+ logging.info(f"Successfully fetched {len(questions_data)} questions.")
268
  except Exception as e:
269
  return f"Error fetching questions: {e}", None
270
+
271
+ # --- MODIFICATION FOR DEBUGGING ---
272
+ # We will only process the first question from the list.
273
+ questions_to_process = [questions_data[0]]
274
+ logging.info(
275
+ f"DEBUG MODE: Processing only the first question out of {len(questions_data)}."
276
+ )
277
+ # --- END OF MODIFICATION ---
278
+
279
  results_log = []
280
  answers_payload = []
281
+
282
+ # The loop now runs only once.
283
+ for i, item in enumerate(questions_to_process):
284
  task_id = item.get("task_id")
285
  question_text = item.get("question")
286
+ logging.info(f"--- Processing question (Task ID: {task_id}) ---")
 
 
287
  if not task_id or question_text is None:
288
  continue
289
  try:
 
307
  "Submitted Answer": f"AGENT ERROR: {e}",
308
  }
309
  )
310
+ # Also return the error in the status for immediate feedback
311
+ return f"Agent failed on the first question with error: {e}", pd.DataFrame(
312
+ results_log
313
+ )
314
+
315
  if not answers_payload:
316
+ return "Agent did not produce an answer for the first question.", pd.DataFrame(
317
+ results_log
318
+ )
319
+
320
  submission_data = {
321
  "username": username.strip(),
322
  "agent_code": agent_code,
323
  "answers": answers_payload,
324
  }
325
+ logging.info(f"Submitting {len(answers_payload)} answer for user '{username}'...")
326
+
327
  try:
328
  response = requests.post(submit_url, json=submission_data, timeout=60)
329
  response.raise_for_status()
330
  result_data = response.json()
331
  final_status = (
332
+ f"Submission Successful (for one question)!\n"
333
  f"User: {result_data.get('username')}\n"
334
  f"Overall Score: {result_data.get('score', 'N/A')}% "
335
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
requirements.txt CHANGED
@@ -1,16 +1,28 @@
1
- gradio==5.25.2
 
 
 
 
 
 
 
2
  langchain
3
- langgraph
4
- langchain-huggingface
5
  langchain-community
6
- duckduckgo-search
7
- numexpr
8
- transformers
9
- --extra-index-url https://download.pytorch.org/whl/cpu
10
- torch
11
- torchvision
12
- torchaudio
13
- pytube
14
- sentencepiece
15
- accelerate
 
 
 
 
 
 
16
  gradio[oauth]
 
1
+ # --- Core Web & Gradio Framework (Known Stable) ---
2
+ gradio==4.36.1
3
+ fastapi==0.110.0
4
+ pydantic==2.7.4
5
+ uvicorn==0.29.0
6
+ aiohttp==3.9.5 # Dependency of Gradio
7
+
8
+ # --- Core LangChain Agent & LLM Libs ---
9
  langchain
10
+ langchain-core==0.2.22
 
11
  langchain-community
12
+ langchain-huggingface==0.0.3
13
+ huggingface-hub==0.24.1
14
+ langgraph==0.0.69 # CORRECTED VERSION
15
+ transformers==4.42.4
16
+
17
+ # --- Tools and Data Handling ---
18
+ torch==2.3.1
19
+ torchvision==0.18.1
20
+ torchaudio==2.3.1
21
+ duckduckgo-search==6.1.8
22
+ numexpr==2.10.1
23
+ pandas==2.2.2
24
+ requests==2.32.3
25
+ pytube==15.0.0
26
+ sentencepiece==0.2.0
27
+ accelerate==0.32.1
28
  gradio[oauth]