Spaces:
Running
Running
Commit
·
97d70c2
1
Parent(s):
1652bce
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,71 @@
|
|
1 |
-
from flask import Flask,
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
#import transformers
|
10 |
-
import time
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
14 |
-
@app.route(
|
15 |
def index():
|
16 |
-
return render_template(
|
17 |
|
18 |
-
@app.route(
|
19 |
def paraphrase():
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request
|
2 |
+
from textblob import TextBlob
|
3 |
+
import re
|
4 |
+
import nltk
|
5 |
+
from nltk.translate.bleu_score import sentence_bleu
|
6 |
+
from nltk.corpus import wordnet
|
7 |
+
from nltk.corpus import sentiwordnet as swn
|
8 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
|
|
|
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
+
@app.route('/')
|
13 |
def index():
|
14 |
+
return render_template('index.html')
|
15 |
|
16 |
+
@app.route('/paraphrase', methods=['POST'])
|
17 |
def paraphrase():
|
18 |
+
input_text = request.form['input_text']
|
19 |
+
input_text = re.sub(r'[^\w\s]', '', input_text) # remove special characters
|
20 |
+
|
21 |
+
# Correct grammar using TextBlob
|
22 |
+
input_text = str(TextBlob(input_text).correct())
|
23 |
+
|
24 |
+
# Summarize the text using TextBlob
|
25 |
+
summarized_text = str(TextBlob(input_text).summarize())
|
26 |
+
|
27 |
+
# Paraphrase the text
|
28 |
+
paraphrased_text = generate_paraphrase(input_text)
|
29 |
+
|
30 |
+
# Emotion detection
|
31 |
+
emotion = detect_emotion(input_text)
|
32 |
+
|
33 |
+
# Named Entity Recognition
|
34 |
+
entities = nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(input_text)))
|
35 |
+
|
36 |
+
# Part-of-Speech Tagging
|
37 |
+
pos_tags = nltk.pos_tag(nltk.word_tokenize(input_text))
|
38 |
+
|
39 |
+
# Sentiment Analysis
|
40 |
+
sentiment = SentimentIntensityAnalyzer().polarity_scores(input_text)
|
41 |
+
|
42 |
+
return render_template('index.html', input_text=input_text, summarized_text=summarized_text, paraphrased_text=paraphrased_text, entities=entities, pos_tags=pos_tags, sentiment=sentiment, emotion=emotion)
|
43 |
+
|
44 |
+
def generate_paraphrase(text):
|
45 |
+
# Use TextBlob to generate paraphrased text
|
46 |
+
paraphrased_text = str(TextBlob(text).words)
|
47 |
+
|
48 |
+
# Custom synonyms
|
49 |
+
custom_synonyms = [('happy', 'joyful'), ('sad', 'unhappy')]
|
50 |
+
for syn in custom_synonyms:
|
51 |
+
paraphrased_text = paraphrased_text.replace(syn[0], syn[1])
|
52 |
+
|
53 |
+
return paraphrased_text
|
54 |
+
|
55 |
+
def detect_emotion(text):
|
56 |
+
# Use SentiWordNet to detect emotion in text
|
57 |
+
emotions = []
|
58 |
+
words = nltk.word_tokenize(text)
|
59 |
+
for word in words:
|
60 |
+
synset = swn.senti_synsets(word)
|
61 |
+
if len(synset) > 0:
|
62 |
+
emotions.append(synset[0].pos_score() - synset[0].neg_score())
|
63 |
+
if emotions:
|
64 |
+
emotion = max(emotions)
|
65 |
+
else:
|
66 |
+
emotion = 0
|
67 |
+
|
68 |
+
return 'positive' if emotion > 0 else 'negative' if emotion < 0 else 'neutral'
|
69 |
+
|
70 |
+
if __name__ == '__main__':
|
71 |
+
app.run(host="0.0.0.0",port=7860,debug=True)
|