Katya Beresneva commited on
Commit ·
b97b365
1
Parent(s): 9722a21
add Template Final Assignment
Browse files
agent.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
class KateState(AgentState):
|
| 3 |
+
task_id: str
|
| 4 |
+
question: str
|
| 5 |
+
file_name: str
|
| 6 |
+
|
| 7 |
+
KATE_AGENT_PROMPT = """
|
| 8 |
+
You are Kate's Advanced AI Assistant designed to solve complex tasks efficiently.
|
| 9 |
+
|
| 10 |
+
Key Principles:
|
| 11 |
+
1. Always think step-by-step before answering
|
| 12 |
+
2. Be extremely concise - only provide the exact answer needed
|
| 13 |
+
3. Use tools whenever possible
|
| 14 |
+
4. Never guess - if unsure, say "I don't know"
|
| 15 |
+
5. Follow the exact format: FINAL ANSWER: [your answer]
|
| 16 |
+
|
| 17 |
+
Special Capabilities:
|
| 18 |
+
- Advanced web search
|
| 19 |
+
- Multimedia analysis
|
| 20 |
+
- Data processing
|
| 21 |
+
- Code execution
|
| 22 |
+
|
| 23 |
+
Examples:
|
| 24 |
+
QUESTION: Capital of France? FINAL ANSWER: Paris
|
| 25 |
+
QUESTION: 2+2? FINAL ANSWER: 4
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
class KateMultiModalAgent:
|
| 29 |
+
def __init__(self, model_name: str | None = None):
|
| 30 |
+
self.model_name = model_name or AGENT_MODEL_NAME
|
| 31 |
+
self.llm = self._get_llm()
|
| 32 |
+
self.tools = self._get_tools()
|
| 33 |
+
self.agent = self._create_agent()
|
| 34 |
+
|
| 35 |
+
def _create_agent(self):
|
| 36 |
+
return create_react_agent(
|
| 37 |
+
self.llm,
|
| 38 |
+
tools=self.tools,
|
| 39 |
+
state_schema=KateState,
|
| 40 |
+
state_modifier=KATE_AGENT_PROMPT,
|
| 41 |
+
checkpointer=MemorySaver(),
|
| 42 |
+
max_iterations=10
|
| 43 |
+
)
|
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from agent import KateMultiModalAgent
|
| 7 |
+
|
| 8 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
+
AGENT_NAME = "Kate's Advanced Agent"
|
| 10 |
+
|
| 11 |
+
async def run_agent(
|
| 12 |
+
agt: KateMultiModalAgent,
|
| 13 |
+
item: dict
|
| 14 |
+
) -> str | None:
|
| 15 |
+
try:
|
| 16 |
+
task_id = item.get("task_id")
|
| 17 |
+
question_text = item.get("question")
|
| 18 |
+
file_name = item.get("file_name", None)
|
| 19 |
+
|
| 20 |
+
if not task_id or question_text is None:
|
| 21 |
+
print(f"Skipping invalid item: {item}")
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
print(f"Processing task {task_id}...")
|
| 25 |
+
submitted_answer = await agt(task_id, question_text, file_name)
|
| 26 |
+
return {
|
| 27 |
+
"task_id": task_id,
|
| 28 |
+
"question": question_text,
|
| 29 |
+
"submitted_answer": submitted_answer
|
| 30 |
+
}
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error processing task {item.get('task_id')}: {str(e)}")
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 36 |
+
if not profile:
|
| 37 |
+
return "Please login with your Hugging Face account (Kate Berasneva)", None
|
| 38 |
+
|
| 39 |
+
username = profile.username
|
| 40 |
+
print(f"Kate's Agent running for user: {username}")
|
| 41 |
+
|
| 42 |
+
with gr.Blocks(title="Kate's Agent Evaluation Runner") as demo:
|
| 43 |
+
gr.Markdown("# Kate's Advanced Agent Evaluation")
|
| 44 |
+
gr.Markdown("""
|
| 45 |
+
**Welcome to Kate Berasneva's Agent Solution!**
|
| 46 |
+
|
| 47 |
+
This enhanced agent features:
|
| 48 |
+
- Improved error handling
|
| 49 |
+
- Better tool integration
|
| 50 |
+
- Custom prompt engineering
|
| 51 |
+
- Efficient task processing
|
| 52 |
+
|
| 53 |
+
1. Login with your HF account
|
| 54 |
+
2. Click Run Evaluation
|
| 55 |
+
3. View your results!
|
| 56 |
+
""")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
langchain-experimental
|
| 4 |
+
pydantic==2.0
|
| 5 |
+
python-dotenv
|
tools.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@tool("kate-enhanced-search", parse_docstring=True)
|
| 2 |
+
async def kate_search(
|
| 3 |
+
query: str,
|
| 4 |
+
max_results: int = 3
|
| 5 |
+
) -> dict:
|
| 6 |
+
"""
|
| 7 |
+
Kate's enhanced search combining multiple sources
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
query: Search query
|
| 11 |
+
max_results: Max results to return
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
Combined results from Tavily, Wikipedia and Arxiv
|
| 15 |
+
"""
|
| 16 |
+
pass
|
| 17 |
+
|
| 18 |
+
@tool("analyze-excel-enhanced", parse_docstring=True)
|
| 19 |
+
async def analyze_excel_enhanced(state: dict, file_path: str) -> str:
|
| 20 |
+
"""
|
| 21 |
+
Kate's enhanced Excel analyzer with better data processing
|
| 22 |
+
|
| 23 |
+
Features:
|
| 24 |
+
- Improved data validation
|
| 25 |
+
- Support for larger files
|
| 26 |
+
- Better error messages
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
pass
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"ERROR: {str(e)}"
|