GoodML commited on
Commit
e89bfb2
·
verified ·
1 Parent(s): 91844a7

Made changes for cloudinary links downlaod

Browse files
Files changed (1) hide show
  1. app.py +93 -28
app.py CHANGED
@@ -39,53 +39,118 @@ def health_check():
39
  return jsonify({"status": "success", "message": "API is running successfully!"}), 200
40
 
41
 
42
- @app.route('/process-audio', methods=['POST'])
43
- def process_audio():
44
- # print("GOT THE PROCESS AUDIO REQUEST, ANIKET")
45
 
46
- if 'audio' not in request.files:
47
- return jsonify({"error": "No audio file provided"}), 400
48
 
49
- audio_file = request.files['audio']
50
- # print("AUDIO FILE NAME: ", audio_file)
51
 
52
- temp_audio_path = None
53
- try:
54
- print("STARTING TRANSCRIPTION, ANIKET")
55
 
56
- # Step 1: Save the audio file temporarily to a specific location
57
- with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio_file:
58
- temp_audio_path = temp_audio_file.name # Get the file path
59
- temp_audio_file.write(audio_file.read()) # Write the uploaded audio to the temp file
60
 
61
- # print(f"Temporary audio file saved at: {temp_audio_path}")
62
 
63
- # Step 2: Transcribe the uploaded audio file synchronously
64
- transcription = transcribe_audio(temp_audio_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- # print("BEFORE THE transcription FAILED ERROR, CHECKING IF I GOT THE TRANSCRIPTION", transcription)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  if not transcription:
69
  return jsonify({"error": "Audio transcription failed"}), 500
70
 
71
- # print("GOT THE transcription")
72
-
73
- # Step 3: Generate structured recipe information using Gemini API synchronously
74
- # print("Starting the GEMINI REQUEST TO STRUCTURE IT")
75
  structured_data = query_gemini_api(transcription)
76
 
77
- # print("GOT THE STRUCTURED DATA", structured_data)
78
- # Step 4: Return the structured data
79
  return jsonify(structured_data)
80
 
81
  except Exception as e:
82
  return jsonify({"error": str(e)}), 500
83
 
84
  finally:
85
- # Clean up the temporary WAV file
86
- if temp_audio_path and os.path.exists(temp_audio_path):
87
- os.remove(temp_audio_path)
88
- print(f"Temporary WAV file deleted: {temp_audio_path}")
 
 
 
 
89
 
90
 
91
 
 
39
  return jsonify({"status": "success", "message": "API is running successfully!"}), 200
40
 
41
 
42
+ # @app.route('/process-audio', methods=['POST'])
43
+ # def process_audio():
44
+ # # print("GOT THE PROCESS AUDIO REQUEST, ANIKET")
45
 
46
+ # if 'audio' not in request.files:
47
+ # return jsonify({"error": "No audio file provided"}), 400
48
 
49
+ # audio_file = request.files['audio']
50
+ # # print("AUDIO FILE NAME: ", audio_file)
51
 
52
+ # temp_audio_path = None
53
+ # try:
54
+ # print("STARTING TRANSCRIPTION, ANIKET")
55
 
56
+ # # Step 1: Save the audio file temporarily to a specific location
57
+ # with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio_file:
58
+ # temp_audio_path = temp_audio_file.name # Get the file path
59
+ # temp_audio_file.write(audio_file.read()) # Write the uploaded audio to the temp file
60
 
61
+ # # print(f"Temporary audio file saved at: {temp_audio_path}")
62
 
63
+ # # Step 2: Transcribe the uploaded audio file synchronously
64
+ # transcription = transcribe_audio(temp_audio_path)
65
+
66
+ # # print("BEFORE THE transcription FAILED ERROR, CHECKING IF I GOT THE TRANSCRIPTION", transcription)
67
+
68
+ # if not transcription:
69
+ # return jsonify({"error": "Audio transcription failed"}), 500
70
+
71
+ # # print("GOT THE transcription")
72
+
73
+ # # Step 3: Generate structured recipe information using Gemini API synchronously
74
+ # # print("Starting the GEMINI REQUEST TO STRUCTURE IT")
75
+ # structured_data = query_gemini_api(transcription)
76
+
77
+ # # print("GOT THE STRUCTURED DATA", structured_data)
78
+ # # Step 4: Return the structured data
79
+ # return jsonify(structured_data)
80
+
81
+ # except Exception as e:
82
+ # return jsonify({"error": str(e)}), 500
83
+
84
+ # finally:
85
+ # # Clean up the temporary WAV file
86
+ # if temp_audio_path and os.path.exists(temp_audio_path):
87
+ # os.remove(temp_audio_path)
88
+ # print(f"Temporary WAV file deleted: {temp_audio_path}")
89
+
90
+ def download_video(url, temp_video_path):
91
+ """Download video from the given URL and save it to temp_video_path."""
92
+ response = requests.get(url, stream=True)
93
+ if response.status_code == 200:
94
+ with open(temp_video_path, 'wb') as f:
95
+ for chunk in response.iter_content(chunk_size=1024):
96
+ f.write(chunk)
97
+ print(f"Video downloaded successfully to {temp_video_path}")
98
+ else:
99
+ raise Exception(f"Failed to download video, status code: {response.status_code}")
100
+
101
+ def convert_to_wav(video_path, wav_path):
102
+ """Convert video file to WAV format using FFmpeg."""
103
+ try:
104
+ ffmpeg_command = [
105
+ "ffmpeg", "-i", video_path, "-q:a", "0", "-map", "a", wav_path
106
+ ]
107
+ subprocess.run(ffmpeg_command, check=True)
108
+ print(f"Video converted to WAV successfully: {wav_path}")
109
+ except subprocess.CalledProcessError as e:
110
+ raise Exception(f"FFmpeg conversion failed: {e}")
111
+
112
+ @app.route('/process-audio', methods=['POST'])
113
+ def process_audio():
114
+ if 'audioUrl' not in request.json:
115
+ return jsonify({"error": "No audio URL provided"}), 400
116
 
117
+ audio_url = request.json['audioUrl']
118
+ temp_video_path = None
119
+ temp_wav_path = None
120
+
121
+ try:
122
+ # Step 1: Download the video file from the Cloudinary URL
123
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video_file:
124
+ temp_video_path = temp_video_file.name
125
+ download_video(audio_url, temp_video_path)
126
+
127
+ # Step 2: Convert the downloaded video to WAV format
128
+ temp_wav_path = temp_video_path.replace(".mp4", ".wav")
129
+ convert_to_wav(temp_video_path, temp_wav_path)
130
+
131
+ # Step 3: Transcribe the audio file synchronously
132
+ transcription = transcribe_audio(temp_wav_path)
133
 
134
  if not transcription:
135
  return jsonify({"error": "Audio transcription failed"}), 500
136
 
137
+ # Step 4: Generate structured recipe information using Gemini API synchronously
 
 
 
138
  structured_data = query_gemini_api(transcription)
139
 
 
 
140
  return jsonify(structured_data)
141
 
142
  except Exception as e:
143
  return jsonify({"error": str(e)}), 500
144
 
145
  finally:
146
+ # Clean up temporary files
147
+ if temp_video_path and os.path.exists(temp_video_path):
148
+ os.remove(temp_video_path)
149
+ print(f"Temporary video file deleted: {temp_video_path}")
150
+
151
+ if temp_wav_path and os.path.exists(temp_wav_path):
152
+ os.remove(temp_wav_path)
153
+ print(f"Temporary WAV file deleted: {temp_wav_path}")
154
 
155
 
156