Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,25 +6,32 @@ from huggingface_hub import InferenceClient
|
|
6 |
from duckduckgo_search import DDGS
|
7 |
from datasets import load_dataset
|
8 |
import wikipediaapi
|
|
|
|
|
9 |
|
10 |
-
# Constants
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
13 |
|
14 |
-
# Setup
|
15 |
-
llm_model_id = "HuggingFaceH4/zephyr-7b-beta"
|
16 |
hf_client = InferenceClient(llm_model_id, token=HF_TOKEN)
|
|
|
17 |
|
18 |
-
# Wikipedia API
|
19 |
wiki_api = wikipediaapi.Wikipedia(
|
20 |
-
language='en',
|
21 |
user_agent='SmartAgent/1.0 ([email protected])'
|
22 |
)
|
23 |
|
24 |
-
#
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Search functions
|
28 |
def duckduckgo_search(query):
|
29 |
with DDGS() as ddgs:
|
30 |
results = [r for r in ddgs.text(query, max_results=3)]
|
@@ -32,9 +39,35 @@ def duckduckgo_search(query):
|
|
32 |
|
33 |
def wikipedia_search(query):
|
34 |
page = wiki_api.page(query)
|
35 |
-
return page.summary if page.exists() else
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
# Comprehensive Agent
|
38 |
class SmartAgent:
|
39 |
def __init__(self):
|
40 |
pass
|
@@ -42,20 +75,31 @@ class SmartAgent:
|
|
42 |
def __call__(self, question: str) -> str:
|
43 |
q_lower = question.lower()
|
44 |
|
|
|
45 |
if any(term in q_lower for term in ["current", "latest", "2024", "2025", "who is the president", "recent", "live"]):
|
46 |
return duckduckgo_search(question)
|
47 |
|
|
|
48 |
wiki_result = wikipedia_search(question)
|
49 |
-
if
|
50 |
return wiki_result
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
try:
|
53 |
-
resp = hf_client.text_generation(question, max_new_tokens=
|
54 |
return resp
|
55 |
-
except Exception
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
# Submission logic
|
59 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
60 |
space_id = os.getenv("SPACE_ID")
|
61 |
if profile:
|
@@ -80,15 +124,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
80 |
|
81 |
results_log = []
|
82 |
answers_payload = []
|
83 |
-
correct_answers = 0
|
84 |
|
85 |
for item in questions_data:
|
86 |
task_id = item.get("task_id")
|
87 |
question_text = item.get("question")
|
|
|
88 |
if not task_id or not question_text:
|
89 |
continue
|
90 |
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
93 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
94 |
|
@@ -114,7 +164,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
114 |
except Exception as e:
|
115 |
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
116 |
|
117 |
-
# Gradio
|
118 |
with gr.Blocks() as demo:
|
119 |
gr.Markdown("# Smart Agent Evaluation Runner")
|
120 |
gr.Markdown("""
|
@@ -123,12 +173,10 @@ with gr.Blocks() as demo:
|
|
123 |
2. Log in to Hugging Face.
|
124 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
125 |
""")
|
126 |
-
|
127 |
gr.LoginButton()
|
128 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
129 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
130 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
131 |
-
|
132 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
133 |
|
134 |
if __name__ == "__main__":
|
|
|
6 |
from duckduckgo_search import DDGS
|
7 |
from datasets import load_dataset
|
8 |
import wikipediaapi
|
9 |
+
from llama_index.core import VectorStoreIndex, Document, StorageContext, load_index_from_storage
|
10 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
11 |
|
|
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
+
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
14 |
+
llm_model_id = "deepseek-ai/DeepSeek-V2"
|
15 |
+
llm_fallback_id = "mistralai/Mistral-7B-Instruct-v0.2"
|
16 |
|
17 |
+
# Setup HF LLM client
|
|
|
18 |
hf_client = InferenceClient(llm_model_id, token=HF_TOKEN)
|
19 |
+
hf_fallback = InferenceClient(llm_fallback_id, token=HF_TOKEN)
|
20 |
|
21 |
+
# Wikipedia API with user agent
|
22 |
wiki_api = wikipediaapi.Wikipedia(
|
23 |
+
language='en',
|
24 |
user_agent='SmartAgent/1.0 ([email protected])'
|
25 |
)
|
26 |
|
27 |
+
# Build or load LlamaIndex for fast retrieval (optional, for small Wikipedia sample)
|
28 |
+
try:
|
29 |
+
wiki_dataset = load_dataset("wikipedia", "20220301.en", split="train[:5000]", trust_remote_code=True)
|
30 |
+
docs = [Document(text=doc['text']) for doc in wiki_dataset]
|
31 |
+
index = VectorStoreIndex.from_documents(docs)
|
32 |
+
except Exception as e:
|
33 |
+
index = None
|
34 |
|
|
|
35 |
def duckduckgo_search(query):
|
36 |
with DDGS() as ddgs:
|
37 |
results = [r for r in ddgs.text(query, max_results=3)]
|
|
|
39 |
|
40 |
def wikipedia_search(query):
|
41 |
page = wiki_api.page(query)
|
42 |
+
return page.summary if page.exists() else None
|
43 |
+
|
44 |
+
def index_search(query):
|
45 |
+
if index is None:
|
46 |
+
return None
|
47 |
+
res = index.as_query_engine().query(query)
|
48 |
+
return str(res) if res else None
|
49 |
+
|
50 |
+
def handle_excel(file_url):
|
51 |
+
# Download and sum food (not drinks)
|
52 |
+
try:
|
53 |
+
fname = "tmp.xlsx"
|
54 |
+
r = requests.get(file_url)
|
55 |
+
with open(fname, "wb") as f:
|
56 |
+
f.write(r.content)
|
57 |
+
df = pd.read_excel(fname)
|
58 |
+
# Assume drinks have 'drink' or 'beverage' in a column called 'Item' or 'Category'
|
59 |
+
if "Item" in df.columns:
|
60 |
+
food_df = df[~df["Item"].str.contains("drink|beverage", case=False, na=False)]
|
61 |
+
total = food_df["Total"].sum()
|
62 |
+
return f"${total:.2f}"
|
63 |
+
if "Category" in df.columns:
|
64 |
+
food_df = df[df["Category"].str.lower() == "food"]
|
65 |
+
total = food_df["Total"].sum()
|
66 |
+
return f"${total:.2f}"
|
67 |
+
return "File parsed but could not find food sales."
|
68 |
+
except Exception as e:
|
69 |
+
return f"Excel error: {e}"
|
70 |
|
|
|
71 |
class SmartAgent:
|
72 |
def __init__(self):
|
73 |
pass
|
|
|
75 |
def __call__(self, question: str) -> str:
|
76 |
q_lower = question.lower()
|
77 |
|
78 |
+
# DuckDuckGo for current events/recent/live
|
79 |
if any(term in q_lower for term in ["current", "latest", "2024", "2025", "who is the president", "recent", "live"]):
|
80 |
return duckduckgo_search(question)
|
81 |
|
82 |
+
# Wikipedia summary
|
83 |
wiki_result = wikipedia_search(question)
|
84 |
+
if wiki_result:
|
85 |
return wiki_result
|
86 |
|
87 |
+
# LlamaIndex retrieval
|
88 |
+
rag_result = index_search(question)
|
89 |
+
if rag_result:
|
90 |
+
return rag_result
|
91 |
+
|
92 |
+
# LLM generation
|
93 |
try:
|
94 |
+
resp = hf_client.text_generation(question, max_new_tokens=256)
|
95 |
return resp
|
96 |
+
except Exception:
|
97 |
+
try:
|
98 |
+
resp = hf_fallback.text_generation(question, max_new_tokens=256)
|
99 |
+
return resp
|
100 |
+
except Exception as e:
|
101 |
+
return f"HF LLM error: {e}"
|
102 |
|
|
|
103 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
104 |
space_id = os.getenv("SPACE_ID")
|
105 |
if profile:
|
|
|
124 |
|
125 |
results_log = []
|
126 |
answers_payload = []
|
|
|
127 |
|
128 |
for item in questions_data:
|
129 |
task_id = item.get("task_id")
|
130 |
question_text = item.get("question")
|
131 |
+
file_url = item.get("file_url", None)
|
132 |
if not task_id or not question_text:
|
133 |
continue
|
134 |
|
135 |
+
# Handle Excel task
|
136 |
+
if file_url and ("excel" in question_text.lower() or "file" in question_text.lower()):
|
137 |
+
submitted_answer = handle_excel(file_url)
|
138 |
+
else:
|
139 |
+
submitted_answer = agent(question_text)
|
140 |
+
|
141 |
+
# Final answer extraction/formatting if needed (TODO: Add regex/extract logic)
|
142 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
143 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
144 |
|
|
|
164 |
except Exception as e:
|
165 |
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
166 |
|
167 |
+
# Gradio interface
|
168 |
with gr.Blocks() as demo:
|
169 |
gr.Markdown("# Smart Agent Evaluation Runner")
|
170 |
gr.Markdown("""
|
|
|
173 |
2. Log in to Hugging Face.
|
174 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
175 |
""")
|
|
|
176 |
gr.LoginButton()
|
177 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
178 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
179 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
180 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
181 |
|
182 |
if __name__ == "__main__":
|