Technozam commited on
Commit
24c62f7
·
1 Parent(s): 8f29de9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +207 -0
app.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """NotesGenerator.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1eq-p4wE1YK7TJ6TtBIV8BJr8SnfKzYdB
8
+ """
9
+
10
+ from textwrap3 import wrap
11
+
12
+ text = """A Lion lay asleep in the forest, his great head resting on his paws. A timid little Mouse came upon him unexpectedly, and in her fright and haste to
13
+ get away, ran across the Lion's nose. Roused from his nap, the Lion laid his huge paw angrily on the tiny creature to kill her. "Spare me!" begged
14
+ the poor Mouse. "Please let me go and some day I will surely repay you." The Lion was much amused to think that a Mouse could ever help him. But he
15
+ was generous and finally let the Mouse go. Some days later, while stalking his prey in the forest, the Lion was caught in the toils of a hunter's
16
+ net. Unable to free himself, he filled the forest with his angry roaring. The Mouse knew the voice and quickly found the Lion struggling in the net.
17
+ Running to one of the great ropes that bound him, she gnawed it until it parted, and soon the Lion was free. "You laughed when I said I would repay
18
+ you," said the Mouse. "Now you see that even a Mouse can help a Lion." """
19
+ for wrp in wrap(text, 150):
20
+
21
+ print (wrp)
22
+ print ("\n")
23
+
24
+ import torch
25
+ from transformers import T5ForConditionalGeneration,T5Tokenizer
26
+ summary_model = T5ForConditionalGeneration.from_pretrained('t5-base')
27
+ summary_tokenizer = T5Tokenizer.from_pretrained('t5-base')
28
+
29
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30
+ summary_model = summary_model.to(device)
31
+
32
+ import random
33
+ import numpy as np
34
+
35
+ def set_seed(seed: int):
36
+ random.seed(seed)
37
+ np.random.seed(seed)
38
+ torch.manual_seed(seed)
39
+ torch.cuda.manual_seed_all(seed)
40
+
41
+ set_seed(42)
42
+
43
+ import nltk
44
+ # nltk.download('punkt')
45
+ # nltk.download('brown')
46
+ # nltk.download('wordnet')
47
+ from nltk.corpus import wordnet as wn
48
+ from nltk.tokenize import sent_tokenize
49
+
50
+ def postprocesstext (content):
51
+ final=""
52
+ for sent in sent_tokenize(content):
53
+ sent = sent.capitalize()
54
+ final = final +" <br>"+sent +"<br>"
55
+ return final
56
+
57
+
58
+ def summarizer(text,model,tokenizer):
59
+ text = text.strip()
60
+ text = "summarize: "+text
61
+ # print (text)
62
+ max_len = 512
63
+ encoding = tokenizer.encode_plus(text,max_length=max_len, pad_to_max_length=False,truncation=True, return_tensors="pt").to(device)
64
+
65
+ input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]
66
+
67
+ outs = model.generate(input_ids=input_ids,
68
+ attention_mask=attention_mask,
69
+ early_stopping=True,
70
+ num_beams=3,
71
+ num_return_sequences=1,
72
+ no_repeat_ngram_size=2,
73
+ min_length = 75,
74
+ max_length=300)
75
+
76
+
77
+ dec = [tokenizer.decode(ids,skip_special_tokens=True) for ids in outs]
78
+ summary = dec[0]
79
+ print(summary)
80
+ summary = postprocesstext(summary)
81
+ summary= summary.strip()
82
+
83
+ return summary
84
+
85
+
86
+ summarized_text = summarizer(text,summary_model,summary_tokenizer)
87
+
88
+
89
+ print ("\noriginal Text >>")
90
+ for wrp in wrap(text, 150):
91
+ print (wrp)
92
+ print ("\n")
93
+ print ("Summarized Text >>")
94
+ for wrp in wrap(summarized_text, 150):
95
+ print (wrp)
96
+ print ("\n")
97
+
98
+ """# **UI by using Gradio**"""
99
+
100
+ import mysql.connector
101
+ import datetime;
102
+
103
+ mydb = mysql.connector.connect(
104
+ host="qtechdb-1.cexugk1h8rui.ap-northeast-1.rds.amazonaws.com",
105
+ user="admin",
106
+ password="F3v2vGWzb8vaniE3nqzi",
107
+ database="spring_social"
108
+ )
109
+
110
+ import gradio as gr
111
+
112
+ context = gr.Textbox(lines=10, placeholder="Enter paragraph/content here...", label="Text")
113
+ subject = gr.Textbox(placeholder="Enter subject/title here...", label="Text")
114
+ output = gr.Markdown( label="Notes")
115
+
116
+ def generate_question_text(context,subject):
117
+ summary_text = summarizer(context,summary_model,summary_tokenizer)
118
+ for wrp in wrap(summary_text, 150):
119
+ print (wrp)
120
+ # np = get_keywords(context,summary_text,total)
121
+ # print ("\n\nNoun phrases",np)
122
+ output="<b style='color:black;'>Notes and key points of the topic are:</b><br>"
123
+ summary = summary_text
124
+ output = output+ summary
125
+
126
+ mycursor = mydb.cursor()
127
+ timedate = datetime.datetime.now()
128
+
129
+ sql = "INSERT INTO notestexts (subject, input, output, timedate) VALUES (%s,%s, %s,%s)"
130
+ val = (subject, context, output, timedate)
131
+ mycursor.execute(sql, val)
132
+
133
+ mydb.commit()
134
+
135
+ print(mycursor.rowcount, "record inserted.")
136
+
137
+ return output
138
+
139
+ iface = gr.Interface(
140
+ fn=generate_question_text,
141
+ inputs=[context,subject],
142
+ outputs=[output], css=".gradio-container {background-image: url('file=blue.jpg')}",
143
+ allow_flagging="manual",flagging_options=["Save Data"])
144
+
145
+ iface.launch(debug=True, share=True)
146
+
147
+ def generate_question(context,subjectfile):
148
+ summary_text = summarizer(context,summary_model,summary_tokenizer)
149
+ for wrp in wrap(summary_text, 150):
150
+ print (wrp)
151
+ # np = get_keywords(context,summary_text,total)
152
+ # print ("\n\nNoun phrases",np)
153
+ output="<b style='color:black;'>Notes and key points of the topic are:</b><br>"
154
+ summary = summary_text
155
+ output = output+ summary
156
+
157
+ return output
158
+
159
+ import glob
160
+ import os.path
161
+ import pandas as pd
162
+
163
+ file =None
164
+
165
+
166
+ def filecreate(x,subjectfile):
167
+
168
+ with open(x.name) as fo:
169
+ text = fo.read()
170
+ # print(text)
171
+ generated = generate_question(text,subject)
172
+
173
+ mycursor = mydb.cursor()
174
+
175
+ timedate= datetime.datetime.now()
176
+
177
+ sql = "INSERT INTO notesfiles (subject, input, output, timedate) VALUES (%s,%s, %s,%s)"
178
+ val = (subject, text, generated, timedate)
179
+ mycursor.execute(sql, val)
180
+
181
+ mydb.commit()
182
+
183
+ print(mycursor.rowcount, "record inserted.")
184
+ # return text
185
+ return generated
186
+
187
+ # filecreate(file)
188
+
189
+ import gradio as gr
190
+
191
+ context = gr.HTML(label="Text")
192
+ subjectfile = gr.Textbox(placeholder="Enter subject/title here...", label="Text")
193
+
194
+ file = gr.File()
195
+
196
+ fface = gr.Interface(
197
+ fn=filecreate,
198
+ inputs=[file,subjectfile],
199
+ outputs=context,
200
+ css=".gradio-container {background-image: url('file=blue.jpg')}",
201
+ allow_flagging="manual",flagging_options=["Save Data"])
202
+
203
+
204
+ # fface.launch(debug=True, share=True)
205
+
206
+ demo = gr.TabbedInterface([iface, fface], ["Text", "Upload File"], css=".gradio-container {background-image: url('file=blue.jpg')}")
207
+ demo.launch(debug=True, share=True)