dmfelder commited on
Commit
2221f89
Β·
verified Β·
1 Parent(s): 02e4f99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -7
app.py CHANGED
@@ -121,14 +121,31 @@ class SimpleAgent:
121
  raise ValueError("Agent not initialized. Call setup() first.")
122
 
123
  try:
124
- # For now, we'll use a simple approach with the inference client
125
- # In a real implementation, you'd want to use proper agent tools
126
 
127
  # Create a prompt that encourages the model to give a direct answer
128
- prompt = f"""Question: {question}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  Question Type: {question_type}
130
 
131
- Please provide a direct answer to this question. End your response with "Final Answer: [your answer]"
132
 
133
  Response:"""
134
 
@@ -292,6 +309,7 @@ def run_and_submit_all(request: gr.Request):
292
  question_text = item.get("question", "")
293
  question_type = item.get("question_type", "")
294
  question_preview = item.get("question_preview", question_text[:50] + "...")
 
295
  tools_code = item.get("tools_code") # This might be used in advanced implementations
296
 
297
  if not task_id:
@@ -311,8 +329,11 @@ def run_and_submit_all(request: gr.Request):
311
  status = "ERROR"
312
 
313
  try:
 
 
 
314
  # Call the agent
315
- submitted_answer = agent(question_text, question_type, tools_code)
316
  processing_time = time.time() - start_time
317
  status = "OK" if submitted_answer != "ERROR" else "ERROR"
318
 
@@ -330,9 +351,13 @@ def run_and_submit_all(request: gr.Request):
330
  "Question Preview": question_preview[:80] + "..." if len(question_preview) > 80 else question_preview,
331
  "Submitted Answer": submitted_answer[:100] + "..." if len(submitted_answer) > 100 else submitted_answer,
332
  "Processing Time (s)": f"{processing_time:.2f}",
333
- "Status": status
 
334
  })
335
 
 
 
 
336
  except Exception as e:
337
  processing_time = time.time() - start_time
338
  logging.exception(f"Error running agent on task {task_id}: {e}")
@@ -350,13 +375,15 @@ def run_and_submit_all(request: gr.Request):
350
  "Question Preview": question_preview[:80] + "..." if len(question_preview) > 80 else question_preview,
351
  "Submitted Answer": submitted_answer[:100] + "..." if len(submitted_answer) > 100 else submitted_answer,
352
  "Processing Time (s)": "ERROR",
353
- "Status": status
 
354
  })
355
 
356
  # Yield progressive updates to Gradio UI with better formatting
