Hmd6919 commited on
Commit
b4f2a5c
·
verified ·
1 Parent(s): b5f3ec2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -82
app.py CHANGED
@@ -1,84 +1,8 @@
1
- import os
2
- import gradio as gr
3
- import requests
4
- import pandas as pd
5
- from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
6
 
7
- def create_agent():
8
- # Try smaller free model
9
- model = LiteLLMModel(model_id="huggingface/HuggingFaceH4/zephyr-7b-beta")
10
- search_tool = DuckDuckGoSearchTool()
11
- agent = CodeAgent(
12
- tools=[search_tool],
13
- model=model,
14
- additional_authorized_imports=["requests", "pandas", "numpy", "time", "re", "math"]
15
- )
16
- return agent
17
 
18
- def run_and_submit_all(profile: gr.OAuthProfile | None):
19
- space_id = os.getenv("SPACE_ID")
20
-
21
- if profile:
22
- username = f"{profile.username}"
23
- else:
24
- return "Please Login to Hugging Face with the button.", None
25
-
26
- api_url = DEFAULT_API_URL
27
- questions_url = f"{api_url}/questions"
28
- submit_url = f"{api_url}/submit"
29
-
30
- # 1. Instantiate the real Agent
31
- try:
32
- smol_agent = create_agent()
33
- except Exception as e:
34
- return f"Error initializing agent: {e}", None
35
-
36
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
37
-
38
- # 2. Fetch Questions
39
- try:
40
- response = requests.get(questions_url, timeout=15)
41
- response.raise_for_status()
42
- questions_data = response.json()
43
- except Exception as e:
44
- return f"Error fetching questions: {e}", None
45
-
46
- # 3. Run your Agent
47
- results_log = []
48
- answers_payload = []
49
-
50
- for item in questions_data:
51
- task_id = item.get("task_id")
52
- question_text = item.get("question")
53
-
54
- try:
55
- # Here the agent actually "thinks" and returns a result
56
- submitted_answer = smol_agent.run(question_text)
57
- answers_payload.append({"task_id": task_id, "submitted_answer": str(submitted_answer)})
58
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": str(submitted_answer)})
59
- except Exception as e:
60
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
61
-
62
- # 4. Submit
63
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
64
-
65
- try:
66
- response = requests.post(submit_url, json=submission_data, timeout=60)
67
- response.raise_for_status()
68
- result_data = response.json()
69
- final_status = f"Submission Successful!\nScore: {result_data.get('score')}%"
70
- return final_status, pd.DataFrame(results_log)
71
- except Exception as e:
72
- return f"Submission Failed: {e}", pd.DataFrame(results_log)
73
-
74
- # --- Gradio Interface ---
75
- with gr.Blocks() as demo:
76
- gr.Markdown("# 🤖 My Custom Agent Runner")
77
- gr.LoginButton()
78
- run_button = gr.Button("Run Evaluation & Submit All Answers")
79
- status_output = gr.Textbox(label="Status", lines=5)
80
- results_table = gr.DataFrame(label="Results", wrap=True)
81
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
82
-
83
- if __name__ == "__main__":
84
- demo.launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
 
 
 
 
2
 
3
+ # 1. Define the tool (the agent's "eyes")
4
+ search_tool = DuckDuckGoSearchTool()
 
 
 
 
 
 
 
 
5
 
6
+ # 2. Define the model (the agent's "brain")
7
+ # We use a standard open model so you don't need a paid API key
8
+ model = HfApiModel()