Spaces:
Running
Running
import re | |
from textblob import TextBlob | |
import nltk | |
import emopy | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import torch | |
import spacy | |
from flask import Flask, request, render_template | |
nltk.download('averaged_perceptron_tagger') | |
nltk.download('punkt') | |
nltk.download('maxent_ne_chunker') | |
nltk.download('words') | |
nlp = spacy.load("en_core_web_sm") | |
app = Flask(__name__) | |
def index(): | |
if request.method == 'POST': | |
text = request.form['text'] | |
paraphrase_option = request.form['paraphrase_option'] | |
remove_special_chars = request.form.get('remove_special_chars') | |
summarize = request.form.get('summarize') | |
correct_grammar = request.form.get('correct_grammar') | |
if correct_grammar: | |
text = str(TextBlob(text).correct()) | |
if remove_special_chars: | |
text = re.sub(r'[^\w\s]', '', text) | |
if summarize: | |
doc = nlp(text) | |
sentences = [sent.text for sent in doc.sents] | |
text = " ".join(sentences[:3]) + "..." | |
if paraphrase_option == 'repeat': | |
text = re.sub(r'\b(\w+)\b', r'\1', text) | |
elif paraphrase_option == 'emotion_detector': | |
emotion = emopy.EmotionDetector() | |
emotions = emotion.detect_emotion(text) | |
emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral'] | |
index = np.argmax(emotions) | |
emotion = emotion_labels[index] | |
if emotion == 'happy': | |
text = text.upper() | |
elif emotion == 'sad': | |
text = text.lower() | |
else: | |
text = text.capitalize() | |
return render_template('index.html', text=text) | |
return render_template('index.html') | |
if __name__ == '__main__': | |
app.run(host="0.0.0.0",port=7860,debug=True) |