Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import PyPDF2
|
| 3 |
-
import requests
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# ============= EXTRAER TEXTO DEL PDF =============
|
| 7 |
def extraer_texto_pdf(pdf_file):
|
|
@@ -18,7 +18,7 @@ def extraer_texto_pdf(pdf_file):
|
|
| 18 |
def analizar_con_llm(texto):
|
| 19 |
"""El LLM analiza la factura y devuelve un resumen en un párrafo"""
|
| 20 |
|
| 21 |
-
token = os.getenv("
|
| 22 |
if not token:
|
| 23 |
return "❌ Error: Falta configurar HF_TOKEN en Settings → Secrets"
|
| 24 |
|
|
@@ -36,46 +36,24 @@ def analizar_con_llm(texto):
|
|
| 36 |
TEXTO DE LA FACTURA:
|
| 37 |
{texto_limpio}
|
| 38 |
|
| 39 |
-
Responde en un solo párrafo claro y conciso:"""
|
| 40 |
|
| 41 |
-
# Modelo LLM gratuito y funcional
|
| 42 |
-
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 43 |
-
|
| 44 |
-
headers = {
|
| 45 |
-
"Content-Type": "application/json",
|
| 46 |
-
"Authorization": f"Bearer {token}"
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
payload = {
|
| 50 |
-
"inputs": prompt,
|
| 51 |
-
"parameters": {
|
| 52 |
-
"max_new_tokens": 500,
|
| 53 |
-
"temperature": 0.3,
|
| 54 |
-
"return_full_text": False
|
| 55 |
-
},
|
| 56 |
-
"options": {
|
| 57 |
-
"wait_for_model": True
|
| 58 |
-
}
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
try:
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Extraer respuesta
|
| 73 |
-
|
| 74 |
-
return resultado[0].get('generated_text', 'Sin respuesta')
|
| 75 |
-
elif isinstance(resultado, dict):
|
| 76 |
-
return resultado.get('generated_text', 'Sin respuesta')
|
| 77 |
-
|
| 78 |
-
return "❌ No se pudo obtener respuesta del modelo"
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
return f"❌ Error: {str(e)}"
|
|
@@ -135,8 +113,9 @@ with gr.Blocks(title="Analizador de Facturas con IA") as demo:
|
|
| 135 |
gr.Markdown("""
|
| 136 |
---
|
| 137 |
**Configuración necesaria:**
|
| 138 |
-
1.
|
| 139 |
-
2.
|
|
|
|
| 140 |
""")
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import PyPDF2
|
|
|
|
| 3 |
import os
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
# ============= EXTRAER TEXTO DEL PDF =============
|
| 7 |
def extraer_texto_pdf(pdf_file):
|
|
|
|
| 18 |
def analizar_con_llm(texto):
|
| 19 |
"""El LLM analiza la factura y devuelve un resumen en un párrafo"""
|
| 20 |
|
| 21 |
+
token = os.getenv("HF_TOKEN")
|
| 22 |
if not token:
|
| 23 |
return "❌ Error: Falta configurar HF_TOKEN en Settings → Secrets"
|
| 24 |
|
|
|
|
| 36 |
TEXTO DE LA FACTURA:
|
| 37 |
{texto_limpio}
|
| 38 |
|
| 39 |
+
Responde en un solo párrafo claro y conciso en español:"""
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
+
# Usar el nuevo InferenceClient de Hugging Face
|
| 43 |
+
client = InferenceClient(token=token)
|
| 44 |
|
| 45 |
+
# Llamar al modelo con chat.completions (compatible con OpenAI)
|
| 46 |
+
response = client.chat.completions.create(
|
| 47 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 48 |
+
messages=[
|
| 49 |
+
{"role": "user", "content": prompt}
|
| 50 |
+
],
|
| 51 |
+
max_tokens=500,
|
| 52 |
+
temperature=0.3
|
| 53 |
+
)
|
| 54 |
|
| 55 |
# Extraer respuesta
|
| 56 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
return f"❌ Error: {str(e)}"
|
|
|
|
| 113 |
gr.Markdown("""
|
| 114 |
---
|
| 115 |
**Configuración necesaria:**
|
| 116 |
+
1. Instala: `pip install huggingface_hub`
|
| 117 |
+
2. Ve a Settings → Secrets
|
| 118 |
+
3. Crea: `HF_TOKEN` = tu token de https://huggingface.co/settings/tokens
|
| 119 |
""")
|
| 120 |
|
| 121 |
if __name__ == "__main__":
|