File size: 5,212 Bytes
10e9b7d eccf8e4 3c4371f 10e9b7d 6d169a1 8d9e499 c0a6618 6d169a1 c0a6618 8d9e499 c0a6618 e80aab9 3db6293 e80aab9 8d9e499 9a82dc6 80e8087 9a82dc6 9e76a0e 9a82dc6 9e76a0e 6d169a1 9e76a0e 9a82dc6 9e76a0e 9a82dc6 9e76a0e 9a82dc6 9e76a0e 9a82dc6 9e76a0e 9a82dc6 9e76a0e 9a82dc6 9e76a0e 9a82dc6 a38b536 9e76a0e a38b536 9e76a0e 162e437 9e76a0e a38b536 9e76a0e 9a82dc6 9e76a0e 80e8087 9a82dc6 a38b536 9e76a0e a38b536 9e76a0e a38b536 9a82dc6 a38b536 9e76a0e 3db234b 9e76a0e a38b536 9e76a0e 162e437 9e76a0e 9a82dc6 9e76a0e 9a82dc6 9e76a0e 9a82dc6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
import os
import gradio as gr
import requests
import pandas as pd
# --- LangChain & Dependency Imports ---
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
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# --- Agent Definition ---
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)
# The agent has ONLY ONE tool: web_search
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."),
]
# A simple, direct prompt
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}"
# --- Main Application Logic ---
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)
# --- Gradio Interface ---
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) |