# 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://-.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://-.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)