Dhrumit1314 commited on
Commit
20742ab
1 Parent(s): e832bfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -53
app.py CHANGED
@@ -57,59 +57,53 @@ def hello2():
57
  # Route for uploading video files
58
  @app.route('/upload_video', methods=['POST'])
59
  def upload_video():
60
- try:
61
- start_time = time.time()
62
- if 'video' not in request.files:
63
- return jsonify({'error': 'No video file found in the request'})
64
-
65
- video = request.files['video']
66
- if video.filename == '':
67
- return jsonify({'error': 'No selected video file'})
68
-
69
- if video.mimetype.split('/')[0] != 'video':
70
- return jsonify({'error': 'The file uploaded is not a video'})
71
-
72
- model_name = request.form.get('modelName')
73
- print("MODEL:", model_name)
74
-
75
- # Save the uploaded video to a temporary directory
76
- video_path = os.path.join(os.getcwd(), 'temp', secure_filename(video.filename))
77
- video.save(video_path)
78
-
79
- # Transcribe audio from the uploaded video
80
- transcript = transcribe_audio(video_path)
81
-
82
- # Save the video file to Hugging Face Spaces
83
- video_file_id = hf_hub.upload(video_path, name=video.filename)
84
-
85
- # Save the transcribed audio to a temporary WAV file
86
- audio_path = os.path.join(os.getcwd(), 'temp', 'transcribed_audio.wav')
87
- with open(audio_path, 'w') as audio_file:
88
- audio_file.write(transcript)
89
-
90
- # Save the audio file to Hugging Face Spaces
91
- audio_file_id = hf_hub.upload(audio_path, name='transcribed_audio.wav')
92
-
93
- # Summarize the transcribed text using the selected model
94
- summary = ""
95
- if model_name == 'T5':
96
- summary = summarize_text_t5(transcript)
97
- elif model_name == 'BART':
98
- summary = summarize_text_bart(transcript)
99
- else:
100
- summary = summarizer(transcript)
101
-
102
- # Calculate the elapsed time and print a success message
103
- end_time = time.time()
104
- elapsed_time = end_time - start_time
105
- print(f"Video saved successfully. Time taken: {elapsed_time} seconds")
106
-
107
- # Return the transcript, summary, model name, video file ID, and audio file ID in the response
108
- return jsonify({'message': 'successful', 'transcript': transcript, 'summary': summary, 'modelName': model_name,
109
- 'videoFileId': video_file_id, 'audioFileId': audio_file_id})
110
- except Exception as e:
111
- # If an error occurs during processing, return an error message
112
- return jsonify({'error': str(e)})
113
 
114
  # def upload_video():
115
  # start_time = time.time()
 
57
  # Route for uploading video files
58
  @app.route('/upload_video', methods=['POST'])
59
  def upload_video():
60
+ start_time = time.time()
61
+ if 'video' not in request.files:
62
+ return jsonify({'error': 'No video file found in the request'})
63
+ video = request.files['video']
64
+ if video.mimetype.split('/')[0] != 'video':
65
+ return jsonify({'error': 'The file uploaded is not a video'})
66
+
67
+ model_name = request.form.get('modelName')
68
+ print("MODEL:", model_name)
69
+
70
+ video_path = os.path.join(os.getcwd(), secure_filename(video.filename))
71
+ video.save(video_path)
72
+
73
+ # Initialize HfApi and HfFolder
74
+ api = HfApi()
75
+ folder = HfFolder()
76
+
77
+ # Get your Hugging Face API token
78
+ token = folder.get_token()
79
+
80
+ # Specify your namespace (username or organization name)
81
+ namespace = "Dhrumit1314/notivai-backend"
82
+
83
+ # Upload the video file
84
+ video_file_id = api.upload_file(
85
+ token=token,
86
+ path=video_path,
87
+ filename=video.filename,
88
+ namespace=namespace
89
+ )
90
+
91
+ transcript = transcribe_audio(video_path)
92
+
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
+ end_time = time.time()
102
+ elapsed_time = end_time - start_time
103
+ print(f"Video saved successfully. Time taken: {elapsed_time} seconds")
104
+
105
+ return jsonify({'message': 'successful', 'transcript': transcript, 'summary': summary, 'modelName': model_name, 'videoFileId': video_file_id})
106
+
 
 
 
 
 
 
107
 
108
  # def upload_video():
109
  # start_time = time.time()