Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
@@ -5,30 +6,74 @@ import pandas as pd
|
|
| 5 |
from langchain_core.messages import HumanMessage
|
| 6 |
from veryfinal import build_graph
|
| 7 |
|
|
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
def __init__(self):
|
| 13 |
-
print("
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
state = {
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
config = {"configurable": {"thread_id": f"eval_{hash(question)}"}}
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
space_id = os.getenv("SPACE_ID")
|
|
|
|
| 32 |
if profile:
|
| 33 |
username = f"{profile.username}"
|
| 34 |
print(f"User logged in: {username}")
|
|
@@ -40,14 +85,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 40 |
questions_url = f"{api_url}/questions"
|
| 41 |
submit_url = f"{api_url}/submit"
|
| 42 |
|
|
|
|
| 43 |
try:
|
| 44 |
-
agent =
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Error instantiating agent: {e}")
|
| 47 |
return f"Error initializing agent: {e}", None
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
|
|
|
| 51 |
print(f"Fetching questions from: {questions_url}")
|
| 52 |
try:
|
| 53 |
response = requests.get(questions_url, timeout=15)
|
|
@@ -61,31 +111,49 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 61 |
print(f"Error fetching questions: {e}")
|
| 62 |
return f"Error fetching questions: {e}", None
|
| 63 |
|
|
|
|
| 64 |
results_log = []
|
| 65 |
answers_payload = []
|
| 66 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
| 67 |
-
|
|
|
|
| 68 |
task_id = item.get("task_id")
|
| 69 |
question_text = item.get("question")
|
|
|
|
| 70 |
if not task_id or question_text is None:
|
| 71 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 72 |
continue
|
|
|
|
|
|
|
|
|
|
| 73 |
try:
|
| 74 |
submitted_answer = agent(question_text)
|
| 75 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 76 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
|
|
|
| 78 |
print(f"Error running agent on task {task_id}: {e}")
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if not answers_payload:
|
| 82 |
print("Agent did not produce any answers to submit.")
|
| 83 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 84 |
|
|
|
|
| 85 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 86 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 87 |
print(status_update)
|
| 88 |
|
|
|
|
| 89 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 90 |
try:
|
| 91 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
@@ -107,24 +175,35 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 107 |
results_df = pd.DataFrame(results_log)
|
| 108 |
return status_message, results_df
|
| 109 |
|
|
|
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
-
gr.Markdown("#
|
| 112 |
gr.Markdown(
|
| 113 |
"""
|
| 114 |
**Instructions:**
|
| 115 |
-
1.
|
| 116 |
-
2.
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
**
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
"""
|
| 123 |
)
|
| 124 |
|
| 125 |
gr.LoginButton()
|
| 126 |
|
| 127 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 128 |
|
| 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)
|
|
@@ -135,5 +214,5 @@ with gr.Blocks() as demo:
|
|
| 135 |
)
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
print("\n" + "-"*30 + "
|
| 139 |
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
+
""" Enhanced Multi-LLM Agent Evaluation Runner with Agno Integration"""
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
|
|
|
| 6 |
from langchain_core.messages import HumanMessage
|
| 7 |
from veryfinal import build_graph
|
| 8 |
|
| 9 |
+
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
# --- Enhanced Agent Definition ---
|
| 13 |
+
class EnhancedMultiLLMAgent:
|
| 14 |
+
"""A multi-provider LangGraph agent with Agno-style reasoning capabilities."""
|
| 15 |
def __init__(self):
|
| 16 |
+
print("Enhanced Multi-LLM Agent with Agno Integration initialized.")
|
| 17 |
+
try:
|
| 18 |
+
self.graph = build_graph(provider="groq")
|
| 19 |
+
print("Enhanced Multi-LLM Graph built successfully.")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error building graph: {e}")
|
| 22 |
+
self.graph = None
|
| 23 |
|
| 24 |
def __call__(self, question: str) -> str:
|
| 25 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 26 |
+
|
| 27 |
+
if self.graph is None:
|
| 28 |
+
return "Error: Agent not properly initialized"
|
| 29 |
+
|
| 30 |
+
# CRITICAL FIX: Always pass the complete state expected by the graph
|
| 31 |
state = {
|
| 32 |
+
"messages": [HumanMessage(content=question)],
|
| 33 |
+
"query": question, # This was the critical missing field
|
| 34 |
+
"agent_type": "",
|
| 35 |
+
"final_answer": "",
|
| 36 |
+
"perf": {},
|
| 37 |
+
"agno_resp": "",
|
| 38 |
+
"tools_used": [],
|
| 39 |
+
"reasoning": "",
|
| 40 |
+
"confidence": ""
|
| 41 |
+
}
|
| 42 |
+
# CRITICAL FIX: Always provide the required config with thread_id
|
| 43 |
config = {"configurable": {"thread_id": f"eval_{hash(question)}"}}
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
result = self.graph.invoke(state, config)
|
| 47 |
+
|
| 48 |
+
# Handle different response formats
|
| 49 |
+
if isinstance(result, dict):
|
| 50 |
+
if 'messages' in result and result['messages']:
|
| 51 |
+
answer = result['messages'][-1].content
|
| 52 |
+
elif 'final_answer' in result:
|
| 53 |
+
answer = result['final_answer']
|
| 54 |
+
else:
|
| 55 |
+
answer = str(result)
|
| 56 |
+
else:
|
| 57 |
+
answer = str(result)
|
| 58 |
+
|
| 59 |
+
# Extract final answer if present
|
| 60 |
+
if "FINAL ANSWER:" in answer:
|
| 61 |
+
return answer.split("FINAL ANSWER:")[-1].strip()
|
| 62 |
+
else:
|
| 63 |
+
return answer.strip()
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
error_msg = f"Error: {str(e)}"
|
| 67 |
+
print(error_msg)
|
| 68 |
+
return error_msg
|
| 69 |
|
| 70 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 71 |
+
"""
|
| 72 |
+
Fetches all questions, runs the Enhanced Multi-LLM Agent on them,
|
| 73 |
+
submits all answers, and displays the results.
|
| 74 |
+
"""
|
| 75 |
space_id = os.getenv("SPACE_ID")
|
| 76 |
+
|
| 77 |
if profile:
|
| 78 |
username = f"{profile.username}"
|
| 79 |
print(f"User logged in: {username}")
|
|
|
|
| 85 |
questions_url = f"{api_url}/questions"
|
| 86 |
submit_url = f"{api_url}/submit"
|
| 87 |
|
| 88 |
+
# 1. Instantiate Agent
|
| 89 |
try:
|
| 90 |
+
agent = EnhancedMultiLLMAgent()
|
| 91 |
+
if agent.graph is None:
|
| 92 |
+
return "Error: Failed to initialize agent properly", None
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error instantiating agent: {e}")
|
| 95 |
return f"Error initializing agent: {e}", None
|
| 96 |
+
|
| 97 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "No space ID available"
|
| 98 |
+
print(f"Agent code URL: {agent_code}")
|
| 99 |
|
| 100 |
+
# 2. Fetch Questions
|
| 101 |
print(f"Fetching questions from: {questions_url}")
|
| 102 |
try:
|
| 103 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 111 |
print(f"Error fetching questions: {e}")
|
| 112 |
return f"Error fetching questions: {e}", None
|
| 113 |
|
| 114 |
+
# 3. Run your Agent
|
| 115 |
results_log = []
|
| 116 |
answers_payload = []
|
| 117 |
+
print(f"Running Enhanced Multi-LLM agent with Agno integration on {len(questions_data)} questions...")
|
| 118 |
+
|
| 119 |
+
for i, item in enumerate(questions_data):
|
| 120 |
task_id = item.get("task_id")
|
| 121 |
question_text = item.get("question")
|
| 122 |
+
|
| 123 |
if not task_id or question_text is None:
|
| 124 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 125 |
continue
|
| 126 |
+
|
| 127 |
+
print(f"Processing question {i+1}/{len(questions_data)}: {task_id}")
|
| 128 |
+
|
| 129 |
try:
|
| 130 |
submitted_answer = agent(question_text)
|
| 131 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 132 |
+
results_log.append({
|
| 133 |
+
"Task ID": task_id,
|
| 134 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
| 135 |
+
"Submitted Answer": submitted_answer[:200] + "..." if len(submitted_answer) > 200 else submitted_answer
|
| 136 |
+
})
|
| 137 |
except Exception as e:
|
| 138 |
+
error_msg = f"AGENT ERROR: {e}"
|
| 139 |
print(f"Error running agent on task {task_id}: {e}")
|
| 140 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": error_msg})
|
| 141 |
+
results_log.append({
|
| 142 |
+
"Task ID": task_id,
|
| 143 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
| 144 |
+
"Submitted Answer": error_msg
|
| 145 |
+
})
|
| 146 |
|
| 147 |
if not answers_payload:
|
| 148 |
print("Agent did not produce any answers to submit.")
|
| 149 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 150 |
|
| 151 |
+
# 4. Prepare Submission
|
| 152 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 153 |
+
status_update = f"Enhanced Multi-LLM Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 154 |
print(status_update)
|
| 155 |
|
| 156 |
+
# 5. Submit
|
| 157 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 158 |
try:
|
| 159 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 175 |
results_df = pd.DataFrame(results_log)
|
| 176 |
return status_message, results_df
|
| 177 |
|
| 178 |
+
# --- Build Gradio Interface using Blocks ---
|
| 179 |
with gr.Blocks() as demo:
|
| 180 |
+
gr.Markdown("# Enhanced Multi-LLM Agent with Agno Integration")
|
| 181 |
gr.Markdown(
|
| 182 |
"""
|
| 183 |
**Instructions:**
|
| 184 |
+
1. Log in to your Hugging Face account using the button below.
|
| 185 |
+
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 186 |
+
|
| 187 |
+
**Enhanced Agent Features:**
|
| 188 |
+
- **Multi-LLM Support**: Groq (Llama-3 8B/70B, DeepSeek), Google Gemini, NVIDIA NIM
|
| 189 |
+
- **Agno Integration**: Systematic reasoning with step-by-step analysis
|
| 190 |
+
- **Intelligent Routing**: Automatically selects best provider based on query complexity
|
| 191 |
+
- **Enhanced Tools**: Mathematical operations, web search, Wikipedia integration
|
| 192 |
+
- **Question-Answering**: Optimized for evaluation tasks with proper formatting
|
| 193 |
+
- **Error Handling**: Robust fallback mechanisms and comprehensive logging
|
| 194 |
+
|
| 195 |
+
**Routing Examples:**
|
| 196 |
+
- Standard: "What is the capital of France?" → Llama-3 8B
|
| 197 |
+
- Complex: "Analyze quantum computing principles" → Llama-3 70B
|
| 198 |
+
- Search: "Find information about Mercedes Sosa" → Search-Enhanced
|
| 199 |
+
- Agno: "agno llama-70: Systematic analysis of AI ethics" → Agno Llama-3 70B
|
| 200 |
+
- Provider-specific: "google: Explain machine learning" → Google Gemini
|
| 201 |
"""
|
| 202 |
)
|
| 203 |
|
| 204 |
gr.LoginButton()
|
| 205 |
|
| 206 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 207 |
|
| 208 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 209 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
|
| 214 |
)
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
print("\n" + "-"*30 + " Enhanced Multi-LLM Agent with Agno Starting " + "-"*30)
|
| 218 |
demo.launch(debug=True, share=False)
|