VeuReu commited on
Commit
f35e105
·
verified ·
1 Parent(s): cf7b953

Delete scripts/remote_clients.py

Browse files
Files changed (1) hide show
  1. scripts/remote_clients.py +0 -78
scripts/remote_clients.py DELETED
@@ -1,78 +0,0 @@
1
- # remote_clients.py — clientes para Spaces remotos (Gradio/HTTP)
2
- from __future__ import annotations
3
- from typing import Any, Dict, List, Optional
4
- import os, json, requests
5
- from tenacity import retry, stop_after_attempt, wait_exponential
6
-
7
- try:
8
- from gradio_client import Client as GradioClient
9
- except Exception:
10
- GradioClient = None # type: ignore
11
-
12
- class BaseRemoteClient:
13
- def __init__(self, base_url: str, use_gradio: bool = True, hf_token: Optional[str] = None, timeout: int = 180):
14
- self.base_url = base_url.rstrip("/")
15
- self.use_gradio = use_gradio and GradioClient is not None
16
- self.hf_token = hf_token or os.getenv("HF_TOKEN")
17
- self.timeout = timeout
18
- self._client = None
19
- if self.use_gradio:
20
- headers = {"Authorization": f"Bearer {self.hf_token}"} if self.hf_token else None
21
- self._client = GradioClient(self.base_url, hf_token=self.hf_token, headers=headers)
22
-
23
- @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=8))
24
- def _post_json(self, route: str, payload: Dict[str, Any]) -> Dict[str, Any]:
25
- url = f"{self.base_url}{route}"
26
- headers = {"Authorization": f"Bearer {self.hf_token}"} if self.hf_token else {}
27
- r = requests.post(url, json=payload, headers=headers, timeout=self.timeout)
28
- r.raise_for_status()
29
- return r.json()
30
-
31
- class InstructClient(BaseRemoteClient):
32
- def generate(self, prompt: str, system: Optional[str] = None, **kwargs) -> str:
33
- if self.use_gradio and self._client:
34
- out = self._client.predict(prompt, api_name="/predict")
35
- return str(out)
36
- data = {"prompt": prompt, "system": system, **kwargs}
37
- res = self._post_json("/generate", data)
38
- return res.get("text", "")
39
-
40
- class VisionClient(BaseRemoteClient):
41
- def describe(self, image_paths: List[str], context: Optional[Dict[str, Any]] = None, **kwargs) -> List[str]:
42
- if self.use_gradio and self._client:
43
- out = self._client.predict(image_paths, json.dumps(context or {}), api_name="/predict")
44
- if isinstance(out, str):
45
- try:
46
- return json.loads(out)
47
- except Exception:
48
- return [out]
49
- return list(out)
50
- data = {"images": image_paths, "context": context or {}, **kwargs}
51
- res = self._post_json("/describe", data)
52
- return res.get("descriptions", [])
53
-
54
- class ToolsClient(BaseRemoteClient):
55
- def chat(self, messages: List[Dict[str, str]], tools: Optional[List[Dict[str, Any]]] = None, **kwargs) -> Dict[str, Any]:
56
- if self.use_gradio and self._client:
57
- out = self._client.predict(json.dumps(messages), json.dumps(tools or []), api_name="/predict")
58
- if isinstance(out, str):
59
- try:
60
- return json.loads(out)
61
- except Exception:
62
- return {"text": out}
63
- return out
64
- data = {"messages": messages, "tools": tools or [], **kwargs}
65
- return self._post_json("/chat", data)
66
-
67
- class ASRClient(BaseRemoteClient):
68
- def transcribe(self, audio_path: str, **kwargs) -> Dict[str, Any]:
69
- if self.use_gradio and self._client:
70
- out = self._client.predict(audio_path, api_name="/predict")
71
- if isinstance(out, str):
72
- return {"text": out}
73
- return out
74
- files = {"file": open(audio_path, "rb")}
75
- headers = {"Authorization": f"Bearer {self.hf_token}"} if self.hf_token else {}
76
- r = requests.post(f"{self.base_url}/transcribe", files=files, data=kwargs, headers=headers, timeout=self.timeout)
77
- r.raise_for_status()
78
- return r.json()