357
  progress_text = (
358
  f"πŸ”„ Processing Question {i}/{len(questions_data)}\n"
359
  f"πŸ“ Question Type: {question_type}\n"
 
360
  f"❓ Question Preview: {question_preview[:100]}{'...' if len(question_preview) > 100 else ''}\n"
361
  f"πŸ’­ Agent Answer: {submitted_answer[:150]}{'...' if len(submitted_answer) > 150 else ''}\n"
362
  f"⏱️ Processing Time: {processing_time:.2f}s\n"
@@ -434,6 +461,18 @@ def run_and_submit_all(request: gr.Request):
434
  agent.cleanup()
435
 
436
 
 
 
 
 
 
 
 
 
 
 
 
 
437
  # --- Gradio Interface ---
438
  with gr.Blocks() as demo:
439
  gr.Markdown("# Hugging Face Agent Certification Runner")
 
121
  raise ValueError("Agent not initialized. Call setup() first.")
122
 
123
  try:
124
+ # Check if the question mentions an attached file
125
+ has_attachment = "Attached file available at:" in question
126
 
127
  # Create a prompt that encourages the model to give a direct answer
128
+ if has_attachment:
129
+ prompt = f"""You are an AI assistant that can analyze questions and attached files.
130
+
131
+ Question: {question}
132
+ Question Type: {question_type}
133
+
134
+ For questions with attached files, you should:
135
+ 1. First acknowledge the attached file
136
+ 2. If needed, mention that you would analyze the file content
137
+ 3. Provide the best answer you can based on the question
138
+
139
+ Please provide a direct, factual answer. End your response with "Final Answer: [your answer]"
140
+
141
+ Response:"""
142
+ else:
143
+ prompt = f"""You are an AI assistant that answers questions directly and accurately.
144
+
145
+ Question: {question}
146
  Question Type: {question_type}
147
 
148
+ Please provide a direct, factual answer to this question. Think step by step if needed, but focus on giving a clear, concise answer. End your response with "Final Answer: [your answer]"
149
 
150
  Response:"""
151
 
 
309
  question_text = item.get("question", "")
310
  question_type = item.get("question_type", "")
311
  question_preview = item.get("question_preview", question_text[:50] + "...")
312
+ file_name = item.get("file_name") # Check for attached files
313
  tools_code = item.get("tools_code") # This might be used in advanced implementations
314
 
315
  if not task_id:
 
329
  status = "ERROR"
330
 
331
  try:
332
+ # Prepare question with file attachment if present
333
+ question_with_attachment = compute_question_with_attachment(question_text, task_id, file_name, api_url)
334
+
335
  # Call the agent
336
+ submitted_answer = agent(question_with_attachment, question_type, tools_code)
337
  processing_time = time.time() - start_time
338
  status = "OK" if submitted_answer != "ERROR" else "ERROR"
339
 
 
351
  "Question Preview": question_preview[:80] + "..." if len(question_preview) > 80 else question_preview,
352
  "Submitted Answer": submitted_answer[:100] + "..." if len(submitted_answer) > 100 else submitted_answer,
353
  "Processing Time (s)": f"{processing_time:.2f}",
354
+ "Status": status,
355
+ "Has File": "Yes" if file_name else "No"
356
  })
357
 
358
+ # Add rate limiting delay to prevent API errors
359
+ time.sleep(2) # Start with a smaller delay, increase if needed
360
+
361
  except Exception as e:
362
  processing_time = time.time() - start_time
363
  logging.exception(f"Error running agent on task {task_id}: {e}")
 
375
  "Question Preview": question_preview[:80] + "..." if len(question_preview) > 80 else question_preview,
376
  "Submitted Answer": submitted_answer[:100] + "..." if len(submitted_answer) > 100 else submitted_answer,
377
  "Processing Time (s)": "ERROR",
378
+ "Status": status,
379
+ "Has File": "Yes" if file_name else "No"
380
  })
381
 
382
  # Yield progressive updates to Gradio UI with better formatting
383
  progress_text = (
384
  f"πŸ”„ Processing Question {i}/{len(questions_data)}\n"
385
  f"πŸ“ Question Type: {question_type}\n"
386
+ f"πŸ“Ž Has Attachment: {'Yes' if file_name else 'No'}\n"
387
  f"❓ Question Preview: {question_preview[:100]}{'...' if len(question_preview) > 100 else ''}\n"
388
  f"πŸ’­ Agent Answer: {submitted_answer[:150]}{'...' if len(submitted_answer) > 150 else ''}\n"
389
  f"⏱️ Processing Time: {processing_time:.2f}s\n"
 
461
  agent.cleanup()
462
 
463
 
464
+ def compute_question_with_attachment(question: str, task_id: str, file_name: str, api_url: str) -> str:
465
+ """
466
+ Adds file attachment information to the question if a file is present.
467
+ This allows the agent to know about and access attached files.
468
+ """
469
+ if file_name:
470
+ file_url = f"{api_url}/files/{task_id}"
471
+ return f"{question}\n\nAttached file available at: {file_url}"
472
+ else:
473
+ return question
474
+
475
+
476
  # --- Gradio Interface ---
477
  with gr.Blocks() as demo:
478
  gr.Markdown("# Hugging Face Agent Certification Runner")