Rashmi0801's picture
Update app.py
e3debcc verified
raw
history blame
4.47 kB
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"
# CrewAI components (would normally be in separate files)
class ResearchAgent:
def __init__(self):
self.role = "Researcher"
self.goal = "Research information thoroughly"
def research(self, question):
# Implement research logic here
return f"Researched information about: {question}"
class WritingAgent:
def __init__(self):
self.role = "Writer"
self.goal = "Write clear and accurate answers"
def write(self, research_data):
# Implement writing logic here
return f"Comprehensive answer based on: {research_data}"
# Enhanced Agent Definition
class CrewAIAgent:
def __init__(self):
print("Initializing CrewAI agents...")
self.researcher = ResearchAgent()
self.writer = WritingAgent()
print("CrewAI agents initialized.")
def __call__(self, question: str) -> str:
print(f"Processing question: {question[:50]}...")
# Create and execute crew
try:
research_data = self.researcher.research(question)
final_answer = self.writer.write(research_data)
print(f"Generated answer: {final_answer[:100]}...")
return final_answer
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() # Using CrewAI instead of BasicAgent
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()