-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
402 lines (294 loc) · 12.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from flask import Flask, Response, request, jsonify, render_template
from final_summary import *
from Meeting_duration import *
from keywords import *
from PyPDF2 import PdfReader
import PyPDF2
from transformers import pipeline
import docx
import webvtt
import csv
import pandas as pd
import io
import random
import string
import os
import re
import speech_recognition as sr
import os
from pydub import AudioSegment
from pydub.silence import split_on_silence
from moviepy.editor import VideoFileClip
import shutil
from audio_to_text import *
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/summary_page',methods=['GET','POST'])
def summary_page():
return render_template('summary_page.html')
@app.route('/attendees_page',methods=['GET','POST'])
def attendees_page():
return render_template('attendees_page.html')
meet_attendee_list = []
@app.route('/attendees_result',methods=['GET','POST'])
def attendees_result():
if request.method == 'POST':
transcript = request.files['myfile']
transcript.save(transcript.filename)
with open(transcript.filename, 'r') as f:
vtt_lines = f.readlines()
timestamps = []
for line in vtt_lines:
if regex.match(r'^\d{2}:\d{2}:\d{2}.\d{3} --> \d{2}:\d{2}:\d{2}.\d{3}$', line.strip()):
timestamps.append(line.strip())
formatted_timestamps = []
for ts in timestamps:
start, end = ts.split(' --> ')
start = datetime.strptime(start, '%H:%M:%S.%f')
end = datetime.strptime(end, '%H:%M:%S.%f')
formatted_timestamps.append((start, end))
# print(type(end))
# print(start,"----",end)
# print(formatted_timestamps)
lenn = len(formatted_timestamps)
# print(formatted_timestamps[lenn - 1])
dt = formatted_timestamps[lenn - 1]
last_len = len(dt)
aaa = dt[last_len - 1]
aaa
minutes = aaa.minute
hours = aaa.hour
secs = aaa.second
f = open(transcript.filename, "r")
string = f.read()
# Stores the indices and attendees
dels = []
namee = []
# ans = ""
for i in range(len(string)):
# If opening delimiter
# is encountered
if (string[i] == '<') :
dels.append(i + 2)
# If closing delimiter
# is encountered
elif (string[i] == '>' and len(dels) != 0):
# Extract the position
# of opening delimiter
pos = dels[-1]
dels.pop()
# Length of substring
length = i - 1 - pos
# Extract the substring
ans = string[pos + 1 : pos + 1 + length]
#replacing blank space between names with _
names = ans.replace(" ", "_")
namee.append(names)
meet_attendees = set(namee)
meet_attendees.remove("")
global meet_attendee_list
meet_attendee_list = list(meet_attendees)
S = ', '.join(meet_attendees)
# file = open("Active_Attendees.txt","w")
# file.write("Total Active attendees: ")
# file.write(str(len(meet_attendees)))
# file.write("\nTotal Active attendees Name: \n")
# file.write(str(S))
# file.close()
# ***************************Download file********************
# column_name = "Active_Attendees"
# with open('example.csv', mode='a', newline='') as file:
# # Create a writer object
# writer = csv.writer(file)
# # Write the data to the CSV file
# writer.writerow([column_name])
# df = pd.read_csv('example.csv')
# # Create a new list of values to insert into the column
# new_column_data = list(meet_attendees)
# # Append the new data to the desired column
# df[column_name] = df[column_name].append(pd.Series(new_column_data))
# # Write the updated DataFrame to a new CSV file
# df.to_csv('updated_example.csv', index=False)
# **********************
return render_template('attendees_result.html',
hour = hours,
minutes = minutes,
sec = secs,
meet_attendees_name =str(S),
total_att = len(meet_attendees),
file_name = transcript.filename
)
# Route for TextField Summarization
@app.route('/summarize_text',methods=['POST'])
def summarize_text():
if request.method == 'POST':
text_summary = request.form['text_summary']
Summary = generate_summary(text_summary)
#convert list into string
global summary_string
summary_string = ' '.join(Summary)
# **********Count total words and sentences***************
# text = "This is a sample sentence. Here is another one!"
# Count the number of words
words = re.findall(r'\w+', summary_string)
num_words = len(words)
# Count the number of sentences
sentences = re.findall(r'[^\s][^.!?]*[.!?]', summary_string)
num_sentences = len(sentences)
Keywords = keywords(text_summary)
return render_template('summary_result.html',
text_summary=summary_string,
total_words = num_words,
total_sentences = num_sentences,
top_keywords = Keywords)
# Route for File upload Summarization
summary_string = ""
@app.route('/summarize',methods=['POST'])
def summarize():
if request.method == 'POST':
#fetching file from html form
text = request.files['fileUpload']
text.save(text.filename)
#for getting file extension
find_file = text.filename
x = find_file.split(".")
extension = x[1]
if extension == "pdf":
reader = PdfReader(text.filename)
page = reader.pages[0] #for single page
file_text = page.extractText()
elif extension == "docx":
doc = docx.Document(text.filename)
# Extract the text from the document
file_text = '\n'.join([paragraph.text for paragraph in doc.paragraphs])
elif extension == "vtt":
# Load VTT file
captions = webvtt.read(text.filename)
# Iterate through captions and extract text
file_text = ''
for caption in captions:
file_text += caption.text + '\n'
# print("Original text after removing stopwords =========>>>> ",file_text)
Keywords = keywords(file_text)
else:
#for txt file
f = open(text.filename, "r", encoding="utf8")
file_text = f.read()
Keywords = keywords(file_text)
Summary = generate_summary(file_text)
#convert list into string
global summary_string
summary_string = ' '.join(Summary)
# **********Count total words and sentences***************
# text = "This is a sample sentence. Here is another one!"
# Count the number of words
words = re.findall(r'\w+', summary_string)
num_words = len(words)
# Count the number of sentences
sentences = re.findall(r'[^\s][^.!?]*[.!?]', summary_string)
num_sentences = len(sentences)
# *****************************************************
return render_template('summary_result.html',
text_summary=summary_string,
file_name = text.filename,
total_words = num_words,
total_sentences = num_sentences,
top_keywords = Keywords)
# ******************END************************
@app.route('/download_csv')
def download_csv():
insert_data = ["Active_attendees"]
insert_data.extend(meet_attendee_list)
insert_names= [[x] for x in insert_data]
# Create a buffer to write the CSV data to
output = io.StringIO()
# Use the csv module to write the data to the buffer
writer = csv.writer(output)
for row in insert_names:
writer.writerow(row)
# Set the appropriate response headers
response = Response(output.getvalue(), mimetype='text/csv')
# *******Generate random file name***
# Generate a sequence of 8 random characters (4 letters and 4 numbers)
random_chars = ''.join(random.choices(string.ascii_lowercase + string.digits, k=16))
# Insert an underscore after every 4 characters
result = '_'.join([random_chars[i:i+4] for i in range(0, len(random_chars), 4)])
file_name = "Attendance_" + result +".csv"
# ************************
response.headers.set("Content-Disposition", "attachment", filename=file_name)
return response
# ***********************Download Summary***************************
@app.route('/download_summary',methods=['POST'])
def download_summary():
if request.method == 'POST':
text_summary = request.form['text_summary']
# Add a summary in the file
# Delete a file
os.remove('summary.txt')
with open('summary.txt', 'a') as f:
f.write(text_summary + '\n')
# return 'Data added successfully!'
with open('summary.txt', 'r') as f:
data = f.read()
return Response(
data,
mimetype="text/plain",
headers={"Content-disposition":
"attachment; filename=summary.txt"})
# ****************************************************************
# ***********************Voice to Text Page***************************
@app.route('/voice_to_text')
def voice_to_text():
return render_template('voice_to_text.html')
# ****************************************************************
# ***********************Voice to Text Result Page****************************
@app.route('/voice_to_text_result', methods=['GET', 'POST'])
def voice_to_text_result():
r = sr.Recognizer()
if request.method == 'POST':
video_file = request.files['fileUpload']
video_file_name = video_file.filename
video_folder_name = "video_chunks"
if not os.path.isdir(video_folder_name):
os.mkdir(video_folder_name)
target_folder = "video_chunks"
video_file.save(os.path.join(target_folder, video_file.filename))
# *************Video to audio*************8
# clip = VideoFileClip("./Input_Files/sample_video.mp4")
path = "./video_chunks/" + video_file_name
clip = VideoFileClip(path)
# Extract the audio from the video
audio = clip.audio
# *******Generate random file name***
# Generate a sequence of 8 random characters (4 letters and 4 numbers)
random_chars1 = ''.join(random.choices(string.ascii_lowercase + string.digits, k=16))
# Insert an underscore after every 4 characters
result = '_'.join([random_chars1[i:i+4] for i in range(0, len(random_chars1), 4)])
video_file_name_id = "video_to_audio" + result +".wav"
# ***************************************
# Save the audio to a WAV file
audio.write_audiofile(video_file_name_id, codec='pcm_s16le')
# """Split audio into chunks and apply speech recognition"""
# Open audio file with pydub
whole_text = convert_audio_to_text(video_file_name_id)
os.remove(video_file_name_id)
shutil.rmtree("./audio-chunks")
# shutil.rmtree("./video_chunks")
# Return text for all chunks
return render_template('voice_result.html', whole_text= whole_text)
# ****************************************************************
@app.route('/audio_to_text', methods=['GET', 'POST'])
def audio_to_text():
r = sr.Recognizer()
if request.method == 'POST':
audio_file = request.files['fileUpload']
audio_file_name = audio_file.filename
whole_text = convert_audio_to_text(audio_file_name)
shutil.rmtree("./audio-chunks")
# shutil.rmtree("./video_chunks")
# Return text for all chunks
return render_template('voice_result.html', whole_text= whole_text)
if __name__ == '__main__':
app.run(debug=True, port=5000, threaded=True)