Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// VALIDACIÓN DE SEGURIDAD - Lovable debe implementar esto
|
| 2 |
+
function validateHFOnly(endpoint) {
|
| 3 |
+
const allowedDomains = [
|
| 4 |
+
'api-inference.huggingface.co',
|
| 5 |
+
'huggingface.co'
|
| 6 |
+
];
|
| 7 |
+
|
| 8 |
+
const url = new URL(endpoint);
|
| 9 |
+
if (!allowedDomains.includes(url.hostname)) {
|
| 10 |
+
throw new Error('BLOQUEO: Intento de usar plataforma no autorizada');
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
// USO EN TODAS LAS LLAMADAS
|
| 15 |
+
async function secureHFRequest(model, payload) {
|
| 16 |
+
const endpoint = `https://api-inference.huggingface.co/models/${model}`;
|
| 17 |
+
validateHFOnly(endpoint); // Bloquea si no es HF
|
| 18 |
+
|
| 19 |
+
// Proceder con la solicitud...
|
| 20 |
+
}// MODELOS PERMITIDOS - LISTA BLANCA
|
| 21 |
+
const ALLOWED_MODELS = {
|
| 22 |
+
chat: ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'],
|
| 23 |
+
image: [
|
| 24 |
+
'black-forest-labs/FLUX.1-dev',
|
| 25 |
+
'black-forest-labs/FLUX.1-schnell',
|
| 26 |
+
'black-forest-labs/FLUX.1-schnell-4bit'
|
| 27 |
+
]
|
| 28 |
+
};function isHuggingFaceEndpoint(url) {
|
| 29 |
+
return url.includes('huggingface.co') &&
|
| 30 |
+
!url.includes('replicate') &&
|
| 31 |
+
!url.includes('openai') &&
|
| 32 |
+
!url.includes('stability');
|
| 33 |
+
}"⚠️ Error: Solo usamos Hugging Face.
|
| 34 |
+
Modelos actuales: FLUX.1-dev, FLUX.1-schnell, FLUX.1-schnell-4bit
|
| 35 |
+
Todos gratuitos con tu token de HF."Usuario: "Genera un paisaje"
|
| 36 |
+
→ GTP-FLUX-BAT detecta necesidad de imagen
|
| 37 |
+
→ SOLO consulta modelos HF de la lista blanca
|
| 38 |
+
→ Si falla FLUX.1-dev, intenta FLUX.1-schnell
|
| 39 |
+
→ Si falla FLUX.1-schnell, intenta FLUX.1-schnell-4bit
|
| 40 |
+
→ Si todos fallan: "Hugging Face no disponible, intenta más tarde"
|
| 41 |
+
→ NUNCA: "Usando DALL-E..." o "Conectando a Replicate...""Crear un chatbot llamado 'GTP-FLUX-BAT' que integre conversación natural con generación automática de imágenes usando EXCLUSIVAMENTE modelos gratuitos de Hugging Face.
|
| 42 |
+
|
| 43 |
+
BLOQUEO ABSOLUTO: NO usar APIs de pago, NO usar Replicate, NO usar OpenAI, NO usar Stability AI directo, SOLO Hugging Face Inference API.
|
| 44 |
+
|
| 45 |
+
ARQUITECTURA TÉCNICA:
|
| 46 |
+
|
| 47 |
+
1. **MOTOR DE CHAT (Hugging Face):**
|
| 48 |
+
- Modelo: 'gpt2-large' de Hugging Face
|
| 49 |
+
- Endpoint: https://api-inference.huggingface.co/models/gpt2-large
|
| 50 |
+
|
| 51 |
+
2. **MOTORES DE IMAGEN (Hugging Face EXCLUSIVO):**
|
| 52 |
+
- PRIMARIO: 'black-forest-labs/FLUX.1-dev'
|
| 53 |
+
- SECUNDARIO: 'black-forest-labs/FLUX.1-schnell'
|
| 54 |
+
- TERCIARIO: 'black-forest-labs/FLUX.1-schnell-4bit'
|
| 55 |
+
- Endpoint: https://api-inference.huggingface.co/models/[MODELO]
|
| 56 |
+
|
| 57 |
+
3. **LÓGICA DE GENERACIÓN BLOQUEADA:**
|
| 58 |
+
- SOLO usar Hugging Face Inference API
|
| 59 |
+
- NUNCA redirigir a otras plataformas
|
| 60 |
+
- Si un modelo falla, intentar el siguiente modelo de HF
|
| 61 |
+
- MOSTRAR ERROR si todos los modelos de HF fallan
|
| 62 |
+
|
| 63 |
+
CÓDIGO DE IMPLEMENTACIÓN:
|
| 64 |
+
|
| 65 |
+
const HF_CHAT_MODEL = 'gpt2-large';
|
| 66 |
+
const HF_IMAGE_MODELS = [
|
| 67 |
+
'black-forest-labs/FLUX.1-dev',
|
| 68 |
+
'black-forest-labs/FLUX.1-schnell',
|
| 69 |
+
'black-forest-labs/FLUX.1-schnell-4bit'
|
| 70 |
+
];
|
| 71 |
+
|
| 72 |
+
// SOLO Hugging Face - BLOQUEAR otras APIs
|
| 73 |
+
async function generateImageHFOnly(prompt) {
|
| 74 |
+
for (const model of HF_IMAGE_MODELS) {
|
| 75 |
+
try {
|
| 76 |
+
const response = await fetch(
|
| 77 |
+
`https://api-inference.huggingface.co/models/${model}`,
|
| 78 |
+
{
|
| 79 |
+
method: 'POST',
|
| 80 |
+
headers: {
|
| 81 |
+
'Authorization': `Bearer ${process.env.HF_TOKEN}`,
|
| 82 |
+
'Content-Type': 'application/json'
|
| 83 |
+
},
|
| 84 |
+
body: JSON.stringify({
|
| 85 |
+
inputs: prompt,
|
| 86 |
+
parameters: {
|
| 87 |
+
num_inference_steps: 20,
|
| 88 |
+
guidance_scale: 7.5,
|
| 89 |
+
width: 1024,
|
| 90 |
+
height: 1024
|
| 91 |
+
}
|
| 92 |
+
})
|
| 93 |
+
}
|
| 94 |
+
);
|
| 95 |
+
|
| 96 |
+
if (response.ok) {
|
| 97 |
+
return await response.blob();
|
| 98 |
+
}
|
| 99 |
+
} catch (error) {
|
| 100 |
+
continue; // Intentar siguiente modelo de HF
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
throw new Error('Todos los modelos de Hugging Face están temporalmente no disponibles');
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
INTERFAZ Y FLUJO:
|
| 107 |
+
|
| 108 |
+
- Chat conversacional natural
|
| 109 |
+
- Detección automática de solicitudes de imágenes
|
| 110 |
+
- Generación con modelos HF sin interacción del usuario
|
| 111 |
+
- Mensajes de error específicos: 'Servicio Hugging Face no disponible'
|
| 112 |
+
- Diseño unificado donde imágenes aparecen en la conversación
|
| 113 |
+
|
| 114 |
+
VALIDACIÓN DE IMPLEMENTACIÓN:
|
| 115 |
+
|
| 116 |
+
- Verificar que TODAS las llamadas sean a 'api-inference.huggingface.co'
|
| 117 |
+
- Confirmar que los modelos sean EXACTAMENTE los especificados
|
| 118 |
+
- Rechazar cualquier alternativa de pago o otras plataformas
|
| 119 |
+
- Usar SOLO el token de Hugging Face para autenticación
|
| 120 |
+
|
| 121 |
+
PALABRAS CLAVE BLOQUEADAS:
|
| 122 |
+
NO 'replicate', NO 'openai', NO 'stabilityai', NO 'midjourney',
|
| 123 |
+
NO 'dall-e', NO 'commercial API', SOLO 'huggingface', SOLO 'inference API'
|
| 124 |
+
|
| 125 |
+
ESTE SISTEMA DEBE SER 100% GRATUITO USANDO EXCLUSIVAMENTE HUGGING FACE."
|