File size: 1,672 Bytes
6c036c1
aab52b9
 
 
19693ec
a9b1392
aab52b9
61e43c4
 
 
 
 
 
aab52b9
14cd230
19693ec
3afee96
 
61e43c4
 
3afee96
e3d3bd5
19693ec
 
61e43c4
19693ec
61e43c4
 
 
 
19693ec
 
aab52b9
 
19693ec
aab52b9
14cd230
19693ec
 
aab52b9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# Cargar el modelo y el tokenizador
model_name = "BSC-LT/salamandra-2b"

if "model" not in globals():
    tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
    tokenizer.pad_token = tokenizer.eos_token  # 馃敼 Evita errores de atenci贸n
    model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)

# Funci贸n de generaci贸n optimizada
def generate_response(prompt):
    system_prompt = "Responde solo con el texto solicitado, sin informaci贸n personal ni datos irrelevantes."

    inputs = tokenizer(
        f"Instrucci贸n: {system_prompt} \n Pregunta: {prompt} \n Respuesta directa:",
        return_tensors="pt",
        padding=True  # 馃敼 Evita respuestas inconsistentes
    )

    outputs = model.generate(
        inputs.input_ids,
        max_new_tokens=50,  # 馃敼 En vez de max_length (mejor control de generaci贸n)
        do_sample=True,
        temperature=0.45,  # 馃敼 Menos aleatoriedad, m谩s coherencia
        top_p=0.9,  # 馃敼 M谩s controlado
        repetition_penalty=1.1,  # 馃敼 Evita repeticiones
        early_stopping=True,
    )

    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Interfaz en Gradio
with gr.Blocks() as demo:
    gr.Markdown("# 馃 Chatbot SOLID&ALIA - Optimizado con Instrucciones Claras")
    input_text = gr.Textbox(label="Escribe tu texto aqu铆")
    output_text = gr.Textbox(label="Respuesta de ALIA")
    submit_button = gr.Button("Generar respuesta")
    submit_button.click(generate_response, inputs=input_text, outputs=output_text)

demo.launch()