|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") |
|
|
|
|
|
grammar_correction_pipe = pipeline("text2text-generation", model="pszemraj/grammar-synthesis-small") |
|
|
|
|
|
def correct_grammar(user_input): |
|
if user_input.strip(): |
|
corrected_text = grammar_correction_pipe(user_input)[0]['generated_text'] |
|
return corrected_text |
|
else: |
|
return "Please enter some text for grammar correction." |
|
|
|
|
|
def summarize_text(user_input): |
|
if user_input.strip(): |
|
summary = summarizer(user_input, max_length=150, min_length=50, do_sample=False)[0]['summary_text'] |
|
return summary |
|
else: |
|
return "Please enter some text to summarize." |
|
|
|
|
|
def correct_and_summarize(user_input): |
|
corrected_text = correct_grammar(user_input) |
|
summary = summarize_text(corrected_text) |
|
return summary |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## Text Summarization and Grammar Correction Assistant") |
|
|
|
|
|
task = gr.Dropdown(choices=["Summarize Text", "Correct Grammar"], label="Choose a task") |
|
|
|
|
|
user_input = gr.Textbox(label="Enter your text here:") |
|
|
|
|
|
output = gr.Textbox(label="Output", interactive=False) |
|
|
|
|
|
submit_btn = gr.Button("Submit") |
|
|
|
|
|
def process_input(task, user_input): |
|
if task == "Summarize Text": |
|
return correct_and_summarize(user_input) |
|
elif task == "Correct Grammar": |
|
return correct_grammar(user_input) |
|
|
|
|
|
submit_btn.click(process_input, inputs=[task, user_input], outputs=output) |
|
|
|
|
|
demo.launch() |
|
|