Spaces:
Sleeping

File size: 2,885 Bytes
d88b351
605cd4f
 
 
 
 
 
 
 
 
 
 
 
d88b351
 
 
 
 
605cd4f
 
d88b351
af57a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d88b351
605cd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d88b351
e1b1fdd
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import spaces  # Important spaces al principi absolut de tot
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# Configurarem el dispositiu (GPU si est脿 disponible, sin贸 CPU)
device = "cuda" if torch.cuda.is_available() else "cpu"
model_name = "ZyroGod/flan-t5-base-summarization"

# Carregarem el model i el tokenizer un sol cop en engegar l'aplicaci贸
print(f"Carregant el model {model_name} al dispositiu: {device}...")
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Carregant el model de manera segura segons el dispositiu (evitant errors de dtype)
model = AutoModelForSeq2SeqLM.from_pretrained(
    model_name, 
    torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
).to(device)
model.eval()

@spaces.GPU  # Necessari per a Hugging Face Spaces amb ZeroGPU
def generar_resum(text_entrada):
    try:
        # Comprovarem que el text d'entrada no estigui buit
        if not text_entrada or not text_entrada.strip():
            return "Si us plau, introdueix un text v脿lid per resumir."
        
        # Prepararem el text amb el prefix del model
        input_text = "summarize: " + text_entrada
        
        # Tokenitzem el text assegurant portar-lo al dispositiu correcte
        inputs = tokenizer(
            input_text, 
            return_tensors="pt", 
            max_length=512, 
            truncation=True
        ).to(device)

        # Generem el resum amb par脿metres segurs
        with torch.no_grad():
            output_ids = model.generate(
                input_ids=inputs["input_ids"],
                attention_mask=inputs["attention_mask"],
                max_length=128,
                min_length=20,
                num_beams=4,
                length_penalty=2.0,
                no_repeat_ngram_size=3,
                early_stopping=True
            )

        # Descodifiquem la sortida
        summary = tokenizer.decode(output_ids[0], skip_special_tokens=True)
        return summary
        
    except Exception as e:
        return f"S'ha produ茂t un error durant la generaci贸 del resum: {str(e)}"
    
# Dissenyarem la interf铆cie gr脿fica web amb Gradio
demo = gr.Interface(
    fn=generar_resum,
    inputs=gr.Textbox(
        lines=6, 
        placeholder="Enganxa aqu铆 el text o article que vols resumir (en angl猫s)...", 
        label="Text d'entrada"
    ),
    outputs=gr.Textbox(
        label="Resum generat"
    ),
    title="Aplicaci贸 de Resum de Textos en angl猫s amb Flan-T5",
    description="Eina interactiva basada en el model personalitzat per fer resum de textos en angl猫s."
)

if __name__ == "__main__":
    # Per a Hugging Face Spaces, s'executa amb host de xarxa obert i sense share=True
    demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)