Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,46 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
-
from peft import PeftModel
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Load tokenizer and base model
|
| 8 |
-
tokenizer = T5Tokenizer.from_pretrained("./")
|
| 9 |
-
base_model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
| 10 |
-
model = PeftModel.from_pretrained(base_model, "./")
|
| 11 |
-
|
| 12 |
-
# Use GPU if available
|
| 13 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
-
model.to(device)
|
| 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 |
-
description="Summarize long text using your fine-tuned T5-small model with LoRA adapters. Supports translation."
|
| 48 |
-
).launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
+
from peft import PeftModel
|
| 4 |
+
from deep_translator import GoogleTranslator
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Load tokenizer and base model
|
| 8 |
+
tokenizer = T5Tokenizer.from_pretrained("./")
|
| 9 |
+
base_model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
| 10 |
+
model = PeftModel.from_pretrained(base_model, "./")
|
| 11 |
+
|
| 12 |
+
# Use GPU if available
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
model.to(device)
|
| 15 |
+
|
| 16 |
+
def summarize(text, language='en'):
|
| 17 |
+
if language != 'en':
|
| 18 |
+
text = GoogleTranslator(source='auto', target='en').translate(text)
|
| 19 |
+
|
| 20 |
+
input_ids = tokenizer("summarize: " + text, return_tensors="pt", truncation=True, max_length=512).input_ids.to(device)
|
| 21 |
+
output_ids = model.generate(
|
| 22 |
+
input_ids,
|
| 23 |
+
max_length=80,
|
| 24 |
+
min_length=15,
|
| 25 |
+
length_penalty=1.5,
|
| 26 |
+
num_beams=8,
|
| 27 |
+
no_repeat_ngram_size=3,
|
| 28 |
+
early_stopping=True
|
| 29 |
+
)
|
| 30 |
+
summary = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 31 |
+
|
| 32 |
+
if language != 'en':
|
| 33 |
+
summary = GoogleTranslator(source='en', target=language).translate(summary)
|
| 34 |
+
|
| 35 |
+
return summary
|
| 36 |
+
|
| 37 |
+
gr.Interface(
|
| 38 |
+
fn=summarize,
|
| 39 |
+
inputs=[
|
| 40 |
+
gr.Textbox(lines=10, label="Enter Article"),
|
| 41 |
+
gr.Dropdown(choices=["en", "hi", "te", "fr", "es", "de"], value="en", label="Output Language")
|
| 42 |
+
],
|
| 43 |
+
outputs="textbox",
|
| 44 |
+
title="T5 Summarizer (LoRA Optimized)",
|
| 45 |
+
description="Summarize articles using a fine-tuned T5 model with LoRA. Supports multiple languages."
|
| 46 |
+
).launch()
|
|
|
|
|
|