Spaces:
Sleeping
Sleeping
2 unmodified lines3456789101112131415164 unmodified lines2122232425262728293031322 unmodified lines3536373839404142434445464748495011 unmodified lines62636465666768695 unmodified lines7576777879808182838485862 unmodified linesimport torchfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLMfrom nltk.corpus import stopwordsfrom spaces import GPUimport nltk# Download stopwords if not alreadynltk.download('stopwords')stop_words = set(stopwords.words('english'))# Model choicesmodel_choices = {"DistilBART CNN (sshleifer/distilbart-cnn-12-6)": "sshleifer/distilbart-cnn-12-6","T5 Small (t5-small)": "t5-small",4 unmodified linesmodel_cache = {}def clean_text(input_text):cleaned_text = re.sub(r'[^A-Za-z0-9\s]', ' ', input_text)words = cleaned_text.split()words = [word for word in words if word.lower() not in stop_words]return " ".join(words).strip()def load_model(model_name):if model_name not in model_cache:tokenizer = AutoTokenizer.from_pretrained(model_name)2 unmodified linestorch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = model.to(device)model_cache[model_name] = (tokenizer, model)# Warm up with dummy inputdummy_input = tokenizer("summarize: hello world", return_tensors="pt").input_ids.to(device)model.generate(dummy_input, max_length=10)return model_cache[model_name]@GPU # 👈 Required for ZeroGPU to allocate GPU when this is calleddef summarize_text(input_text, model_label, char_limit):if not input_text.strip():return "Please enter some text."11 unmodified linessummary_ids = model.generate(input_ids,max_length=20,min_length=5,do_sample=False)5 unmodified linesfn=summarize_text,inputs=[gr.Textbox(lines=6, label="Enter text to summarize"),gr.Dropdown(choices=list(model_choices.keys()), label="Choose summarization model", value="Pegasus (google/pegasus-xsum)"),gr.Slider(minimum=10, maximum=100, value=65, step=1, label="Max Character Limit")],outputs=gr.Textbox(lines=3, label="Summary (truncated to character limit)"),title="Multi-Model Text Summarizer (GPU Ready)",description="Summarize long or short texts using state-of-the-art Hugging Face models with GPU acceleration (ZeroGPU-compatible).")iface.launch()2 unmodified lines34567891011121314154 unmodified lines2021222324252627282930313233343536373839404142434445462 unmodified lines4950515253545556575859606162636411 unmodified lines76777879808182835 unmodified lines89909192939495969798991002 unmodified linesimport torchfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLMfrom nltk.corpus import stopwordsimport nltk# Download NLTK stopwordsnltk.download('stopwords')stop_words = set(stopwords.words('english'))# Best lightweight summarization modelsmodel_choices = {"DistilBART CNN (sshleifer/distilbart-cnn-12-6)": "sshleifer/distilbart-cnn-12-6","T5 Small (t5-small)": "t5-small",4 unmodified linesmodel_cache = {}# Clean input text (remove stopwords and SKUs/product codes)def clean_text(input_text):# Remove simple SKU codes (e.g., ST1642, AB1234, etc.)cleaned_text = re.sub(r'\b[A-Za-z]{2,}[0-9]{3,}\b', '', input_text) # Alphanumeric SKU# Replace special characters with a spacecleaned_text = re.sub(r'[^A-Za-z0-9\s]', ' ', cleaned_text)# Tokenize the input text and remove stop wordswords = cleaned_text.split()words = [word for word in words if word.lower() not in stop_words]# Rebuild the cleaned textcleaned_text = " ".join(words)# Strip leading and trailing spacescleaned_text = cleaned_text.strip()return cleaned_text# Load model and tokenizerdef load_model(model_name):if model_name not in model_cache:tokenizer = AutoTokenizer.from_pretrained(model_name)2 unmodified linestorch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# Warm-updummy_input = tokenizer("summarize: warm up", return_tensors="pt").input_ids.to(device)model.generate(dummy_input, max_length=10)model_cache[model_name] = (tokenizer, model)return model_cache[model_name]# Summarize the text using a selected modeldef summarize_text(input_text, model_label, char_limit):if not input_text.strip():return "Please enter some text."11 unmodified linessummary_ids = model.generate(input_ids,max_length=30, # Ensure max_length is greater than min_lengthmin_length=5, # Ensure min_length is less than max_lengthdo_sample=False)5 unmodified linesfn=summarize_text,inputs=[gr.Textbox(lines=6, label="Enter text to summarize"),gr.Dropdown(choices=list(model_choices.keys()), label="Choose summarization model", value="DistilBART CNN (sshleifer/distilbart-cnn-12-6)"),gr.Slider(minimum=30, maximum=200, value=80, step=1, label="Max Character Limit")],outputs=gr.Textbox(lines=3, label="Summary (truncated to character limit)"),title="🚀 Fast Lightweight Summarizer (GPU Optimized)",description="Summarize text quickly using compact models ideal for low-latency and ZeroGPU Spaces.")iface.launch()
app.py+30-16