Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
# 1. Cargamos el modelo
|
| 5 |
-
# Quitamos device_map y torch_dtype para que Hugging Face use la configuraci贸n por defecto m谩s estable.
|
| 6 |
generador = pipeline(
|
| 7 |
"text-generation",
|
| 8 |
-
model="edusc182/Gemma_2B"
|
|
|
|
|
|
|
| 9 |
)
|
| 10 |
|
| 11 |
def predecir(pregunta):
|
| 12 |
-
# 2. Preparamos
|
| 13 |
prompt = f"Pregunta: {pregunta}\nRespuesta:"
|
| 14 |
|
| 15 |
-
# 3. Generamos la respuesta
|
| 16 |
resultados = generador(
|
| 17 |
prompt,
|
| 18 |
-
max_new_tokens=
|
| 19 |
return_full_text=False,
|
| 20 |
temperature=0.7,
|
| 21 |
do_sample=True
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
texto_generado = resultados[0]['generated_text'].strip()
|
| 26 |
-
return texto_generado
|
| 27 |
|
| 28 |
-
#
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=predecir,
|
| 31 |
inputs=gr.Textbox(lines=2, placeholder="Escribe tu pregunta aqu铆..."),
|
| 32 |
-
outputs=gr.Textbox(label="Respuesta
|
| 33 |
title="Asistente Gemma 2B",
|
| 34 |
-
description="
|
| 35 |
)
|
| 36 |
|
| 37 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# 1. Cargamos el modelo con optimizaci贸n de memoria para el servidor gratuito
|
|
|
|
| 6 |
generador = pipeline(
|
| 7 |
"text-generation",
|
| 8 |
+
model="edusc182/Gemma_2B",
|
| 9 |
+
torch_dtype=torch.bfloat16, # 隆CRUCIAL! Reduce el peso del modelo a la mitad
|
| 10 |
+
device_map="cpu" # Asegura que no busque tarjetas gr谩ficas inexistentes
|
| 11 |
)
|
| 12 |
|
| 13 |
def predecir(pregunta):
|
| 14 |
+
# 2. Preparamos la pregunta
|
| 15 |
prompt = f"Pregunta: {pregunta}\nRespuesta:"
|
| 16 |
|
| 17 |
+
# 3. Generamos la respuesta limitando los recursos para no saturar la CPU
|
| 18 |
resultados = generador(
|
| 19 |
prompt,
|
| 20 |
+
max_new_tokens=100, # Reducido un poco para mayor velocidad en CPU
|
| 21 |
return_full_text=False,
|
| 22 |
temperature=0.7,
|
| 23 |
do_sample=True
|
| 24 |
)
|
| 25 |
|
| 26 |
+
return resultados[0]['generated_text'].strip()
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# 4. Interfaz simplificada
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=predecir,
|
| 31 |
inputs=gr.Textbox(lines=2, placeholder="Escribe tu pregunta aqu铆..."),
|
| 32 |
+
outputs=gr.Textbox(label="Respuesta"),
|
| 33 |
title="Asistente Gemma 2B",
|
| 34 |
+
description="Optimizado para servidor gratuito."
|
| 35 |
)
|
| 36 |
|
| 37 |
iface.launch()
|