Spaces:
Runtime error
Runtime error
Upload huggingface_utils.py with huggingface_hub
Browse files- huggingface_utils.py +101 -0
huggingface_utils.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import time
|
4 |
+
from requests.exceptions import RequestException, Timeout
|
5 |
+
|
6 |
+
class HuggingFaceUtils:
|
7 |
+
MODELS = {
|
8 |
+
'Mixtral': {
|
9 |
+
'url': "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
10 |
+
'format': lambda p: f"<s>[INST] {p} [/INST]"
|
11 |
+
}
|
12 |
+
}
|
13 |
+
|
14 |
+
def __init__(self, token):
|
15 |
+
if not token:
|
16 |
+
raise ValueError("Se requiere un token de HuggingFace")
|
17 |
+
self.token = token
|
18 |
+
self.headers = {
|
19 |
+
"Authorization": f"Bearer {token}",
|
20 |
+
"Content-Type": "application/json"
|
21 |
+
}
|
22 |
+
self.session = requests.Session()
|
23 |
+
self.max_retries = 3
|
24 |
+
self.base_timeout = 60
|
25 |
+
|
26 |
+
def _make_request(self, url, payload, attempt=1):
|
27 |
+
"""Hacer request con reintentos y manejo de errores"""
|
28 |
+
try:
|
29 |
+
response = self.session.post(
|
30 |
+
url,
|
31 |
+
headers=self.headers,
|
32 |
+
json=payload,
|
33 |
+
timeout=self.base_timeout
|
34 |
+
)
|
35 |
+
response.raise_for_status()
|
36 |
+
return response.json()
|
37 |
+
|
38 |
+
except Timeout:
|
39 |
+
if attempt < self.max_retries:
|
40 |
+
time.sleep(2 ** attempt) # Espera exponencial
|
41 |
+
return self._make_request(url, payload, attempt + 1)
|
42 |
+
raise Exception("Timeout al conectar con HuggingFace")
|
43 |
+
|
44 |
+
except RequestException as e:
|
45 |
+
if hasattr(e, 'response') and e.response is not None:
|
46 |
+
if e.response.status_code == 401:
|
47 |
+
raise Exception("Token de HuggingFace inv谩lido")
|
48 |
+
elif e.response.status_code == 429:
|
49 |
+
raise Exception("L铆mite de rate excedido")
|
50 |
+
else:
|
51 |
+
raise Exception(f"Error de HuggingFace: {e.response.status_code}")
|
52 |
+
|
53 |
+
if attempt < self.max_retries:
|
54 |
+
time.sleep(2 ** attempt)
|
55 |
+
return self._make_request(url, payload, attempt + 1)
|
56 |
+
raise Exception(f"Error de conexi贸n con HuggingFace: {str(e)}")
|
57 |
+
|
58 |
+
def generate_response(self, prompt, model_name='Mixtral', max_length=200):
|
59 |
+
"""Generar respuesta con manejo de errores mejorado"""
|
60 |
+
try:
|
61 |
+
if not prompt:
|
62 |
+
raise ValueError("No se proporcion贸 un prompt")
|
63 |
+
|
64 |
+
if model_name not in self.MODELS:
|
65 |
+
raise ValueError(f"Modelo {model_name} no disponible")
|
66 |
+
|
67 |
+
model_info = self.MODELS[model_name]
|
68 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_info['url']}"
|
69 |
+
formatted_prompt = model_info['format'](prompt)
|
70 |
+
|
71 |
+
payload = {
|
72 |
+
"inputs": formatted_prompt,
|
73 |
+
"parameters": {
|
74 |
+
"max_new_tokens": max_length,
|
75 |
+
"temperature": 0.9,
|
76 |
+
"top_p": 0.9,
|
77 |
+
"repetition_penalty": 1.2,
|
78 |
+
"do_sample": True,
|
79 |
+
"return_full_text": False,
|
80 |
+
"stop": ["[/INST]", "</s>"]
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
result = self._make_request(api_url, payload)
|
85 |
+
|
86 |
+
# A帽adir depuraci贸n para la respuesta
|
87 |
+
print(f"Respuesta de HuggingFace: {result}")
|
88 |
+
|
89 |
+
if isinstance(result, list) and len(result) > 0:
|
90 |
+
text = result[0].get('generated_text', '').strip()
|
91 |
+
if text:
|
92 |
+
# Evitar respuestas predeterminadas
|
93 |
+
if "隆Hola! Soy tu asistente virtual" in text:
|
94 |
+
raise ValueError("Respuesta predeterminada detectada")
|
95 |
+
return text
|
96 |
+
|
97 |
+
raise ValueError("No se pudo generar una respuesta coherente")
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
print(f"Error en generate_response: {str(e)}")
|
101 |
+
raise # Propagar el error para manejarlo en el nivel superior
|