Spaces:
Runtime error
Runtime error
| 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() |