Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification
|
| 3 |
+
import gradio
|
| 4 |
+
|
| 5 |
+
mapper = {"India Today": 0, "NDTV": 1, "The Indian Express": 2, "The Times Of India": 3, "The Hindu": 4}
|
| 6 |
+
rev_mapper = {k:v for v,k in mapper.items()}
|
| 7 |
+
|
| 8 |
+
source_path = "rg089/bert_newspaper_source"
|
| 9 |
+
title_path = "rg089/t5-headline-generation"
|
| 10 |
+
summary_path = "rg089/distilbart-summarization"
|
| 11 |
+
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
|
| 14 |
+
source_model = AutoModelForSequenceClassification.from_pretrained(source_path).to(device)
|
| 15 |
+
source_tokenizer = AutoTokenizer.from_pretrained(source_path)
|
| 16 |
+
|
| 17 |
+
title_model = AutoModelForSeq2SeqLM.from_pretrained(title_path).to(device)
|
| 18 |
+
title_tokenizer = AutoTokenizer.from_pretrained(title_path)
|
| 19 |
+
|
| 20 |
+
summary_model = AutoModelForSeq2SeqLM.from_pretrained(summary_path).to(device)
|
| 21 |
+
summary_tokenizer = AutoTokenizer.from_pretrained(summary_path)
|
| 22 |
+
|
| 23 |
+
def generate(model, tokenizer, test_samples, prefix="", max_length=256):
|
| 24 |
+
model.eval()
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
if type(test_samples) == str:
|
| 27 |
+
test_samples = prefix + test_samples
|
| 28 |
+
else:
|
| 29 |
+
for i in range(len(test_samples)):
|
| 30 |
+
test_samples[i] = prefix + test_samples[i]
|
| 31 |
+
with tokenizer.as_target_tokenizer():
|
| 32 |
+
inputs = tokenizer(
|
| 33 |
+
test_samples,
|
| 34 |
+
truncation=True,
|
| 35 |
+
padding="max_length",
|
| 36 |
+
max_length=max_length,
|
| 37 |
+
return_tensors="pt")
|
| 38 |
+
|
| 39 |
+
input_ids = inputs.input_ids.to(device)
|
| 40 |
+
attention_mask = inputs.attention_mask.to(device)
|
| 41 |
+
|
| 42 |
+
outputs = model.generate(input_ids, attention_mask=attention_mask, num_beams=10, max_length=max_length) #, min_length=50)
|
| 43 |
+
output_str = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 44 |
+
return output_str[0]
|
| 45 |
+
|
| 46 |
+
def classify(model, tokenizer, content, title):
|
| 47 |
+
model.eval()
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
model_inputs = tokenizer(title, content, padding=True, truncation=True, return_tensors="pt").to(device)
|
| 50 |
+
outputs = model(**model_inputs)
|
| 51 |
+
logits = outputs.logits
|
| 52 |
+
selected = logits.argmax(dim=-1).cpu().tolist()
|
| 53 |
+
answers = [rev_mapper[sel] for sel in selected]
|
| 54 |
+
return answers[0]
|
| 55 |
+
|
| 56 |
+
def main(content, classify_source=False):
|
| 57 |
+
output = ""
|
| 58 |
+
title = generate(title_model, title_tokenizer, content, prefix="headline: ")
|
| 59 |
+
output += f"Title: {title}\n"
|
| 60 |
+
if classify_source:
|
| 61 |
+
source = classify(source_model, source_tokenizer, content, title)
|
| 62 |
+
output += f"Source: {source}\n\n"
|
| 63 |
+
summary = generate(summary_model, summary_tokenizer, content, prefix="")
|
| 64 |
+
output += f"Summary: {summary}"
|
| 65 |
+
return output
|
| 66 |
+
|
| 67 |
+
title = "News Helper: Generate Headlines, Summary and Classify the Newspaper Source!"
|
| 68 |
+
description = """
|
| 69 |
+
The current sources supported for classification are: The Times of India, The Indian Express, NDTV, The Hindu and India Today.
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
placeholder = "Enter the content of the article here."
|
| 73 |
+
|
| 74 |
+
iface = gradio.Interface(fn=main, inputs=[gradio.inputs.Textbox(lines=10, placeholder=placeholder, label='Article Content:'),
|
| 75 |
+
gradio.inputs.Checkbox(default=True, label='Classify the Source:')], outputs="textbox", title=title,
|
| 76 |
+
description=description)
|
| 77 |
+
iface.launch()
|