alfulanny commited on
Commit
3fd4b07
·
verified ·
1 Parent(s): 10205df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -3
app.py CHANGED
@@ -1,7 +1,16 @@
1
- """Gradio interface for the smolagents CodeAgent."""
 
 
 
 
 
 
 
2
  import os
3
  import sys
4
  import logging
 
 
5
  from code_agent import run_agent
6
 
7
  # (Gradio update/analytics env flags removed per user request)
@@ -21,6 +30,68 @@ def respond(prompt: str) -> str:
21
  logger.error("Agent failed: %s", e)
22
  return f"Agent error: {type(e).__name__}: {str(e)[:200]}"
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  _demo = None
26
 
@@ -65,15 +136,72 @@ def _get_demo():
65
 
66
  with gr.Blocks() as demo:
67
  gr.Markdown("# Agents Course — Final Agent Demo")
 
 
68
  with gr.Row():
69
  prompt_box = gr.Textbox(label="Fetched Prompt (read-only)", lines=6)
70
  with gr.Row():
71
  fetch_btn = gr.Button("Fetch Random Task")
72
  run_btn = gr.Button("Run Agent on Fetched Task")
73
- output_box = gr.Textbox(label="Agent Response", lines=12)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
 
75
  fetch_btn.click(fn=fetch_random_task, inputs=[], outputs=[prompt_box])
76
- run_btn.click(fn=run_on_current, inputs=[prompt_box], outputs=[output_box])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  _demo = demo
79
  return _demo
 
1
+ """
2
+ Gradio interface for the smolagents CodeAgent.
3
+
4
+ By default this script will NOT launch a local Gradio server. To allow
5
+ local runs (for testing) set the environment variable `RUN_LOCAL=1` or pass
6
+ `--run-local` on the command line. This prevents accidental local launches
7
+ when you intended to deploy to Hugging Face Spaces.
8
+ """
9
  import os
10
  import sys
11
  import logging
12
+ import json
13
+ from typing import List, Dict, Any
14
  from code_agent import run_agent
15
 
16
  # (Gradio update/analytics env flags removed per user request)
 
30
  logger.error("Agent failed: %s", e)
31
  return f"Agent error: {type(e).__name__}: {str(e)[:200]}"
32
 
