AdityaMayukhSom
commited on
Commit
•
27fdfb1
1
Parent(s):
53150b7
base app for title generation
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import transformers
|
3 |
+
import pandas as pd
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
paper_contents: str = ""
|
7 |
+
generated_title: str = ""
|
8 |
+
maximum_tokens: int = 10
|
9 |
+
preferred_model: str = ""
|
10 |
+
|
11 |
+
AVAILABLE_MODELS = [
|
12 |
+
"TohidaRehman/pegasus-large-Abstract-Title-CSPubSum",
|
13 |
+
"TohidaRehman/Llama-3-8b-Abstract-Title-CSPubSum",
|
14 |
+
"TohidaRehman/t5-base-Abstract-Title",
|
15 |
+
"czearing/article-title-generator"
|
16 |
+
]
|
17 |
+
|
18 |
+
def generate_title(input_text: str, model_name: str, max_length: int = 20) -> str:
|
19 |
+
prefix: str = "summarize: "
|
20 |
+
text_with_prefix = prefix + input_text
|
21 |
+
|
22 |
+
try:
|
23 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
|
24 |
+
model = transformers.AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
25 |
+
|
26 |
+
inputs = tokenizer(
|
27 |
+
text_with_prefix,
|
28 |
+
return_tensors='pt',
|
29 |
+
max_length=512,
|
30 |
+
truncation=True,
|
31 |
+
padding=True
|
32 |
+
)
|
33 |
+
|
34 |
+
predictions = model.generate(
|
35 |
+
input_ids=inputs['input_ids'],
|
36 |
+
attention_mask=inputs['attention_mask'],
|
37 |
+
max_length=max_length,
|
38 |
+
num_beams=4,
|
39 |
+
do_sample=True,
|
40 |
+
min_length=3,
|
41 |
+
)
|
42 |
+
|
43 |
+
summary = tokenizer.decode(predictions[0], skip_special_tokens=True)
|
44 |
+
return summary
|
45 |
+
except Exception as e:
|
46 |
+
return str(e)
|
47 |
+
|
48 |
+
st.set_page_config(
|
49 |
+
layout="wide",
|
50 |
+
page_title="Research Paper Title Generator",
|
51 |
+
page_icon=":sun_behind_cloud:",
|
52 |
+
)
|
53 |
+
|
54 |
+
st.markdown("""
|
55 |
+
<style type="text/css">
|
56 |
+
* {
|
57 |
+
border-radius: 0px !important;
|
58 |
+
}
|
59 |
+
|
60 |
+
div[role="slider"] {
|
61 |
+
border-radius: 50% !important;
|
62 |
+
}
|
63 |
+
|
64 |
+
.stSpinner i {
|
65 |
+
border-radius: 50% !important;
|
66 |
+
}
|
67 |
+
|
68 |
+
.block-container {
|
69 |
+
padding-bottom: 1rem !important;
|
70 |
+
}
|
71 |
+
</style>
|
72 |
+
""", unsafe_allow_html=True)
|
73 |
+
|
74 |
+
col1, col2 = st.columns([2, 1], gap="medium")
|
75 |
+
col1.subheader("Research Paper Title Generator")
|
76 |
+
col2.subheader("Generated Title")
|
77 |
+
|
78 |
+
with col1.form(key='research_paper_title_generation_parameters'):
|
79 |
+
paper_contents = st.text_area('Paper Contents', key="paper_contents", value="", height=220, placeholder="Paste Paper Contents Here")
|
80 |
+
preferred_model = st.selectbox("Select Preferred Model", AVAILABLE_MODELS, key="preferred_model")
|
81 |
+
maximum_tokens = st.slider("Maximum Tokens", key="maximum_tokens", value=10, min_value=3, max_value=20, step=1)
|
82 |
+
submitted = st.form_submit_button(label='Generate Title', type="primary")
|
83 |
+
if submitted:
|
84 |
+
with col2:
|
85 |
+
col2.write("")
|
86 |
+
with st.spinner(text="In progress..."):
|
87 |
+
generated_title = generate_title(paper_contents, preferred_model, maximum_tokens)
|
88 |
+
col2.write(generated_title)
|