Sonu313131 commited on
Commit
0e37d38
Β·
verified Β·
1 Parent(s): 03a5300

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -18,9 +18,9 @@ login(token=os.environ["HUGGINGFACEHUB_API_TOKEN"])
18
  # --- Define Tools ---
19
  search_tool = DuckDuckGoSearchTool()
20
 
21
- # --- Main Async Function ---
22
  async def run_and_submit_all(profile: gr.OAuthProfile | None):
23
- # Initialize Agent
24
  try:
25
  agent = CodeAgent(
26
  tools=[search_tool],
@@ -29,23 +29,26 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
29
  verbosity_level=2
30
  )
31
  except Exception as e:
32
- return f"❌ Agent Initialization Error: {e}", None
 
33
 
34
  space_id = os.getenv("SPACE_ID", "unknown")
35
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
36
 
37
- # Fetch questions
38
  try:
39
  response = requests.get(QUESTIONS_URL, timeout=15)
40
  response.raise_for_status()
41
  questions = response.json()
42
  if not questions:
43
- return "⚠️ No questions received.", None
 
44
  except Exception as e:
45
- return f"❌ Failed to fetch questions: {e}", None
 
46
 
47
  answers = []
48
  logs = []
 
49
 
50
  for item in questions:
51
  task_id = item.get("task_id")
@@ -53,6 +56,9 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
53
  if not task_id or not question:
54
  continue
55
 
 
 
 
56
  system_prompt = (
57
  "You are a general AI assistant. I will ask you a question. "
58
  "Report your thoughts, and finish your answer with the following template: "
@@ -61,7 +67,6 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
61
  full_prompt = system_prompt + f"Question: {question.strip()}"
62
 
63
  try:
64
- loop = asyncio.get_running_loop()
65
  result = await loop.run_in_executor(None, lambda: agent(full_prompt))
66
 
67
  if isinstance(result, dict) and "final_answer" in result:
@@ -75,16 +80,19 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
75
  final_answer = str(result).strip()
76
 
77
  except Exception as e:
78
- print(f"[ERROR] Task {task_id} failed: {e}")
79
  final_answer = f"AGENT ERROR: {e}"
 
80
 
81
  answers.append({"task_id": task_id, "model_answer": final_answer})
82
  logs.append({"Task ID": task_id, "Question": question, "Submitted Answer": final_answer})
 
 
83
 
84
  valid_answers = [a for a in answers if isinstance(a["task_id"], str) and isinstance(a["model_answer"], str)]
85
 
86
  if not valid_answers:
87
- return "❌ Agent produced no valid answers.", pd.DataFrame(logs)
 
88
 
89
  submission = {
90
  "username": profile.username if profile else "unknown",
@@ -106,10 +114,10 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
106
  f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})\n"
107
  f"Message: {result_data.get('message', 'No message.')}"
108
  )
109
- return summary, pd.DataFrame(logs)
110
 
111
  except Exception as e:
112
- return f"❌ Submission failed: {e}", pd.DataFrame(logs)
113
 
114
  # --- Gradio UI ---
115
  with gr.Blocks() as demo:
@@ -117,15 +125,16 @@ with gr.Blocks() as demo:
117
  gr.Markdown("""
118
  - Log in with your Hugging Face account.
119
  - Click the button below to run the agent and submit the answers.
120
- - Wait for the final score to appear.
121
  """)
122
 
123
  gr.LoginButton()
124
  run_button = gr.Button("πŸš€ Run Evaluation & Submit")
125
- status = gr.Textbox(label="Status", lines=6)
126
  table = gr.DataFrame(label="Answer Log")
 
127
 
128
- run_button.click(fn=run_and_submit_all, outputs=[status, table])
129
 
130
  # --- Launch ---
131
  if __name__ == "__main__":
 
18
  # --- Define Tools ---
19
  search_tool = DuckDuckGoSearchTool()
20
 
21
+ # --- Main Async Function with Progress Logs ---
22
  async def run_and_submit_all(profile: gr.OAuthProfile | None):
23
+ log_output = ""
24
  try:
25
  agent = CodeAgent(
26
  tools=[search_tool],
 
29
  verbosity_level=2
30
  )
31
  except Exception as e:
32
+ yield f"❌ Agent Initialization Error: {e}", None, log_output
33
+ return
34
 
35
  space_id = os.getenv("SPACE_ID", "unknown")
36
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
37
 
 
38
  try:
39
  response = requests.get(QUESTIONS_URL, timeout=15)
40
  response.raise_for_status()
41
  questions = response.json()
42
  if not questions:
43
+ yield "⚠️ No questions received.", None, log_output
44
+ return
45
  except Exception as e:
46
+ yield f"❌ Failed to fetch questions: {e}", None, log_output
47
+ return
48
 
49
  answers = []
50
  logs = []
51
+ loop = asyncio.get_running_loop()
52
 
53
  for item in questions:
54
  task_id = item.get("task_id")
 
56
  if not task_id or not question:
57
  continue
58
 
59
+ log_output += f"πŸ” Solving Task ID: {task_id}...\n"
60
+ yield None, None, log_output # Live update
61
+
62
  system_prompt = (
63
  "You are a general AI assistant. I will ask you a question. "
64
  "Report your thoughts, and finish your answer with the following template: "
 
67
  full_prompt = system_prompt + f"Question: {question.strip()}"
68
 
69
  try:
 
70
  result = await loop.run_in_executor(None, lambda: agent(full_prompt))
71
 
72
  if isinstance(result, dict) and "final_answer" in result:
 
80
  final_answer = str(result).strip()
81
 
82
  except Exception as e:
 
83
  final_answer = f"AGENT ERROR: {e}"
84
+ print(f"[ERROR] Task {task_id} failed: {e}")
85
 
86
  answers.append({"task_id": task_id, "model_answer": final_answer})
87
  logs.append({"Task ID": task_id, "Question": question, "Submitted Answer": final_answer})
88
+ log_output += f"βœ… Done: {task_id} β€” Answer: {final_answer[:60]}\n"
89
+ yield None, None, log_output # Live update
90
 
91
  valid_answers = [a for a in answers if isinstance(a["task_id"], str) and isinstance(a["model_answer"], str)]
92
 
93
  if not valid_answers:
94
+ yield "❌ Agent produced no valid answers.", pd.DataFrame(logs), log_output
95
+ return
96
 
97
  submission = {
98
  "username": profile.username if profile else "unknown",
 
114
  f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})\n"
115
  f"Message: {result_data.get('message', 'No message.')}"
116
  )
117
+ yield summary, pd.DataFrame(logs), log_output
118
 
119
  except Exception as e:
120
+ yield f"❌ Submission failed: {e}", pd.DataFrame(logs), log_output
121
 
122
  # --- Gradio UI ---
123
  with gr.Blocks() as demo:
 
125
  gr.Markdown("""
126
  - Log in with your Hugging Face account.
127
  - Click the button below to run the agent and submit the answers.
128
+ - Watch the log to see which question is being solved in real-time.
129
  """)
130
 
131
  gr.LoginButton()
132
  run_button = gr.Button("πŸš€ Run Evaluation & Submit")
133
+ status = gr.Textbox(label="Final Status", lines=6)
134
  table = gr.DataFrame(label="Answer Log")
135
+ progress_log = gr.Textbox(label="Live Progress Log", lines=10, interactive=False)
136
 
137
+ run_button.click(fn=run_and_submit_all, outputs=[status, table, progress_log])
138
 
139
  # --- Launch ---
140
  if __name__ == "__main__":