Grammarly_Beta / app.py
Priyanhsu's picture
Update app.py
21cfe5e
from transformers import *
import gradio as gr
import nltk
from nltk.tokenize import sent_tokenize
from language_tool_python import LanguageTool
from nltk.sentiment import SentimentIntensityAnalyzer
import torch
nltk.download('vader_lexicon')
nltk.download('stopwords')
model = PegasusForConditionalGeneration.from_pretrained("tuner007/pegasus_paraphrase")
tokenizer = PegasusTokenizerFast.from_pretrained("tuner007/pegasus_paraphrase")
def get_paraphrased_sentences(paragraph):
num_return_sequences = 1
num_beams = 1
# Split the paragraph into sentences
sentences = paragraph.split(". ")
paraphrased_sentences = []
for sentence in sentences:
# Tokenize the sentence
inputs = tokenizer([sentence], truncation=True, padding="longest", return_tensors="pt")
# Generate the paraphrased sentences
outputs = model.generate(
**inputs,
num_beams=num_beams,
num_return_sequences=num_return_sequences,
)
# Decode the generated sentences using the tokenizer to get them back to text
paraphrased = tokenizer.batch_decode(outputs, skip_special_tokens=True)
paraphrased_sentences.extend(paraphrased)
return paraphrased_sentences
# Initialize LanguageTool and SentimentIntensityAnalyzer objects
tool = LanguageTool('en-US')
sia = SentimentIntensityAnalyzer()
def grammar_check(text):
matches = tool.check(text)
corrected_text = tool.correct(text)
return corrected_text, matches
def analyze_sentiment(text):
sentiment_scores = sia.polarity_scores(text)
sentiment = ""
if sentiment_scores['compound'] > 0:
sentiment = "Positive"
elif sentiment_scores['compound'] == 0:
sentiment = "Neutral"
else:
sentiment = "Negative"
return sentiment
def extract_wrong_words(matches):
wrong_words = set()
for match in matches:
wrong_words.update(match.replacements)
return wrong_words
def dropdown_select(text, output_type):
corrected_text, matches = grammar_check(text)
sentiment_result = analyze_sentiment(corrected_text)
words = corrected_text.split()
wrong_words = extract_wrong_words(matches)
if output_type == "Grammar":
return corrected_text, None, sentiment_result, len(words), wrong_words
elif output_type == "Paraphrase":
paraphrase_text = get_paraphrased_sentences(corrected_text)
return None, paraphrase_text, sentiment_result, len(words), wrong_words
elif output_type == "All":
paraphrase_text = get_paraphrased_sentences(corrected_text)
return corrected_text, paraphrase_text, sentiment_result, len(words), wrong_words
else:
return None,None,None,None,None,
import gradio as gr
iface = gr.Interface(
fn=dropdown_select,
inputs=[
gr.inputs.Textbox(placeholder="Enter your text here..."),
gr.inputs.Dropdown(["All", "Grammar", "Paraphrase"], label="Select Output Type", default=None)
],
outputs=[
gr.outputs.Textbox(label="Modified Grammar"),
gr.outputs.Textbox(label="Paraphrased Content"),
gr.outputs.Textbox(label="Sentiment Analysis"),
gr.outputs.Textbox(label="Total Words Count"),
gr.outputs.Textbox(label="Detected Wrong Words")
],
title="CSharpGrammarly",
description="CSHARPGRAMMARLYTOOL",
allow_flagging=False # Disable flagging for simplicity
)
iface.launch(debug=True)