File size: 2,171 Bytes
228c3d2
0c5cf49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
from transformers import pipeline

# Load the summarization model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

# Use the grammar correction model via pipeline
grammar_correction_pipe = pipeline("text2text-generation", model="pszemraj/flan-t5-large-grammar-synthesis")

# Function for grammar correction
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."

# Function for text summarization
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."

# Function to combine grammar correction and summarization
def correct_and_summarize(user_input):
    corrected_text = correct_grammar(user_input)  # First correct the grammar
    summary = summarize_text(corrected_text)  # Then summarize the corrected text
    return summary

# Gradio UI setup
with gr.Blocks() as demo:
    gr.Markdown("## Text Summarization and Grammar Correction Assistant")

    # Dropdown to select task
    task = gr.Dropdown(choices=["Summarize Text", "Correct Grammar"], label="Choose a task")

    # Input component for text
    user_input = gr.Textbox(label="Enter your text here:")

    # Output box for displaying the result
    output = gr.Textbox(label="Output", interactive=False)

    # Submit button
    submit_btn = gr.Button("Submit")

    # Function to process the input based on selected task
    def process_input(task, user_input):
        if task == "Summarize Text":
            return correct_and_summarize(user_input)  # Correct grammar, then summarize
        elif task == "Correct Grammar":
            return correct_grammar(user_input)  # Only correct grammar

    # Link the submit button to process the input
    submit_btn.click(process_input, inputs=[task, user_input], outputs=output)

# Launch the Gradio interface
demo.launch()