File size: 2,068 Bytes
e8b40b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
# clients/client_requests.py
import argparse, base64, json, os
import requests

def call_describe_raw(space_base_url: str, image_path: str, text: str, max_new_tokens: int = 256, temperature: float = 0.7):
    """

    Golpea el endpoint REST puro /api/describe_raw (multipart/form-data).

    Ej.: space_base_url = "https://<usuario>-<space>.hf.space"

    """
    url = space_base_url.rstrip("/") + "/api/describe_raw"
    with open(image_path, "rb") as f:
        files = {"image": (os.path.basename(image_path), f, "application/octet-stream")}
        data = {"text": text, "max_new_tokens": str(max_new_tokens), "temperature": str(temperature)}
        r = requests.post(url, files=files, data=data, timeout=600)
    r.raise_for_status()
    return r.json()["text"]

def call_predict_legacy(space_base_url: str, image_path: str, text: str, max_new_tokens: int = 256, temperature: float = 0.7):
    """

    Alternativa: endpoint estándar /api/predict/describe (formato Gradio).

    Enviamos la imagen como URL de carga previa no disponible aquí, así que

    preferimos /api/describe_raw. Se incluye por compatibilidad.

    """
    url = space_base_url.rstrip("/") + "/api/predict/describe"
    # Para /api/predict lo normal es usar gradio_client que gestiona subidas;
    # con requests puro, tendrías que hacer upload previo a /upload y referenciar la URL.
    # Lo dejamos como placeholder educativo.
    raise NotImplementedError("Usa call_describe_raw para HTTP directo.")

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--space-url", required=True, help="https://<usuario>-<space>.hf.space")
    ap.add_argument("--image", required=True)
    ap.add_argument("--text", default="Describe la imagen con detalle.")
    ap.add_argument("--max-new-tokens", type=int, default=256)
    ap.add_argument("--temperature", type=float, default=0.7)
    args = ap.parse_args()
    out = call_describe_raw(args.space_url, args.image, args.text, args.max_new_tokens, args.temperature)
    print(out)