|
import os |
|
import gradio as gr |
|
import requests |
|
import pandas as pd |
|
|
|
|
|
from groq import Groq |
|
from langchain_groq import ChatGroq |
|
from langchain.agents import AgentExecutor, create_tool_calling_agent |
|
from langchain_tavily import TavilySearch |
|
from langchain_core.prompts import ChatPromptTemplate |
|
from langchain.tools import Tool |
|
|
|
|
|
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
|
|
|
class SimpleAgent: |
|
def __init__(self, groq_api_key: str, tavily_api_key: str): |
|
print("Initializing SimpleAgent...") |
|
self.llm = ChatGroq(model_name="llama3-70b-8192", groq_api_key=groq_api_key, temperature=0.0) |
|
|
|
|
|
self.tools = [ |
|
TavilySearch(name="web_search", max_results=3, tavily_api_key=tavily_api_key, description="A search engine for finding up-to-date information on the internet."), |
|
] |
|
|
|
|
|
prompt = ChatPromptTemplate.from_messages([ |
|
("system", ( |
|
"You are a helpful assistant. You have access to one tool: a web search engine. " |
|
"Use it when you need to find current information or facts. " |
|
"After using the tool, provide ONLY the final, concise answer." |
|
)), |
|
("human", "{input}"), |
|
("placeholder", "{agent_scratchpad}"), |
|
]) |
|
|
|
agent = create_tool_calling_agent(self.llm, self.tools, prompt) |
|
self.agent_executor = AgentExecutor(agent=agent, tools=self.tools, verbose=True, handle_parsing_errors=True) |
|
print("SimpleAgent Initialized.") |
|
|
|
def __call__(self, question: str) -> str: |
|
print(f"Agent received question: {question[:50]}...") |
|
try: |
|
response = self.agent_executor.invoke({"input": question}) |
|
return response.get("output", "Agent failed to produce an answer.") |
|
except Exception as e: |
|
return f"Agent execution failed with an error: {e}" |
|
|
|
|
|
def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
space_id = os.getenv("SPACE_ID") |
|
if not profile: return "Please Login to Hugging Face with the button.", None |
|
username = profile.username |
|
print(f"User logged in: {username}") |
|
|
|
try: |
|
groq_api_key = os.getenv("GROQ_API_KEY") |
|
tavily_api_key = os.getenv("TAVILY_API_KEY") |
|
if not all([groq_api_key, tavily_api_key]): raise ValueError("GROQ or TAVILY API key is missing.") |
|
agent = SimpleAgent(groq_api_key=groq_api_key, tavily_api_key=tavily_api_key) |
|
except Exception as e: return f"Error initializing agent: {e}", None |
|
|
|
questions_url = f"{DEFAULT_API_URL}/questions" |
|
try: |
|
response = requests.get(questions_url, timeout=20) |
|
response.raise_for_status() |
|
questions_data = response.json() |
|
except Exception as e: return f"Error fetching questions: {e}", None |
|
|
|
results_log, answers_payload = [], [] |
|
for item in questions_data: |
|
task_id, q_text = item.get("task_id"), item.get("question") |
|
if not task_id or not q_text: continue |
|
answer = agent(question=q_text) |
|
answers_payload.append({"task_id": task_id, "submitted_answer": answer}) |
|
results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": answer}) |
|
|
|
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" |
|
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload} |
|
submit_url = f"{DEFAULT_API_URL}/submit" |
|
try: |
|
response = requests.post(submit_url, json=submission_data, timeout=300) |
|
response.raise_for_status() |
|
result_data = response.json() |
|
final_status = (f"Submission Successful!\nUser: {result_data.get('username')}\n" |
|
f"Overall Score: {result_data.get('score', 'N/A')}% " |
|
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n" |
|
f"Message: {result_data.get('message', 'No message received.')}") |
|
return final_status, pd.DataFrame(results_log) |
|
except Exception as e: return f"Submission Failed: {e}", pd.DataFrame(results_log) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Simple Agent Runner (Web Search Only)") |
|
gr.Markdown("This agent can only search the web. Let's establish a stable baseline.") |
|
gr.LoginButton() |
|
run_button = gr.Button("Run Evaluation & Submit All Answers") |
|
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False) |
|
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True) |
|
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table]) |
|
|
|
if __name__ == "__main__": |
|
print("\n" + "-"*30 + " App Starting " + "-"*30) |
|
for key in ["GROQ_API_KEY", "TAVILY_API_KEY"]: |
|
print(f"✅ {key} secret is set." if os.getenv(key) else f"⚠️ WARNING: {key} secret is not set.") |
|
print("-"*(60 + len(" App Starting ")) + "\n") |
|
demo.launch(debug=True, share=False) |