Spaces:
Sleeping
Sleeping
Download app.py from monled/text-summarizer: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/spaces/monled/text-summarizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/monled/text-summarizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/monled/text-summarizer/resolve/main/app.py
2.89 kB
| 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() | |
| # 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) |