masterllm / utilities /classify.py
redhairedshanks1's picture
Upload 23 files
bf45da8
import os
import requests
CLASSIFY_API = "https://point9-classify.hf.space/api/classify" # Replace with your space URL
def classify_remote(state):
filename = state.get("filename")
text = state.get("text")
data = {}
if text is not None:
data["text"] = text
if filename is not None:
data["filename"] = filename
if "start_page" in state:
data["start_page"] = state["start_page"]
if "end_page" in state:
data["end_page"] = state["end_page"]
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
path = state.get("temp_files", {}).get(filename)
if path:
with open(path, "rb") as f:
files = {"file": (filename, f, "application/pdf")}
resp = requests.post(CLASSIFY_API, files=files, data=data, headers=headers)
else:
if "text" not in data:
raise ValueError("classify_remote requires at least one of: file or text in state")
resp = requests.post(CLASSIFY_API, data=data, headers=headers)
if resp.status_code != 200:
raise RuntimeError(f"Classify API failed: {resp.text}")
state["classification"] = resp.json()
return state