Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from keybert import KeyBERT
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
import nltk
|
| 5 |
+
nltk.download('punkt_tab')
|
| 6 |
+
|
| 7 |
+
device="cuda"
|
| 8 |
+
kw_model = KeyBERT()
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("itskavya/t5-small-finetuned-titlegen2") # this is where the model is saved on hf, can load it n use it
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("itskavya/t5-small-finetuned-titlegen2")
|
| 11 |
+
model.to(device)
|
| 12 |
+
max_input_length=512
|
| 13 |
+
|
| 14 |
+
def predict(text):
|
| 15 |
+
inputs = ["summarize: " + text]
|
| 16 |
+
|
| 17 |
+
inputs = tokenizer(inputs, max_length=max_input_length, truncation=True, return_tensors="pt").to(device)
|
| 18 |
+
output = model.generate(**inputs, num_beams=8, do_sample=True, min_length=10, max_length=64) # num beans 8 means explore 8 sequences, sample introduces randomness
|
| 19 |
+
decoded_output = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 20 |
+
predicted_title = nltk.sent_tokenize(decoded_output.strip())[0]
|
| 21 |
+
|
| 22 |
+
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), top_n=5)
|
| 23 |
+
formatted_keywords = ", ".join([kw[0] for kw in keywords])
|
| 24 |
+
|
| 25 |
+
return predicted_title, formatted_keywords
|
| 26 |
+
|
| 27 |
+
# Create the Gradio interface
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=predict, # function to call for prediction
|
| 30 |
+
inputs=[ # inputs that user will provide
|
| 31 |
+
gr.Textbox(label="Enter abstract..."),
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
],
|
| 35 |
+
outputs=[gr.Textbox(label="Title"), # outputs for title n keyword
|
| 36 |
+
gr.Textbox(label="Keywords"),],
|
| 37 |
+
title="Automated Title and Keyword Extraction from Research Abstracts",
|
| 38 |
+
description="This app uses the abstract of a scientific research article to automatically generate relevant and impactful titles and keywords!"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the app
|
| 42 |
+
interface.launch()
|