Spaces:
Runtime error
Runtime error
File size: 4,041 Bytes
80328ae 1a6274b 80328ae 1a6274b e3debcc 80328ae 1a6274b 80328ae 1a6274b f61e168 e3debcc f61e168 e3debcc 1a6274b e3debcc 1a6274b f61e168 1a6274b e3debcc 1a6274b f61e168 1a6274b 80328ae 1a6274b 80328ae 1a6274b 80328ae 1a6274b e3debcc 1a6274b 4b4ec32 1a6274b 4b4ec32 1a6274b e3debcc 1a6274b 80328ae 1a6274b e3debcc 4b4ec32 1a6274b 80328ae 1a6274b e3debcc |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import os
import gradio as gr
import requests
import pandas as pd
from crewai import Crew, Process
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Constants
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# Import your CrewAI components (assuming they're in separate files)
from agents import news_researcher, news_writer
from tasks import research_task, write_task
class CrewAIAgent:
def __init__(self):
print("Initializing CrewAI agents...")
self.crew = Crew(
agents=[news_researcher, news_writer],
tasks=[research_task, write_task],
process=Process.sequential,
verbose=True
)
print("CrewAI agents initialized.")
def __call__(self, question: str) -> str:
print(f"Processing question: {question[:50]}...")
try:
# Execute the crew with the question as input
result = self.crew.kickoff(inputs={'topic': question})
print(f"Generated answer: {result[:100]}...")
return str(result)
except Exception as e:
print(f"CrewAI error: {e}")
return f"CrewAI Error: {str(e)}"
def run_and_submit_all(profile: gr.OAuthProfile | None):
if not profile:
return "Please log in with Hugging Face first.", None
# Initialize agent
try:
agent = CrewAIAgent()
except Exception as e:
return f"Agent initialization failed: {e}", None
# Fetch questions
try:
response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
questions_data = response.json()
if not questions_data:
return "No questions available.", None
except Exception as e:
return f"Failed to fetch questions: {e}", None
# Process questions
results = []
answers = []
for item in questions_data:
task_id = item.get("task_id")
question = item.get("question")
if not task_id or not question:
continue
try:
answer = agent(question)
answers.append({"task_id": task_id, "submitted_answer": answer})
results.append({
"Task ID": task_id,
"Question": question[:100] + "..." if len(question) > 100 else question,
"Answer": answer[:100] + "..." if len(answer) > 100 else answer
})
except Exception as e:
results.append({
"Task ID": task_id,
"Question": question,
"Answer": f"Error: {str(e)}"
})
# Submit answers
try:
submission = {
"username": profile.username,
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}",
"answers": answers
}
response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
result = response.json()
return (
f"✅ Submitted {len(answers)} answers\n"
f"📊 Score: {result.get('score', 'N/A')}%\n"
f"🔢 Correct: {result.get('correct_count', 0)}/{len(answers)}\n"
f"🤖 Using CrewAI agents",
pd.DataFrame(results)
)
except Exception as e:
return f"Submission failed: {e}", pd.DataFrame(results)
# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🚀 CrewAI Evaluation Runner")
gr.Markdown("""
This combines CrewAI agents with the evaluation framework.
The agents will research and write answers to evaluation questions.
""")
gr.LoginButton()
with gr.Row():
run_btn = gr.Button("Run Evaluation", variant="primary")
with gr.Row():
status = gr.Textbox(label="Status", interactive=False)
results = gr.DataFrame(label="Results", wrap=True)
run_btn.click(
fn=run_and_submit_all,
outputs=[status, results]
)
if __name__ == "__main__":
demo.launch() |