bacchi87 commited on
Commit
c4b2bad
·
verified ·
1 Parent(s): 0217aa1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
5
+
6
+ # 1. Initialize the search tool and model
7
+ search_tool = DuckDuckGoSearchTool()
8
+ model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
9
+
10
+ agent = CodeAgent(
11
+ tools=[search_tool],
12
+ model=model,
13
+ additional_authorized_imports=["math", "re", "json", "datetime"]
14
+ )
15
+
16
+ def answer_question(question: str) -> str:
17
+ """Answers an evaluation question using the agent."""
18
+ prompt = (
19
+ f"Answer the following question precisely and concisely. "
20
+ f"Give only the factual answer without extra conversation or explanation:\n{question}"
21
+ )
22
+ try:
23
+ response = agent.run(prompt)
24
+ return str(response).strip()
25
+ except Exception as e:
26
+ return "N/A"
27
+
28
+ def run_evaluation(profile: gr.OAuthProfile | None):
29
+ if profile is None:
30
+ return "Please sign in with Hugging Face first.", []
31
+
32
+ username = profile.username
33
+ agent_code_url = f"https://huggingface.co/spaces/{username}/Final_Assignment_Template/tree/main"
34
+
35
+ base_url = "https://agents-course-unit4-evaluator.hf.space"
36
+
37
+ # Fetch questions
38
+ try:
39
+ q_resp = requests.get(f"{base_url}/questions", timeout=30)
40
+ questions_data = q_resp.json()
41
+ except Exception as e:
42
+ return f"Failed to fetch questions: {e}", []
43
+
44
+ questions = questions_data if isinstance(questions_data, list) else questions_data.get("questions", [])
45
+
46
+ answers = []
47
+ log_output = f"Running