File size: 1,235 Bytes
bf45da8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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