Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,6 +41,58 @@ def health_check():
|
|
| 41 |
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def query_gemini_api(transcription):
|
| 45 |
"""
|
| 46 |
Send transcription text to Gemini API and fetch structured recipe information synchronously.
|
|
@@ -58,6 +110,7 @@ def query_gemini_api(transcription):
|
|
| 58 |
"7. Serving size: In count of people or portion size.\n"
|
| 59 |
"8. Special Notes or Variations: Include any specific tips, variations, or alternatives mentioned.\n"
|
| 60 |
"9. Festive or Thematic Relevance: Note if the recipe has any special relevance to holidays, events, or seasons.\n"
|
|
|
|
| 61 |
f"Text: {transcription}\n"
|
| 62 |
)
|
| 63 |
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
+
def download_audio(url, temp_video_path):
|
| 45 |
+
"""Download video (MP4 format) from the given URL and save it to temp_video_path."""
|
| 46 |
+
response = requests.get(url, stream=True)
|
| 47 |
+
if response.status_code == 200:
|
| 48 |
+
with open(temp_video_path, 'wb') as f:
|
| 49 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 50 |
+
f.write(chunk)
|
| 51 |
+
print(f"Audio downloaded successfully to {temp_video_path}")
|
| 52 |
+
else:
|
| 53 |
+
raise Exception(f"Failed to download audio, status code: {response.status_code}")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@app.route('/process-video', methods=['POST'])
|
| 58 |
+
def process_video():
|
| 59 |
+
if 'videoUrl' not in request.json:
|
| 60 |
+
return jsonify({"error": "No video URL provided"}), 400
|
| 61 |
+
|
| 62 |
+
video_url = request.json['videoUrl']
|
| 63 |
+
temp_video_path = None
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
# Step 1: Download the WAV file from the provided URL
|
| 67 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video_file:
|
| 68 |
+
temp_video_path = temp_video_file.name
|
| 69 |
+
download_audio(video_url, temp_video_path)
|
| 70 |
+
|
| 71 |
+
# Step 2: get the information from the downloaded MP4 file synchronously
|
| 72 |
+
video_info = get_information_from_video_using_OCR(temp_video_path)
|
| 73 |
+
|
| 74 |
+
if not video_info:
|
| 75 |
+
return jsonify({"error": "video information extraction failed"}), 500
|
| 76 |
+
|
| 77 |
+
# Step 3: Generate structured recipe information using Gemini API synchronously
|
| 78 |
+
structured_data = query_gemini_api(video_info)
|
| 79 |
+
|
| 80 |
+
return jsonify(structured_data)
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return jsonify({"error": str(e)}), 500
|
| 84 |
+
|
| 85 |
+
finally:
|
| 86 |
+
# Clean up temporary audio file
|
| 87 |
+
if temp_video_path and os.path.exists(temp_video_path):
|
| 88 |
+
os.remove(temp_video_path)
|
| 89 |
+
print(f"Temporary audio file deleted: {temp_video_path}")
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
def query_gemini_api(transcription):
|
| 97 |
"""
|
| 98 |
Send transcription text to Gemini API and fetch structured recipe information synchronously.
|
|
|
|
| 110 |
"7. Serving size: In count of people or portion size.\n"
|
| 111 |
"8. Special Notes or Variations: Include any specific tips, variations, or alternatives mentioned.\n"
|
| 112 |
"9. Festive or Thematic Relevance: Note if the recipe has any special relevance to holidays, events, or seasons.\n"
|
| 113 |
+
"Also, make sure not to provide anything else or any other information or warning or text apart from the above things mentioned."
|
| 114 |
f"Text: {transcription}\n"
|
| 115 |
)
|
| 116 |
|