Dhrumit1314 commited on
Commit
90de860
1 Parent(s): a61f9ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -21
app.py CHANGED
@@ -20,6 +20,7 @@ from wordcloud import WordCloud
20
  from heapq import nlargest
21
  from werkzeug.utils import secure_filename
22
  from spacy.lang.en.stop_words import STOP_WORDS
 
23
 
24
  # Change the directory to the backend folder
25
  # os.chdir("E:/Centennial/SEMESTER 6/Software Development Project/backend/")
@@ -58,34 +59,89 @@ def upload_video():
58
  start_time = time.time()
59
  if 'video' not in request.files:
60
  return jsonify({'error': 'No video file found in the request'})
 
61
  video = request.files['video']
 
 
 
62
  if video.mimetype.split('/')[0] != 'video':
63
  return jsonify({'error': 'The file uploaded is not a video'})
64
 
65
  model_name = request.form.get('modelName')
66
  print("MODEL:", model_name)
67
 
68
- # backend_folder = 'backend_videos'
69
- # if not os.path.exists(backend_folder):
70
- # os.makedirs(backend_folder)
71
- video_path = os.path.join(os.getcwd(), secure_filename(video.filename))
72
- video.save(video_path)
73
-
74
- transcript = transcribe_audio(video_path)
75
-
76
- summary = ""
77
- if model_name == 'T5':
78
- summary = summarize_text_t5(transcript)
79
- elif model_name == 'BART':
80
- summary = summarize_text_bart(transcript)
81
- else:
82
- summary = summarizer(transcript)
83
-
84
- end_time = time.time()
85
- elapsed_time = end_time - start_time
86
- print(f"Video saved successfully. Time taken: {elapsed_time} seconds")
87
-
88
- return jsonify({'message': 'successful', 'transcript': transcript, 'summary': summary, 'modelName': model_name})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Route for uploading YouTube video links
91
  @app.route('/youtube_upload_video', methods=['POST'])
 
20
  from heapq import nlargest
21
  from werkzeug.utils import secure_filename
22
  from spacy.lang.en.stop_words import STOP_WORDS
23
+ import huggingface_hub as hf_hub
24
 
25
  # Change the directory to the backend folder
26
  # os.chdir("E:/Centennial/SEMESTER 6/Software Development Project/backend/")
 
59
  start_time = time.time()
60
  if 'video' not in request.files:
61
  return jsonify({'error': 'No video file found in the request'})
62
+
63
  video = request.files['video']
64
+ if video.filename == '':
65
+ return jsonify({'error': 'No selected video file'})
66
+
67
  if video.mimetype.split('/')[0] != 'video':
68
  return jsonify({'error': 'The file uploaded is not a video'})
69
 
70
  model_name = request.form.get('modelName')
71
  print("MODEL:", model_name)
72
 
73
+ try:
74
+ # Save the uploaded video to a temporary directory
75
+ video_path = os.path.join(os.getcwd(), 'temp', secure_filename(video.filename))
76
+ video.save(video_path)
77
+
78
+ # Save the video file to Hugging Face Spaces
79
+ video_file_id = hf_hub.upload(video_path, name=video.filename)
80
+
81
+ # Transcribe audio from the uploaded video
82
+ transcript = transcribe_audio(video_path)
83
+
84
+ # Save the transcribed audio to a temporary WAV file
85
+ audio_path = os.path.join(os.getcwd(), 'temp', 'transcribed_audio.wav')
86
+ with open(audio_path, 'w') as audio_file:
87
+ audio_file.write(transcript)
88
+
89
+ # Save the audio file to Hugging Face Spaces
90
+ audio_file_id = hf_hub.upload(audio_path, name='transcribed_audio.wav')
91
+
92
+ # Summarize the transcribed text using the selected model
93
+ summary = ""
94
+ if model_name == 'T5':
95
+ summary = summarize_text_t5(transcript)
96
+ elif model_name == 'BART':
97
+ summary = summarize_text_bart(transcript)
98
+ else:
99
+ summary = summarizer(transcript)
100
+
101
+ # Calculate the elapsed time and print a success message
102
+ end_time = time.time()
103
+ elapsed_time = end_time - start_time
104
+ print(f"Video saved successfully. Time taken: {elapsed_time} seconds")
105
+
106
+ # Return the transcript, summary, model name, video file ID, and audio file ID in the response
107
+ return jsonify({'message': 'successful', 'transcript': transcript, 'summary': summary, 'modelName': model_name,
108
+ 'videoFileId': video_file_id, 'audioFileId': audio_file_id})
109
+ except Exception as e:
110
+ # If an error occurs during processing, return an error message
111
+ return jsonify({'error': str(e)})
112
+
113
+ # def upload_video():
114
+ # start_time = time.time()
115
+ # if 'video' not in request.files:
116
+ # return jsonify({'error': 'No video file found in the request'})
117
+ # video = request.files['video']
118
+ # if video.mimetype.split('/')[0] != 'video':
119
+ # return jsonify({'error': 'The file uploaded is not a video'})
120
+
121
+ # model_name = request.form.get('modelName')
122
+ # print("MODEL:", model_name)
123
+
124
+ # # backend_folder = 'backend_videos'
125
+ # # if not os.path.exists(backend_folder):
126
+ # # os.makedirs(backend_folder)
127
+ # video_path = os.path.join(os.getcwd(), secure_filename(video.filename))
128
+ # video.save(video_path)
129
+
130
+ # transcript = transcribe_audio(video_path)
131
+
132
+ # summary = ""
133
+ # if model_name == 'T5':
134
+ # summary = summarize_text_t5(transcript)
135
+ # elif model_name == 'BART':
136
+ # summary = summarize_text_bart(transcript)
137
+ # else:
138
+ # summary = summarizer(transcript)
139
+
140
+ # end_time = time.time()
141
+ # elapsed_time = end_time - start_time
142
+ # print(f"Video saved successfully. Time taken: {elapsed_time} seconds")
143
+
144
+ # return jsonify({'message': 'successful', 'transcript': transcript, 'summary': summary, 'modelName': model_name})
145
 
146
  # Route for uploading YouTube video links
147
  @app.route('/youtube_upload_video', methods=['POST'])