Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,55 @@
|
|
| 1 |
-
api_key = "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"
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
-
from huggingface_hub import InferenceClient, login
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
import pandas as pd
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
# Constants
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
-
MODEL_NAME = "meta-llama/llama-4-maverick-17b-128e-instruct"
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
class
|
| 19 |
def __init__(self):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
<|start_header_id|>system<|end_header_id|>
|
| 37 |
-
You are an AI assistant that provides accurate and concise answers to questions.
|
| 38 |
-
Be factual and respond with just the answer unless asked to elaborate.
|
| 39 |
-
<|eot_id|>
|
| 40 |
-
<|start_header_id|>user<|end_header_id|>
|
| 41 |
-
{question}
|
| 42 |
-
<|eot_id|>
|
| 43 |
-
<|start_header_id|>assistant<|end_header_id|>"""
|
| 44 |
-
|
| 45 |
-
response = self.client.text_generation(
|
| 46 |
-
prompt,
|
| 47 |
-
max_new_tokens=256,
|
| 48 |
-
temperature=0.7,
|
| 49 |
-
do_sample=True,
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# Clean up the response
|
| 53 |
-
answer = response.split("<|eot_id|>")[0].strip()
|
| 54 |
-
print(f"Generated answer: {answer[:200]}...")
|
| 55 |
-
return answer
|
| 56 |
except Exception as e:
|
| 57 |
-
print(f"
|
| 58 |
-
return f"Error: {str(e)}"
|
| 59 |
-
|
| 60 |
-
# Authentication
|
| 61 |
-
try:
|
| 62 |
-
login(token=os.getenv("HUGGINGFACE_TOKEN"))
|
| 63 |
-
except Exception as e:
|
| 64 |
-
print(f"Authentication error: {e}")
|
| 65 |
|
| 66 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 67 |
if not profile:
|
|
@@ -69,7 +57,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 69 |
|
| 70 |
# Initialize agent
|
| 71 |
try:
|
| 72 |
-
agent =
|
| 73 |
except Exception as e:
|
| 74 |
return f"Agent initialization failed: {e}", None
|
| 75 |
|
|
@@ -85,7 +73,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 85 |
# Process questions
|
| 86 |
results = []
|
| 87 |
answers = []
|
| 88 |
-
for
|
| 89 |
task_id = item.get("task_id")
|
| 90 |
question = item.get("question")
|
| 91 |
if not task_id or not question:
|
|
@@ -120,7 +108,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 120 |
f"✅ Submitted {len(answers)} answers\n"
|
| 121 |
f"📊 Score: {result.get('score', 'N/A')}%\n"
|
| 122 |
f"🔢 Correct: {result.get('correct_count', 0)}/{len(answers)}\n"
|
| 123 |
-
f"🤖
|
| 124 |
pd.DataFrame(results)
|
| 125 |
)
|
| 126 |
except Exception as e:
|
|
@@ -128,8 +116,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 128 |
|
| 129 |
# Gradio Interface
|
| 130 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 131 |
-
gr.Markdown("#
|
| 132 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
gr.LoginButton()
|
| 135 |
|
|
@@ -146,6 +137,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 146 |
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
| 149 |
-
demo.launch()
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from crewai import Crew, Process
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
# Constants
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 13 |
|
| 14 |
+
# CrewAI components (would normally be in separate files)
|
| 15 |
+
class ResearchAgent:
|
| 16 |
def __init__(self):
|
| 17 |
+
self.role = "Researcher"
|
| 18 |
+
self.goal = "Research information thoroughly"
|
| 19 |
+
|
| 20 |
+
def research(self, question):
|
| 21 |
+
# Implement research logic here
|
| 22 |
+
return f"Researched information about: {question}"
|
| 23 |
+
|
| 24 |
+
class WritingAgent:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
self.role = "Writer"
|
| 27 |
+
self.goal = "Write clear and accurate answers"
|
| 28 |
+
|
| 29 |
+
def write(self, research_data):
|
| 30 |
+
# Implement writing logic here
|
| 31 |
+
return f"Comprehensive answer based on: {research_data}"
|
| 32 |
+
|
| 33 |
+
# Enhanced Agent Definition
|
| 34 |
+
class CrewAIAgent:
|
| 35 |
+
def __init__(self):
|
| 36 |
+
print("Initializing CrewAI agents...")
|
| 37 |
+
self.researcher = ResearchAgent()
|
| 38 |
+
self.writer = WritingAgent()
|
| 39 |
+
print("CrewAI agents initialized.")
|
| 40 |
|
| 41 |
def __call__(self, question: str) -> str:
|
| 42 |
+
print(f"Processing question: {question[:50]}...")
|
| 43 |
+
|
| 44 |
+
# Create and execute crew
|
| 45 |
try:
|
| 46 |
+
research_data = self.researcher.research(question)
|
| 47 |
+
final_answer = self.writer.write(research_data)
|
| 48 |
+
print(f"Generated answer: {final_answer[:100]}...")
|
| 49 |
+
return final_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
+
print(f"CrewAI error: {e}")
|
| 52 |
+
return f"CrewAI Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 55 |
if not profile:
|
|
|
|
| 57 |
|
| 58 |
# Initialize agent
|
| 59 |
try:
|
| 60 |
+
agent = CrewAIAgent() # Using CrewAI instead of BasicAgent
|
| 61 |
except Exception as e:
|
| 62 |
return f"Agent initialization failed: {e}", None
|
| 63 |
|
|
|
|
| 73 |
# Process questions
|
| 74 |
results = []
|
| 75 |
answers = []
|
| 76 |
+
for item in questions_data:
|
| 77 |
task_id = item.get("task_id")
|
| 78 |
question = item.get("question")
|
| 79 |
if not task_id or not question:
|
|
|
|
| 108 |
f"✅ Submitted {len(answers)} answers\n"
|
| 109 |
f"📊 Score: {result.get('score', 'N/A')}%\n"
|
| 110 |
f"🔢 Correct: {result.get('correct_count', 0)}/{len(answers)}\n"
|
| 111 |
+
f"🤖 Using CrewAI agents",
|
| 112 |
pd.DataFrame(results)
|
| 113 |
)
|
| 114 |
except Exception as e:
|
|
|
|
| 116 |
|
| 117 |
# Gradio Interface
|
| 118 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 119 |
+
gr.Markdown("# 🚀 CrewAI Evaluation Runner")
|
| 120 |
+
gr.Markdown("""
|
| 121 |
+
This combines CrewAI agents with the evaluation framework.
|
| 122 |
+
The agents will research and write answers to evaluation questions.
|
| 123 |
+
""")
|
| 124 |
|
| 125 |
gr.LoginButton()
|
| 126 |
|
|
|
|
| 137 |
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
+
demo.launch()
|
|
|
|
|
|