Spaces:
Sleeping
Sleeping
init
Browse files- app.py +27 -0
- requirements.txt +3 -1
- tools.py +68 -0
app.py
CHANGED
@@ -3,13 +3,35 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
# --- Basic Agent Definition ---
|
12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
class BasicAgent:
|
14 |
def __init__(self):
|
15 |
print("BasicAgent initialized.")
|
@@ -19,6 +41,11 @@ class BasicAgent:
|
|
19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
20 |
return fixed_answer
|
21 |
|
|
|
|
|
|
|
|
|
|
|
22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
23 |
"""
|
24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from langgraph.prebuilt import LLMNode,ToolNode
|
7 |
+
from tools import web_search, parse_excel, ocr_image
|
8 |
|
9 |
+
from typing import TypedDict, Annotated
|
10 |
+
|
11 |
+
from langchain.chat_models import ChatOpenAI
|
12 |
+
from langgraph.graph import StateGraph, START, END
|
13 |
+
from langgraph.graph.message import add_messages
|
14 |
+
|
15 |
+
# Create a ToolNode that knows about your web_search function
|
16 |
+
search_node = ToolNode([web_search])
|
17 |
+
|
18 |
+
excel_tool_node = ToolNode([parse_excel])
|
19 |
+
|
20 |
+
image_tool_node = ToolNode([ocr_image])
|
21 |
# (Keep Constants as is)
|
22 |
# --- Constants ---
|
23 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
24 |
|
25 |
# --- Basic Agent Definition ---
|
26 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
class BasicAgent:
|
36 |
def __init__(self):
|
37 |
print("BasicAgent initialized.")
|
|
|
41 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
42 |
return fixed_answer
|
43 |
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
50 |
"""
|
51 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
requirements.txt
CHANGED
@@ -1,2 +1,4 @@
|
|
1 |
gradio
|
2 |
-
requests
|
|
|
|
|
|
1 |
gradio
|
2 |
+
requests
|
3 |
+
pillow
|
4 |
+
pytesseract
|
tools.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.tools import tool
|
2 |
+
from langchain.utilities import DuckDuckGoSearchRun
|
3 |
+
import pandas as pd
|
4 |
+
@tool
|
5 |
+
def web_search(query: str) -> str:
|
6 |
+
ddg = DuckDuckGoSearchRun()
|
7 |
+
return ddg.run(query)
|
8 |
+
|
9 |
+
|
10 |
+
@tool
|
11 |
+
def parse_excel(path: str, sheet_name: str = None) -> str:
|
12 |
+
|
13 |
+
"""
|
14 |
+
Read in an Excel file at `path`, optionally select a sheet by name (or default to the first sheet),
|
15 |
+
then convert the DataFrame to a JSON-like string. Return that text so the LLM can reason over it.
|
16 |
+
|
17 |
+
Example return value (collapsed):
|
18 |
+
"[{'Name': 'Alice', 'Score': 95}, {'Name': 'Bob', 'Score': 88}, ...]"
|
19 |
+
"""
|
20 |
+
# 1. Load the Excel workbook
|
21 |
+
try:
|
22 |
+
xls = pd.ExcelFile(path)
|
23 |
+
except FileNotFoundError:
|
24 |
+
return f"Error: could not find file at {path}."
|
25 |
+
|
26 |
+
# 2. Choose the sheet
|
27 |
+
if sheet_name and sheet_name in xls.sheet_names:
|
28 |
+
df = pd.read_excel(xls, sheet_name=sheet_name)
|
29 |
+
else:
|
30 |
+
# default to first sheet
|
31 |
+
df = pd.read_excel(xls, sheet_name=xls.sheet_names[0])
|
32 |
+
|
33 |
+
# 3. Option A: convert to JSON
|
34 |
+
records = df.to_dict(orient="records")
|
35 |
+
return str(records)
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
# tools.py
|
40 |
+
|
41 |
+
from pathlib import Path
|
42 |
+
from PIL import Image
|
43 |
+
import pytesseract
|
44 |
+
|
45 |
+
|
46 |
+
@tool
|
47 |
+
def ocr_image(path: str) -> str:
|
48 |
+
"""
|
49 |
+
Run OCR on the image at `path` and return the extracted text.
|
50 |
+
- Expects that Tesseract is installed on the host machine.
|
51 |
+
- If the file is missing or unreadable, returns an error string.
|
52 |
+
"""
|
53 |
+
file = Path(path)
|
54 |
+
if not file.exists():
|
55 |
+
return f"Error: could not find image at {path}"
|
56 |
+
try:
|
57 |
+
# Open image via PIL
|
58 |
+
img = Image.open(file)
|
59 |
+
except Exception as e:
|
60 |
+
return f"Error: could not open image: {e}"
|
61 |
+
|
62 |
+
try:
|
63 |
+
# Run pytesseract OCR
|
64 |
+
text = pytesseract.image_to_string(img)
|
65 |
+
except Exception as e:
|
66 |
+
return f"Error: OCR failed: {e}"
|
67 |
+
|
68 |
+
return text.strip() or "(no visible text detected)"
|