wishmi1234 commited on
Commit
f0197bd
·
verified ·
1 Parent(s): 54b817b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -35
app.py CHANGED
@@ -1,83 +1,87 @@
1
  import gradio as gr
2
- import pandas as pd
3
  import json
4
- from smolagents import CodeAgent, InferenceClientModel
 
 
5
  from smolagents.tools import (
6
  DuckDuckGoSearchTool,
7
  WikipediaSearchTool,
8
  PythonInterpreterTool,
9
- UserInputTool
10
  )
11
 
12
- # -------------------------- MODEL SETUP -------------------------- #
13
  model = InferenceClientModel(
14
- model_id="Qwen/Qwen2-7B-Instruct", # You can change this to another Hugging Face Inference-compatible model
15
- system_message="You are a helpful AI assistant for answering academic questions in a concise and accurate way.",
16
  max_tokens=512,
17
  )
18
 
19
- # -------------------------- TOOLS SETUP -------------------------- #
20
  tools = [
21
- DuckDuckGoSearchTool(max_results=5, rate_limit=2.0),
22
- WikipediaSearchTool(language="en", user_agent="my-eval-agent"),
23
  PythonInterpreterTool(),
24
- UserInputTool()
25
  ]
26
 
27
- # -------------------------- AGENT SETUP -------------------------- #
28
- agent = CodeAgent(
29
- model=model,
30
- tools=tools
31
- )
32
 
33
- # -------------------------- EVALUATION FUNCTION -------------------------- #
34
  def run_agent(file):
 
 
 
35
  try:
36
- questions_data = json.load(file)
 
37
  except Exception as e:
38
- return f"Invalid JSON: {e}", pd.DataFrame()
39
 
40
- results_log = []
41
- answers_payload = []
42
 
43
  for item in questions_data:
44
  task_id = item.get("task_id")
45
- question_text = item.get("question")
46
-
47
- if not task_id or question_text is None:
 
 
 
 
 
48
  continue
49
 
50
  try:
51
- answer = agent.run(question_text)
52
  answers_payload.append({"task_id": task_id, "submitted_answer": answer})
53
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
54
  except Exception as e:
55
  error_msg = f"AGENT ERROR: {e}"
56
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_msg})
 
57
 
58
  if not answers_payload:
59
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
60
 
61
- results_df = pd.DataFrame(results_log)
62
- return "Agent run completed!", results_df
63
 
64
- # -------------------------- GRADIO UI -------------------------- #
65
  demo = gr.Interface(
66
  fn=run_agent,
67
- inputs=gr.File(label="Upload questions.json"),
68
  outputs=[
69
  gr.Textbox(label="Status"),
70
- gr.Dataframe(label="Results", wrap=True),
71
  ],
72
- title="SmolAgent Question Evaluator",
73
- description="Upload a JSON file with `task_id` and `question` fields to evaluate the agent's performance.",
74
  )
75
 
76
  if __name__ == "__main__":
77
  demo.launch()
78
 
79
-
80
-
81
  # import os
82
  # import pandas as pd
83
  # import requests
 
1
  import gradio as gr
 
2
  import json
3
+ import pandas as pd
4
+ from smolagents import CodeAgent
5
+ from smolagents.models import InferenceClientModel
6
  from smolagents.tools import (
7
  DuckDuckGoSearchTool,
8
  WikipediaSearchTool,
9
  PythonInterpreterTool,
10
+ UserInputTool,
11
  )
12
 
13
+ # Define the model
14
  model = InferenceClientModel(
15
+ model_id="Qwen/Qwen1.5-1.8B-Chat", # Ensure this is a model that supports system_message
16
+ system_message="You are a helpful academic assistant tasked with solving student questions accurately.",
17
  max_tokens=512,
18
  )
19
 
20
+ # Define tools
21
  tools = [
22
+ DuckDuckGoSearchTool(),
23
+ WikipediaSearchTool(),
24
  PythonInterpreterTool(),
25
+ UserInputTool(),
26
  ]
27
 
28
+ # Initialize agent
29
+ agent = CodeAgent(model=model, tools=tools)
 
 
 
30
 
31
+ # Run agent on uploaded question set
32
  def run_agent(file):
33
+ results_log = []
34
+ answers_payload = []
35
+
36
  try:
37
+ with open(file.name, "r", encoding="utf-8") as f:
38
+ questions_data = json.load(f)
39
  except Exception as e:
40
+ return f"Error reading JSON file: {e}", pd.DataFrame()
41
 
42
+ print(f"Running agent on {len(questions_data)} questions...")
 
43
 
44
  for item in questions_data:
45
  task_id = item.get("task_id")
46
+ question = item.get("question")
47
+
48
+ if not task_id or question is None:
49
+ results_log.append({
50
+ "Task ID": task_id if task_id else "MISSING",
51
+ "Question": question if question else "MISSING",
52
+ "Submitted Answer": "Invalid entry",
53
+ })
54
  continue
55
 
56
  try:
57
+ answer = agent(question)
58
  answers_payload.append({"task_id": task_id, "submitted_answer": answer})
59
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
60
  except Exception as e:
61
  error_msg = f"AGENT ERROR: {e}"
62
+ print(error_msg)
63
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": error_msg})
64
 
65
  if not answers_payload:
66
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
67
 
68
+ return "Success! Answers submitted.", pd.DataFrame(results_log)
 
69
 
70
+ # Create Gradio UI
71
  demo = gr.Interface(
72
  fn=run_agent,
73
+ inputs=gr.File(label="Upload your questions.json"),
74
  outputs=[
75
  gr.Textbox(label="Status"),
76
+ gr.Dataframe(label="Submitted Answers")
77
  ],
78
+ title="Academic Assistant Agent",
79
+ description="Upload a .json file with questions and task_ids to run the agent.",
80
  )
81
 
82
  if __name__ == "__main__":
83
  demo.launch()
84
 
 
 
85
  # import os
86
  # import pandas as pd
87
  # import requests