Update app.py
Browse files
app.py
CHANGED
@@ -2,96 +2,124 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
-
from huggingface_hub import InferenceClient
|
6 |
-
from duckduckgo_search import DDGS
|
7 |
from datasets import load_dataset
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
-
|
11 |
-
# Hugging Face Token (set in environment)
|
12 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
13 |
-
deepseek_model = "deepseek-ai/DeepSeek-R1"
|
14 |
-
hf_client = InferenceClient(model=deepseek_model, token=HF_TOKEN)
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return "No relevant information found on Wikipedia."
|
24 |
|
|
|
25 |
def duckduckgo_search(query):
|
26 |
with DDGS() as ddgs:
|
27 |
-
results = [r
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
)
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
class SmartAgent:
|
40 |
def __call__(self, question: str) -> str:
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
return duckduckgo_search(question)
|
44 |
-
deepseek_response = ask_deepseek(question)
|
45 |
-
if "DeepSeek Error" not in deepseek_response and deepseek_response.strip():
|
46 |
-
return deepseek_response
|
47 |
-
# fallback to Wikipedia if DeepSeek fails
|
48 |
-
return search_wikipedia(question)
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
51 |
-
|
|
|
|
|
|
|
52 |
return "Please Login to Hugging Face with the button.", None
|
53 |
-
|
54 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
55 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
try:
|
59 |
-
|
60 |
except Exception as e:
|
61 |
-
return f"
|
62 |
|
63 |
-
questions_data = requests.get(questions_url).json()
|
64 |
results_log, answers_payload = [], []
|
65 |
-
|
66 |
for item in questions_data:
|
67 |
-
task_id = item.get("task_id")
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
|
72 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
|
73 |
|
74 |
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
75 |
-
response = requests.post(submit_url, json=submission_data).json()
|
76 |
-
|
77 |
-
final_status = (
|
78 |
-
f"Submission Successful!\n"
|
79 |
-
f"User: {response.get('username')}\n"
|
80 |
-
f"Overall Score: {response.get('score', 'N/A')}%\n"
|
81 |
-
f"({response.get('correct_count', '?')}/{response.get('total_attempted', '?')} correct)\n"
|
82 |
-
f"Message: {response.get('message', 'No message received.')}"
|
83 |
-
)
|
84 |
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
|
|
87 |
with gr.Blocks() as demo:
|
88 |
-
gr.Markdown("# Smart Agent Evaluation
|
89 |
gr.LoginButton()
|
90 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
91 |
-
status_output = gr.Textbox(label="
|
92 |
-
results_table = gr.DataFrame(label="
|
93 |
|
94 |
-
run_button.click(run_and_submit_all, outputs=[status_output, results_table])
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
demo.launch(debug=True)
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
|
|
|
|
5 |
from datasets import load_dataset
|
6 |
+
from duckduckgo_search import DDGS
|
7 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
8 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
9 |
+
from huggingface_hub import InferenceClient
|
10 |
+
import wikipediaapi
|
11 |
|
12 |
+
# Constants
|
13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
14 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
|
15 |
|
16 |
+
# Advanced LLM via Hugging Face Inference API
|
17 |
+
llm_model_id = "deepseek-ai/DeepSeek-R1"
|
18 |
+
hf_client = InferenceClient(llm_model_id, token=HF_TOKEN)
|
19 |
+
|
20 |
+
# Wikipedia API setup
|
21 |
+
wiki_api = wikipediaapi.Wikipedia('en')
|
22 |
|
23 |
+
# Load Wikipedia dataset from Hugging Face
|
24 |
+
wiki_dataset = load_dataset(
|
25 |
+
"wikipedia", "20220301.en", split="train[:10000]", trust_remote_code=True
|
26 |
+
)
|
|
|
27 |
|
28 |
+
# DuckDuckGo search function
|
29 |
def duckduckgo_search(query):
|
30 |
with DDGS() as ddgs:
|
31 |
+
results = [r for r in ddgs.text(query, max_results=3)]
|
32 |
+
if results:
|
33 |
+
return "\n".join([r["body"] for r in results if r.get("body")])
|
34 |
+
else:
|
35 |
+
return "No results found."
|
36 |
|
37 |
+
# Smart Agent combining multiple sources
|
38 |
+
class SmartAgent:
|
39 |
+
def __init__(self):
|
40 |
+
service_context = ServiceContext.from_defaults(
|
41 |
+
llm=HuggingFaceLLM(model_name=llm_model_id, token=HF_TOKEN)
|
42 |
)
|
43 |
+
docs = [doc["text"] for doc in wiki_dataset]
|
44 |
+
self.index = VectorStoreIndex.from_documents(
|
45 |
+
[SimpleDirectoryReader.input_to_document(doc) for doc in docs],
|
46 |
+
service_context=service_context,
|
47 |
+
show_progress=True
|
48 |
+
)
|
49 |
+
self.query_engine = self.index.as_query_engine()
|
50 |
|
|
|
51 |
def __call__(self, question: str) -> str:
|
52 |
+
question_lower = question.lower()
|
53 |
+
|
54 |
+
# Use DuckDuckGo for recent events, dates, or temporal queries
|
55 |
+
if any(term in question_lower for term in ["current", "latest", "2024", "2025", "recent", "today", "president"]):
|
56 |
return duckduckgo_search(question)
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
# Check if Wikipedia page exists for topic
|
59 |
+
page = wiki_api.page(question)
|
60 |
+
if page.exists():
|
61 |
+
return page.summary[:1000] + "..."
|
62 |
+
|
63 |
+
# Fallback to indexed Wikipedia with RAG
|
64 |
+
try:
|
65 |
+
response = self.query_engine.query(question)
|
66 |
+
return str(response)
|
67 |
+
except Exception as e:
|
68 |
+
return f"LLM query error: {e}"
|
69 |
+
|
70 |
+
# Run and submit evaluation
|
71 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
72 |
+
space_id = os.getenv("SPACE_ID")
|
73 |
+
if profile:
|
74 |
+
username = f"{profile.username}"
|
75 |
+
else:
|
76 |
return "Please Login to Hugging Face with the button.", None
|
77 |
+
|
78 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
79 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
80 |
+
|
81 |
+
# Instantiate agent
|
82 |
+
agent = SmartAgent()
|
83 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
84 |
+
|
85 |
+
# Fetch questions
|
86 |
try:
|
87 |
+
questions_data = requests.get(questions_url).json()
|
88 |
except Exception as e:
|
89 |
+
return f"Error fetching questions: {e}", None
|
90 |
|
|
|
91 |
results_log, answers_payload = [], []
|
|
|
92 |
for item in questions_data:
|
93 |
+
task_id, question_text = item.get("task_id"), item.get("question")
|
94 |
+
answer = agent(question_text)
|
95 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
|
96 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
|
|
|
|
|
97 |
|
98 |
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
try:
|
101 |
+
result_data = requests.post(submit_url, json=submission_data).json()
|
102 |
+
final_status = (
|
103 |
+
f"Submission Successful!\n"
|
104 |
+
f"User: {result_data.get('username')}\n"
|
105 |
+
f"Overall Score: {result_data.get('score')}%\n"
|
106 |
+
f"({result_data.get('correct_count')}/{result_data.get('total_attempted')}) correct\n"
|
107 |
+
f"Message: {result_data.get('message')}"
|
108 |
+
)
|
109 |
+
results_df = pd.DataFrame(results_log)
|
110 |
+
return final_status, results_df
|
111 |
+
except Exception as e:
|
112 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
113 |
|
114 |
+
# Gradio interface setup
|
115 |
with gr.Blocks() as demo:
|
116 |
+
gr.Markdown("# 🚀 Smart Multi-Source Agent Evaluation")
|
117 |
gr.LoginButton()
|
118 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
119 |
+
status_output = gr.Textbox(label="Status & Results", lines=6, interactive=False)
|
120 |
+
results_table = gr.DataFrame(label="Agent Answers")
|
121 |
|
122 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
+
demo.launch(debug=True, share=False)
|