ChockqOteewy's picture
Update app.py
4c42a76 verified
raw
history blame
4.61 kB
import os
import gradio as gr
import requests
import pandas as pd
from datasets import load_dataset
from duckduckgo_search import DDGS
from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
from huggingface_hub import InferenceClient
import wikipediaapi
# Constants
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
HF_TOKEN = os.environ.get("HF_TOKEN")
# Advanced LLM via Hugging Face Inference API
llm_model_id = "deepseek-ai/DeepSeek-R1"
hf_client = InferenceClient(llm_model_id, token=HF_TOKEN)
# Wikipedia API setup
wiki_api = wikipediaapi.Wikipedia('en')
# Load Wikipedia dataset from Hugging Face
wiki_dataset = load_dataset(
"wikipedia", "20220301.en", split="train[:10000]", trust_remote_code=True
)
# DuckDuckGo search function
def duckduckgo_search(query):
with DDGS() as ddgs:
results = [r for r in ddgs.text(query, max_results=3)]
if results:
return "\n".join([r["body"] for r in results if r.get("body")])
else:
return "No results found."
# Smart Agent combining multiple sources
class SmartAgent:
def __init__(self):
service_context = ServiceContext.from_defaults(
llm=HuggingFaceLLM(model_name=llm_model_id, token=HF_TOKEN)
)
docs = [doc["text"] for doc in wiki_dataset]
self.index = VectorStoreIndex.from_documents(
[SimpleDirectoryReader.input_to_document(doc) for doc in docs],
service_context=service_context,
show_progress=True
)
self.query_engine = self.index.as_query_engine()
def __call__(self, question: str) -> str:
question_lower = question.lower()
# Use DuckDuckGo for recent events, dates, or temporal queries
if any(term in question_lower for term in ["current", "latest", "2024", "2025", "recent", "today", "president"]):
return duckduckgo_search(question)
# Check if Wikipedia page exists for topic
page = wiki_api.page(question)
if page.exists():
return page.summary[:1000] + "..."
# Fallback to indexed Wikipedia with RAG
try:
response = self.query_engine.query(question)
return str(response)
except Exception as e:
return f"LLM query error: {e}"
# Run and submit evaluation
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID")
if profile:
username = f"{profile.username}"
else:
return "Please Login to Hugging Face with the button.", None
questions_url = f"{DEFAULT_API_URL}/questions"
submit_url = f"{DEFAULT_API_URL}/submit"
# Instantiate agent
agent = SmartAgent()
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
# Fetch questions
try:
questions_data = requests.get(questions_url).json()
except Exception as e:
return f"Error fetching questions: {e}", None
results_log, answers_payload = [], []
for item in questions_data:
task_id, question_text = item.get("task_id"), item.get("question")
answer = agent(question_text)
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
try:
result_data = requests.post(submit_url, json=submission_data).json()
final_status = (
f"Submission Successful!\n"
f"User: {result_data.get('username')}\n"
f"Overall Score: {result_data.get('score')}%\n"
f"({result_data.get('correct_count')}/{result_data.get('total_attempted')}) correct\n"
f"Message: {result_data.get('message')}"
)
results_df = pd.DataFrame(results_log)
return final_status, results_df
except Exception as e:
return f"Submission Failed: {e}", pd.DataFrame(results_log)
# Gradio interface setup
with gr.Blocks() as demo:
gr.Markdown("# 🚀 Smart Multi-Source Agent Evaluation")
gr.LoginButton()
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Status & Results", lines=6, interactive=False)
results_table = gr.DataFrame(label="Agent Answers")
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
if __name__ == "__main__":
demo.launch(debug=True, share=False)