33
+ def extract_prompt_from_question(q: Dict[str, Any]) -> str:
34
+ """Extract the actual question/prompt from a question dict."""
35
+ for key in ("question", "prompt", "input", "text", "task"):
36
+ if key in q and isinstance(q[key], str):
37
+ return q[key]
38
+ return str(q)
39
+
40
+ def fetch_all_questions() -> List[Dict[str, Any]]:
41
+ """Fetch all questions from the scoring API."""
42
+ try:
43
+ from evaluation_client import ScoringAPIClient
44
+ client = ScoringAPIClient()
45
+ questions = client.get_questions()
46
+ return questions if questions else []
47
+ except Exception as e:
48
+ logger.error("Failed to fetch questions: %s", e)
49
+ return []
50
+
51
+ def answer_all_questions(questions: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
52
+ """Answer all questions and return answers in submission format.
53
+
54
+ Args:
55
+ questions: List of question dicts from the scoring API.
56
+
57
+ Returns:
58
+ List of dicts with keys: task_id, submitted_answer.
59
+ """
60
+ answers = []
61
+ total = len(questions)
62
+
63
+ for idx, q in enumerate(questions, start=1):
64
+ task_id = q.get("task_id") or q.get("id") or q.get("taskId")
65
+ if not task_id:
66
+ logger.warning("Question %d/%d: Missing task_id, skipping", idx, total)
67
+ continue
68
+
69
+ prompt = extract_prompt_from_question(q)
70
+ logger.info("Question %d/%d: task_id=%s, prompt_len=%d", idx, total, task_id, len(prompt))
71
+
72
+ try:
73
+ ans = run_agent(prompt)
74
+ ans = ans.strip()
75
+ logger.info(" ✓ Answer: %s", ans[:60])
76
+ answers.append({"task_id": task_id, "submitted_answer": ans})
77
+ except Exception as e:
78
+ logger.error(" ❌ Failed to answer: %s", type(e).__name__)
79
+ answers.append({"task_id": task_id, "submitted_answer": f"(error) {type(e).__name__}"})
80
+
81
+ logger.info("✓ Answered %d/%d questions", len(answers), total)
82
+ return answers
83
+
84
+ def submit_answers(username: str, agent_code_url: str, answers: List[Dict[str, Any]]) -> str:
85
+ """Submit answers to the scoring API."""
86
+ try:
87
+ from evaluation_client import ScoringAPIClient
88
+ client = ScoringAPIClient()
89
+ resp = client.submit(username=username, agent_code=agent_code_url, answers=answers)
90
+ return f"✓ Submission successful! Response: {resp}"
91
+ except Exception as e:
92
+ logger.error("Submission failed: %s", e)
93
+ return f"ERROR: Submission failed: {e}"
94
+
95
 
96
  _demo = None
97
 
 
136
 
137
  with gr.Blocks() as demo:
138
  gr.Markdown("# Agents Course — Final Agent Demo")
139
+
140
+ # Single question interface
141
  with gr.Row():
142
  prompt_box = gr.Textbox(label="Fetched Prompt (read-only)", lines=6)
143
  with gr.Row():
144
  fetch_btn = gr.Button("Fetch Random Task")
145
  run_btn = gr.Button("Run Agent on Fetched Task")
146
+ single_output = gr.Textbox(label="Agent Response", lines=6)
147
+
148
+ # Batch processing interface
149
+ gr.Markdown("## Batch Processing")
150
+ questions_state = gr.State([])
151
+ answers_state = gr.State([])
152
+ status_box = gr.Textbox(label="Status", lines=4)
153
+
154
+ with gr.Row():
155
+ fetch_all_btn = gr.Button("Fetch All Questions")
156
+ answer_all_btn = gr.Button("Answer All Questions")
157
+ submit_btn = gr.Button("Submit All Answers")
158
+
159
+ with gr.Row():
160
+ username_box = gr.Textbox(label="Hugging Face Username", placeholder="your_hf_username")
161
+ agent_url_box = gr.Textbox(label="Agent Code URL", placeholder="https://huggingface.co/spaces/...")
162
+
163
+ batch_output = gr.Textbox(label="Batch Results", lines=12)
164
 
165
+ # Wire up the buttons
166
  fetch_btn.click(fn=fetch_random_task, inputs=[], outputs=[prompt_box])
167
+ run_btn.click(fn=run_on_current, inputs=[prompt_box], outputs=[single_output])
168
+
169
+ def fetch_all_questions_wrapper():
170
+ questions = fetch_all_questions()
171
+ status = f"Fetched {len(questions)} questions"
172
+ return questions, [], status
173
+
174
+ def answer_all_questions_wrapper(questions):
175
+ if not questions:
176
+ return [], "No questions to answer"
177
+ answers = answer_all_questions(questions)
178
+ status = f"Answered {len(answers)} questions"
179
+ return answers, status
180
+
181
+ def submit_answers_wrapper(username, agent_url, answers):
182
+ if not answers:
183
+ return "No answers to submit"
184
+ if not username or not agent_url:
185
+ return "Please provide both username and agent code URL"
186
+ return submit_answers(username, agent_url, answers)
187
+
188
+ fetch_all_btn.click(
189
+ fn=fetch_all_questions_wrapper,
190
+ inputs=[],
191
+ outputs=[questions_state, answers_state, status_box]
192
+ )
193
+
194
+ answer_all_btn.click(
195
+ fn=answer_all_questions_wrapper,
196
+ inputs=[questions_state],
197
+ outputs=[answers_state, status_box]
198
+ )
199
+
200
+ submit_btn.click(
201
+ fn=submit_answers_wrapper,
202
+ inputs=[username_box, agent_url_box, answers_state],
203
+ outputs=[batch_output]
204
+ )
205
 
206
  _demo = demo
207
  return _demo