-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
353 lines (273 loc) · 13.1 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
import os
from pyannote.audio import Pipeline
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from flask import Flask, request, jsonify
from flask_cors import CORS
from transformers import pipeline
import spacy
from moviepy.editor import VideoFileClip
from googletrans import Translator
from gensim.summarization import summarize
import subprocess
import os
from dotenv import load_dotenv
import pysrt
load_dotenv()
HUGGING_FACE_API_KEY = os.environ.get("HUGGING_FACE_API_KEY")
def convert_srt_to_string(srt_file_path):
subs = pysrt.open(srt_file_path)
converted_content = ''
finstr = ''
for sub in subs:
finstr += sub.text + ' '
start_time = sub.start.to_time().strftime("%H:%M:%S,%f")[:-3]
end_time = sub.end.to_time().strftime("%H:%M:%S,%f")[:-3]
converted_content += f"\n<strong>[{start_time} --> {end_time}]</strong>: {sub.text}\n"
return converted_content.strip(), finstr
def remove_duplicates(lst):
return list(set(lst))
app = Flask(__name__)
CORS(app, origins="http://localhost:3000", supports_credentials=True)
nlp = spacy.load("en_core_web_sm")
diarization_pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token=HUGGING_FACE_API_KEY)
@app.route('/api/analyze-audio/<string:text>', methods=['POST'])
def analyze_text(text):
if len(text) == 0:
return jsonify({'error': 'No text provided'}), 400
doc = nlp(text)
person_count = len([ent.text for ent in doc.ents if ent.label_ == "PERSON"])
topics = [token.text for token in doc if token.is_alpha and not token.is_stop]
name = request.form['name']
file = request.files['file']
name = name.strip()
name = name.replace(" ", "_")
directory_path = os.path.join('uploads', name)
filesname, extension = os.path.splitext(file.filename)
filesname = filesname.strip()
filesname = filesname.replace(" ", "_")
text_path = os.path.join(directory_path, "{}.txt".format(filesname))
with open(text_path, 'a') as fil:
fil.write("\nPerson Count: {}\n".format(str(person_count + 1)))
diarization = diarization_pipeline(f"./uploads/{name}/{filesname}.mp3")
temp_lis = []
for _, _, speaker in diarization.itertracks(yield_label=True):
temp_lis.append(speaker)
temp_lis = remove_duplicates(temp_lis)
return jsonify({'person_count' : len(temp_lis) , 'topic' : topics[0]})
@app.route('/api/convert-video-to-mp3', methods=['POST'])
def convert_video_to_mp3():
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
video_file = request.files['file']
name = request.form['name']
name = name.strip()
name = name.replace(" ", "_")
if video_file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if video_file:
try:
print(video_file.filename)
directory_path = os.path.join('uploads', name)
os.makedirs(directory_path, exist_ok=True)
video_path = os.path.join(directory_path, video_file.filename)
filesname, extension = os.path.splitext(video_file.filename)
audio_path = os.path.join(directory_path, "{}.mp3".format(filesname))
video_file.save(video_path)
video_clip = VideoFileClip(video_path)
audio_clip = video_clip.audio
audio_clip.write_audiofile(audio_path)
audio_clip.close()
video_clip.close()
return jsonify({'message': 'Video converted to MP3 successfully', 'audio_file': audio_path}), 200
except Exception as e:
return jsonify({'error': 'Conversion error', 'details': str(e)}), 500
@app.route('/api/convert-mp3-to-text', methods=['POST'])
def convert_mp3_to_text():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
name = request.form['name']
name = name.strip()
name = name.replace(" ", "_")
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
filesname, extension = os.path.splitext(file.filename)
filesname = filesname.strip()
filesname = filesname.replace(" ", "_")
full_filename = filesname+extension
directory_path = os.path.join('uploads', name)
os.makedirs(directory_path, exist_ok=True)
mp3_filename = os.path.join(directory_path, full_filename)
file.save(mp3_filename)
segment_dir = 'segments'
os.makedirs(segment_dir, exist_ok=True)
#-----------------------------------------------------------------------------------------
command1 = f"ffmpeg -i ./uploads/{name}/{filesname}.mp3 -ar 16000 -ac 1 -c:a pcm_s16le ./uploads/{name}/final.wav"
command2 = f".\\WHISPER_EXE_FILES\\main.exe -f ./uploads/{name}/final.wav -m .\\WHISPER_EXE_FILES\\ggml-base.en.bin -osrt -t 8 --language auto --translate true"
result = subprocess.run(command1, shell=True, capture_output=True, text=True)
result2 = subprocess.run(command2, shell=True, capture_output=True, text=True)
srt_file_path = f'./uploads/{name}/final.wav.srt'
text_path = os.path.join(directory_path, "{}.txt".format(filesname))
result_string, joined_text = convert_srt_to_string(srt_file_path)
os.remove(f"./uploads/{name}/final.wav")
#-----------------------------------------------------------------------------------------
diarization = diarization_pipeline(f'./uploads/{name}/{filesname}.mp3')
temp_str = "\n\n-------------------------------\n"
for turn, _, speaker in diarization.itertracks(yield_label=True):
x = f'start={turn.start:.1f}s stop={turn.end:.1f}s ---> {speaker}\n\n'
temp_str = temp_str + x
result_string = result_string + temp_str
result_string = result_string.replace("\n", "<br>")
with open(text_path, 'w') as fil:
fil.write("\nConverted Text: {}\n".format(str(joined_text)))
return jsonify({'text': joined_text, 'whisper_str': result_string})
@app.route('/api/convert-mp4-to-text', methods=['POST'])
def convert_mp4_to_text():
language = request.form['language']
file = request.files['file']
name = request.form['name']
name = name.strip()
name = name.replace(" ", "_")
filesname, extension = os.path.splitext(file.filename)
filesname = filesname.strip()
filesname = filesname.replace(" ", "_")
file = os.path.abspath('./uploads/{}/{}.mp3'.format(name, filesname))
directory_path = os.path.join('uploads', name)
os.makedirs(directory_path, exist_ok=True)
if file:
mp3_filename = file
command1 = f"ffmpeg -i ./uploads/{name}/{filesname}.mp3 -ar 16000 -ac 1 -c:a pcm_s16le ./uploads/{name}/final.wav"
result = subprocess.run(command1, shell=True, capture_output=True, text=True)
segment_dir = 'segments'
try:
if not os.path.exists(segment_dir):
os.mkdir(segment_dir)
except Exception as e:
print(f"Error creating directory: {e}")
command2 = f".\\WHISPER_EXE_FILES\\main.exe -f ./uploads/{name}/final.wav -m .\\WHISPER_EXE_FILES\\ggml-base.en.bin -osrt -t 8 --language auto --translate true "
result2 = subprocess.run(command2, shell=True, capture_output=True, text=True)
srt_file_path = f'./uploads/{name}/final.wav.srt'
text_path = os.path.join(directory_path, "{}.txt".format(filesname))
result_string, joined_text = convert_srt_to_string(srt_file_path)
os.remove(f"./uploads/{name}/final.wav")
# -----------------------------------------------------------------------------------------
diarization = diarization_pipeline(f'./uploads/{name}/{filesname}.mp3')
temp_str = "\n\n-------------------------------\n"
for turn, _, speaker in diarization.itertracks(yield_label=True):
x = f'start={turn.start:.1f}s stop={turn.end:.1f}s ---> {speaker}\n\n'
temp_str = temp_str + x
result_string = result_string + temp_str
result_string = result_string.replace("\n", "<br>")
with open(text_path, 'w') as fil:
fil.write("\nConverted Text: {}\n".format(str(joined_text)))
return jsonify({'text': joined_text, 'whisper_str': result_string})
@app.route('/api/translate_toar/<string:text>', methods=['GET'], endpoint='translate_to_ar')
def translate_to_ar(text):
translator = Translator()
arabic_translation = translator.translate(text, src='auto', dest='ar').text
return jsonify({'translated_txt': arabic_translation})
@app.route('/api/translate_totr/<string:text>', methods=['GET'], endpoint='translate_to_tr')
def translate_to_tr(text):
translator = Translator()
turkish_translation = translator.translate(text, src='auto', dest='tr').text
return jsonify({'translated_txt': turkish_translation})
@app.route('/api/translate_toen/<string:text>', methods=['GET'], endpoint='translate_to_en')
def translate_to_en(text):
translator = Translator()
english_translation = translator.translate(text, src='auto', dest='en').text
return jsonify({'translated_txt': english_translation})
@app.route('/api/translate_tohi/<string:text>', methods=['GET'], endpoint='translate_to_hi')
def translate_to_hi(text):
translator = Translator()
hindi_translation = translator.translate(text, src='auto', dest='hi').text
return jsonify({'translated_txt': hindi_translation})
def add_newlines_every_n_words(input_string, n=10):
words = input_string.split()
output_string = ''
for i, word in enumerate(words):
if i > 0 and i % n == 0:
output_string += '\n'
output_string += word + ' '
return output_string.strip()
@app.route('/api/findtopic/<string:text>', methods=['POST'])
def topic_finder(text):
name = request.form['name']
file = request.files['file']
name = name.strip()
name = name.replace(" ", "_")
directory_path = os.path.join('uploads', name)
filesname, extension = os.path.splitext(file.filename)
filesname = filesname.strip()
filesname = filesname.replace(" ", "_")
text_path = os.path.join(directory_path, "{}.txt".format(filesname))
topic = pipeline("summarization", device=0)
f = topic(text, max_length=15, min_length=2, do_sample=False)
topic2 = f[0]['summary_text']
with open(text_path, 'a') as fil:
fil.write("\nTopic: {}\n".format(topic2))
return jsonify({'topic': "", 'topic2': topic2})
@app.route('/api/findSummary', methods=['POST'])
def summary_find():
text = request.form['text']
pipe = pipeline("summarization", model="google/pegasus-xsum")
temp = add_newlines_every_n_words(text, 30)
try:
summary = summarize(temp)
print("len: ", len(summary))
except:
print("Error in summarization using gensim")
summary = ""
if len(summary) == 0:
output = pipe(text)
summary = output[0]['summary_text']
translator = Translator()
arabic_summary = translator.translate(summary, src='en', dest='ar').text
turkish_summary = translator.translate(summary, src='en', dest='tr').text
name = request.form['name']
file = request.files['file']
name = name.strip()
name = name.replace(" ", "_")
directory_path = os.path.join('uploads', name)
filesname, extension = os.path.splitext(file.filename)
filesname = filesname.strip()
filesname = filesname.replace(" ", "_")
text_path = os.path.join(directory_path, "{}.txt".format(filesname))
# NEW PIPELINE FOR SUMMARIZATION
summarizer = pipeline("summarization")
f = summarizer(text, max_length=130, min_length=30, do_sample=False)
summary = f[0]['summary_text']
with open(text_path, 'a') as fil:
fil.write("\nSummary: {}\n".format(str(summary)))
return jsonify({'summary_en': str(summary), 'summary_ar': arabic_summary, 'summary_tr': turkish_summary})
@app.route('/api/sentiment/<string:text>', methods=['POST'])
def sentiment(text):
name = request.form['name']
file = request.files['file']
name = name.strip()
name = name.replace(" ", "_")
nltk.download('vader_lexicon')
analyzer = SentimentIntensityAnalyzer()
scores = analyzer.polarity_scores(text)
positive_percent = round(scores['pos'] * 100, 2)
negative_percent = round(scores['neg'] * 100, 2)
total = positive_percent + negative_percent
if (total == 0):
fin_pos = 50
fin_neg = 50
else:
fin_pos = round((positive_percent/total)*100, 2)
fin_neg = round((negative_percent/total)*100, 2)
directory_path = os.path.join('uploads', name)
filesname, extension = os.path.splitext(file.filename)
filesname = filesname.strip()
filesname = filesname.replace(" ", "_")
text_path = os.path.join(directory_path, "{}.txt".format(filesname))
with open(text_path, 'a') as fil:
fil.write("\nPositive Sentiment: {}% \nNegative Sentiment: {}%".format(fin_pos, fin_neg))
return jsonify({'positive': fin_pos, 'negative': fin_neg})
if __name__ == '__main__':
app.run(debug=False)