imseldrith commited on
Commit
97d70c2
·
1 Parent(s): 1652bce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -32
app.py CHANGED
@@ -1,38 +1,71 @@
1
- from flask import Flask, request, render_template
2
- import transformers
3
- from parrot import Parrot
4
- import torch
5
- parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5")
6
-
7
- #from flask import Flask, request, render_template
8
- #import parrot
9
- #import transformers
10
- import time
11
 
12
  app = Flask(__name__)
13
 
14
- @app.route("/")
15
  def index():
16
- return render_template("index.html")
17
 
18
- @app.route("/paraphrase", methods=["POST"])
19
  def paraphrase():
20
- text = request.form["text"]
21
- model = request.form["model"]
22
-
23
- if model == "parrot":
24
- start_time = time.time()
25
- paraphrased_text = parrot.augment(input_phrase=text, use_gpu=False)
26
- processing_time = time.time() - start_time
27
-
28
- elif model == "transformers":
29
- start_time = time.time()
30
- paraphrased_text = transformers.paraphrase(text)
31
- processing_time = time.time() - start_time
32
-
33
- processing_percentage = 100 * processing_time / 30
34
-
35
- return render_template("paraphrase.html", original_text=text, paraphrased_text=paraphrased_text, processing_percentage=processing_percentage)
36
-
37
- if __name__ == "__main__":
38
- app.run(host="0.0.0.0",port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)