cas106 commited on
Commit
e959bb8
1 Parent(s): 7d0dd83

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nltk
2
+ nltk.download('punkt')
3
+ nltk.download('averaged_perceptron_tagger')
4
+ nltk.download('brown')
5
+
6
+ import spacy
7
+ from spacy import displacy
8
+ from collections import Counter
9
+ import en_core_web_sm
10
+
11
+ from transformers import pipeline
12
+ summarization = pipeline("summarization", model = "facebook/bart-large-cnn")
13
+
14
+ from textblob import TextBlob
15
+
16
+ import gradio as gr
17
+
18
+ def story(txt):
19
+
20
+ #Find all the names that appear in the story and define the function for finding the most appeared name
21
+
22
+ nlp = en_core_web_sm.load()
23
+ doc = nlp(txt)
24
+ labellist = []
25
+ namelist = []
26
+ orglist = [(X.text, X.label_) for X in doc.ents]
27
+ def most_common(List):
28
+ return max(set(List), key=List.count)
29
+ for i in orglist:
30
+ if i[1] == ("PERSON"):
31
+ labellist.append(i[0])
32
+ for i in labellist:
33
+ if i not in namelist:
34
+ namelist.append(i)
35
+
36
+ #Generate a short summary for the story
37
+
38
+ summary = summarization(txt)
39
+
40
+ #Determine whether the story is positive, negative or neutral
41
+
42
+ count = 0
43
+ count2 = 0
44
+ blob = TextBlob(txt)
45
+ for sentence in blob.sentences:
46
+ for i in sentence.sentiment:
47
+ if sentence.sentiment[0]>0:
48
+ count += 1
49
+ if sentence.sentiment[0]<0:
50
+ count2 += 1
51
+ if count>count2:
52
+ sentiment = ("The story is positive.")
53
+ elif count<count2:
54
+ sentiment = ("The stroy is negative.")
55
+ else:
56
+ sentiment = ("The story is neutral.")
57
+ #Determine the output
58
+ output = ("All names appeared:", namelist, "The most appeared name is", most_common(labellist)+".", "Short summary:", summary, sentiment)
59
+ return(output)
60
+
61
+ #create web app using Gradio
62
+
63
+ demo = gr.Interface(fn = story, inputs="text", outputs="text")
64
+
65
+ demo.launch()