File size: 1,549 Bytes
080eba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
from peft import PeftModel
from deep_translator import GoogleTranslator
import torch

# Load tokenizer and base model
tokenizer = T5Tokenizer.from_pretrained("./")
base_model = T5ForConditionalGeneration.from_pretrained("t5-small")
model = PeftModel.from_pretrained(base_model, "./")

# Use GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

def summarize(text, language='en'):
    if language != 'en':
        text = GoogleTranslator(source='auto', target='en').translate(text)

    input_ids = tokenizer("summarize: " + text, return_tensors="pt", truncation=True, max_length=512).input_ids.to(device)
    output_ids = model.generate(
        input_ids,
        max_length=80,
        min_length=15,
        length_penalty=1.5,
        num_beams=8,
        no_repeat_ngram_size=3,
        early_stopping=True
    )
    summary = tokenizer.decode(output_ids[0], skip_special_tokens=True)

    if language != 'en':
        summary = GoogleTranslator(source='en', target=language).translate(summary)

    return summary

gr.Interface(
    fn=summarize,
    inputs=[
        gr.Textbox(lines=10, label="Enter Article"),
        gr.Dropdown(choices=["en", "hi", "te", "fr", "es", "de"], value="en", label="Output Language")
    ],
    outputs="textbox",
    title="T5 Summarizer (LoRA Optimized)",
    description="Summarize articles using a fine-tuned T5 model with LoRA. Supports multiple languages."
).launch()