Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
3 |
|
4 |
-
# Load grammar model
|
5 |
grammar_model_name = "prithivida/grammar_error_correcter_v1"
|
6 |
grammar_tokenizer = AutoTokenizer.from_pretrained(grammar_model_name)
|
7 |
grammar_model = AutoModelForSeq2SeqLM.from_pretrained(grammar_model_name)
|
8 |
|
9 |
-
# Load
|
10 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
11 |
|
12 |
-
# Load title generation model
|
13 |
title_model_name = "t5-base"
|
14 |
title_tokenizer = AutoTokenizer.from_pretrained(title_model_name)
|
15 |
title_model = AutoModelForSeq2SeqLM.from_pretrained(title_model_name)
|
16 |
|
17 |
-
def polish_abstract(
|
18 |
-
# 1. Grammar Correction
|
19 |
-
|
20 |
-
|
21 |
-
corrected_text = grammar_tokenizer.decode(
|
22 |
|
23 |
-
# 2. Summary
|
24 |
-
|
25 |
-
summary_text = summary_output[0]['summary_text']
|
26 |
|
27 |
-
# 3. Title Generation
|
28 |
title_prompt = "generate title: " + corrected_text
|
29 |
-
title_inputs = title_tokenizer
|
30 |
-
|
31 |
-
generated_title = title_tokenizer.decode(
|
32 |
|
33 |
return corrected_text, summary_text, generated_title
|
34 |
|
35 |
-
# Gradio
|
36 |
iface = gr.Interface(
|
37 |
fn=polish_abstract,
|
38 |
-
inputs=gr.Textbox(lines=
|
39 |
outputs=[
|
40 |
-
gr.Textbox(label="β
Corrected Abstract
|
41 |
-
gr.Textbox(label="πͺ
|
42 |
-
gr.Textbox(label="π Suggested Title"),
|
43 |
],
|
44 |
title="π§ AI Abstract & Title Polisher",
|
45 |
-
description="
|
46 |
)
|
47 |
|
48 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load grammar correction model
|
6 |
grammar_model_name = "prithivida/grammar_error_correcter_v1"
|
7 |
grammar_tokenizer = AutoTokenizer.from_pretrained(grammar_model_name)
|
8 |
grammar_model = AutoModelForSeq2SeqLM.from_pretrained(grammar_model_name)
|
9 |
|
10 |
+
# Load summarization pipeline
|
11 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
12 |
|
13 |
+
# Load title generation model (T5)
|
14 |
title_model_name = "t5-base"
|
15 |
title_tokenizer = AutoTokenizer.from_pretrained(title_model_name)
|
16 |
title_model = AutoModelForSeq2SeqLM.from_pretrained(title_model_name)
|
17 |
|
18 |
+
def polish_abstract(text):
|
19 |
+
# --- 1. Grammar Correction ---
|
20 |
+
grammar_input = grammar_tokenizer("gec: " + text, return_tensors="pt", max_length=512, truncation=True)
|
21 |
+
grammar_output = grammar_model.generate(**grammar_input, max_length=512)
|
22 |
+
corrected_text = grammar_tokenizer.decode(grammar_output[0], skip_special_tokens=True)
|
23 |
|
24 |
+
# --- 2. Summary Generation ---
|
25 |
+
summary_text = summarizer(corrected_text, max_length=120, min_length=40, do_sample=False)[0]['summary_text']
|
|
|
26 |
|
27 |
+
# --- 3. Title Generation ---
|
28 |
title_prompt = "generate title: " + corrected_text
|
29 |
+
title_inputs = title_tokenizer(title_prompt, return_tensors="pt", max_length=512, truncation=True)
|
30 |
+
title_output = title_model.generate(**title_inputs, max_length=20, num_beams=5)
|
31 |
+
generated_title = title_tokenizer.decode(title_output[0], skip_special_tokens=True)
|
32 |
|
33 |
return corrected_text, summary_text, generated_title
|
34 |
|
35 |
+
# Gradio interface
|
36 |
iface = gr.Interface(
|
37 |
fn=polish_abstract,
|
38 |
+
inputs=gr.Textbox(lines=12, label="Paste Your Abstract"),
|
39 |
outputs=[
|
40 |
+
gr.Textbox(label="β
Grammar-Corrected Abstract"),
|
41 |
+
gr.Textbox(label="πͺ Concise Summary"),
|
42 |
+
gr.Textbox(label="π AI-Suggested Title"),
|
43 |
],
|
44 |
title="π§ AI Abstract & Title Polisher",
|
45 |
+
description="This tool corrects grammar, summarizes, and suggests a title for biomedical abstracts."
|
46 |
)
|
47 |
|
48 |
iface.launch()
|