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3 |
import whisper
|
| 4 |
import requests
|
| 5 |
import tempfile
|
| 6 |
import warnings
|
| 7 |
import threading
|
| 8 |
-
|
| 9 |
-
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
-
import requests
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
warnings.filterwarnings("ignore", category=UserWarning, module="whisper")
|
| 17 |
|
| 18 |
-
|
| 19 |
app = Flask(__name__)
|
| 20 |
|
| 21 |
-
|
| 22 |
# Gemini API settings
|
| 23 |
load_dotenv()
|
| 24 |
API_KEY = os.getenv("FIRST_API_KEY")
|
|
@@ -41,7 +189,7 @@ def health_check():
|
|
| 41 |
return jsonify({"status": "success", "message": "API is running successfully!"}), 200
|
| 42 |
|
| 43 |
|
| 44 |
-
def process_video_in_background(video_file, temp_video_file_name):
|
| 45 |
"""
|
| 46 |
This function is executed in a separate thread to handle the long-running
|
| 47 |
video processing tasks such as transcription and querying the Gemini API.
|
|
@@ -50,16 +198,16 @@ def process_video_in_background(video_file, temp_video_file_name):
|
|
| 50 |
transcription = transcribe_audio(temp_video_file_name)
|
| 51 |
|
| 52 |
if not transcription:
|
| 53 |
-
|
| 54 |
return
|
| 55 |
|
| 56 |
structured_data = query_gemini_api(transcription)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
-
|
| 63 |
|
| 64 |
finally:
|
| 65 |
# Clean up temporary files
|
|
@@ -73,6 +221,7 @@ def process_video():
|
|
| 73 |
return jsonify({"error": "No video file provided"}), 400
|
| 74 |
|
| 75 |
video_file = request.files['video']
|
|
|
|
| 76 |
|
| 77 |
try:
|
| 78 |
# Save video to a temporary file
|
|
@@ -81,9 +230,18 @@ def process_video():
|
|
| 81 |
print(f"Video file saved: {temp_video_file.name}")
|
| 82 |
|
| 83 |
# Start the video processing in a background thread
|
| 84 |
-
threading.Thread(target=process_video_in_background, args=(video_file, temp_video_file.name)).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
return jsonify({"error": str(e)}), 500
|
|
@@ -137,9 +295,21 @@ def query_gemini_api(transcription):
|
|
| 137 |
)
|
| 138 |
response.raise_for_status()
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
except requests.exceptions.RequestException as e:
|
| 145 |
print(f"Error querying Gemini API: {e}")
|
|
@@ -147,4 +317,4 @@ def query_gemini_api(transcription):
|
|
| 147 |
|
| 148 |
|
| 149 |
if __name__ == '__main__':
|
| 150 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import subprocess
|
| 3 |
+
# import whisper
|
| 4 |
+
# import requests
|
| 5 |
+
# import tempfile
|
| 6 |
+
# import warnings
|
| 7 |
+
# import threading
|
| 8 |
+
# from flask import Flask, request, jsonify, send_file, render_template
|
| 9 |
+
|
| 10 |
+
# from dotenv import load_dotenv
|
| 11 |
+
# import requests
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# warnings.filterwarnings("ignore", category=UserWarning, module="whisper")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# app = Flask(__name__)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# # Gemini API settings
|
| 23 |
+
# load_dotenv()
|
| 24 |
+
# API_KEY = os.getenv("FIRST_API_KEY")
|
| 25 |
+
|
| 26 |
+
# # Ensure the API key is loaded correctly
|
| 27 |
+
# if not API_KEY:
|
| 28 |
+
# raise ValueError("API Key not found. Make sure it is set in the .env file.")
|
| 29 |
+
|
| 30 |
+
# GEMINI_API_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
|
| 31 |
+
# GEMINI_API_KEY = API_KEY
|
| 32 |
+
|
| 33 |
+
# # Load Whisper AI model at startup
|
| 34 |
+
# print("Loading Whisper AI model...")
|
| 35 |
+
# whisper_model = whisper.load_model("base")
|
| 36 |
+
# print("Whisper AI model loaded successfully.")
|
| 37 |
+
|
| 38 |
+
# # Define the "/" endpoint for health check
|
| 39 |
+
# @app.route("/", methods=["GET"])
|
| 40 |
+
# def health_check():
|
| 41 |
+
# return jsonify({"status": "success", "message": "API is running successfully!"}), 200
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# def process_video_in_background(video_file, temp_video_file_name):
|
| 45 |
+
# """
|
| 46 |
+
# This function is executed in a separate thread to handle the long-running
|
| 47 |
+
# video processing tasks such as transcription and querying the Gemini API.
|
| 48 |
+
# """
|
| 49 |
+
# try:
|
| 50 |
+
# transcription = transcribe_audio(temp_video_file_name)
|
| 51 |
+
|
| 52 |
+
# if not transcription:
|
| 53 |
+
# print("Audio transcription failed")
|
| 54 |
+
# return
|
| 55 |
+
|
| 56 |
+
# structured_data = query_gemini_api(transcription)
|
| 57 |
+
|
| 58 |
+
# # Send structured data back or store it in a database, depending on your use case
|
| 59 |
+
# print("Processing complete. Structured data:", structured_data)
|
| 60 |
+
|
| 61 |
+
# except Exception as e:
|
| 62 |
+
# print(f"Error processing video: {e}")
|
| 63 |
+
|
| 64 |
+
# finally:
|
| 65 |
+
# # Clean up temporary files
|
| 66 |
+
# if os.path.exists(temp_video_file_name):
|
| 67 |
+
# os.remove(temp_video_file_name)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# @app.route('/process-video', methods=['POST'])
|
| 71 |
+
# def process_video():
|
| 72 |
+
# if 'video' not in request.files:
|
| 73 |
+
# return jsonify({"error": "No video file provided"}), 400
|
| 74 |
+
|
| 75 |
+
# video_file = request.files['video']
|
| 76 |
+
|
| 77 |
+
# try:
|
| 78 |
+
# # Save video to a temporary file
|
| 79 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video_file:
|
| 80 |
+
# video_file.save(temp_video_file.name)
|
| 81 |
+
# print(f"Video file saved: {temp_video_file.name}")
|
| 82 |
+
|
| 83 |
+
# # Start the video processing in a background thread
|
| 84 |
+
# threading.Thread(target=process_video_in_background, args=(video_file, temp_video_file.name)).start()
|
| 85 |
+
|
| 86 |
+
# return jsonify({"message": "Video is being processed in the background."}), 202
|
| 87 |
+
|
| 88 |
+
# except Exception as e:
|
| 89 |
+
# return jsonify({"error": str(e)}), 500
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# def transcribe_audio(video_path):
|
| 93 |
+
# """
|
| 94 |
+
# Transcribe audio directly from a video file using Whisper AI.
|
| 95 |
+
# """
|
| 96 |
+
# try:
|
| 97 |
+
# print(f"Transcribing video: {video_path}")
|
| 98 |
+
# result = whisper_model.transcribe(video_path)
|
| 99 |
+
# return result['text']
|
| 100 |
+
# except Exception as e:
|
| 101 |
+
# print(f"Error in transcription: {e}")
|
| 102 |
+
# return None
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# def query_gemini_api(transcription):
|
| 106 |
+
# """
|
| 107 |
+
# Send transcription text to Gemini API and fetch structured recipe information.
|
| 108 |
+
# """
|
| 109 |
+
# try:
|
| 110 |
+
# # Define the structured prompt
|
| 111 |
+
# prompt = (
|
| 112 |
+
# "Analyze the provided cooking video transcription and extract the following structured information:\n"
|
| 113 |
+
# "1. Recipe Name: Identify the name of the dish being prepared.\n"
|
| 114 |
+
# "2. Ingredients List: Extract a detailed list of ingredients with their respective quantities (if mentioned).\n"
|
| 115 |
+
# "3. Steps for Preparation: Provide a step-by-step breakdown of the recipe's preparation process, organized and numbered sequentially.\n"
|
| 116 |
+
# "4. Cooking Techniques Used: Highlight the cooking techniques demonstrated in the video, such as searing, blitzing, wrapping, etc.\n"
|
| 117 |
+
# "5. Equipment Needed: List all tools, appliances, or utensils mentioned, e.g., blender, hot pan, cling film, etc.\n"
|
| 118 |
+
# "6. Nutritional Information (if inferred): Provide an approximate calorie count or nutritional breakdown based on the ingredients used.\n"
|
| 119 |
+
# "7. Serving size: In count of people or portion size.\n"
|
| 120 |
+
# "8. Special Notes or Variations: Include any specific tips, variations, or alternatives mentioned.\n"
|
| 121 |
+
# "9. Festive or Thematic Relevance: Note if the recipe has any special relevance to holidays, events, or seasons.\n"
|
| 122 |
+
# f"Text: {transcription}\n"
|
| 123 |
+
# )
|
| 124 |
+
|
| 125 |
+
# payload = {
|
| 126 |
+
# "contents": [
|
| 127 |
+
# {"parts": [{"text": prompt}]}
|
| 128 |
+
# ]
|
| 129 |
+
# }
|
| 130 |
+
# headers = {"Content-Type": "application/json"}
|
| 131 |
+
|
| 132 |
+
# # Send request to Gemini API
|
| 133 |
+
# response = requests.post(
|
| 134 |
+
# f"{GEMINI_API_ENDPOINT}?key={GEMINI_API_KEY}",
|
| 135 |
+
# json=payload,
|
| 136 |
+
# headers=headers
|
| 137 |
+
# )
|
| 138 |
+
# response.raise_for_status()
|
| 139 |
+
|
| 140 |
+
# # Extract and return the structured data
|
| 141 |
+
# data = response.json()
|
| 142 |
+
# return data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "No result found")
|
| 143 |
+
|
| 144 |
+
# except requests.exceptions.RequestException as e:
|
| 145 |
+
# print(f"Error querying Gemini API: {e}")
|
| 146 |
+
# return {"error": str(e)}
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# if __name__ == '__main__':
|
| 150 |
+
# app.run(debug=True)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
# Above code is without polling and sleep
|
| 154 |
+
|
| 155 |
+
# Below is the latest code
|
| 156 |
import os
|
|
|
|
| 157 |
import whisper
|
| 158 |
import requests
|
| 159 |
import tempfile
|
| 160 |
import warnings
|
| 161 |
import threading
|
| 162 |
+
import time
|
| 163 |
+
from flask import Flask, request, jsonify
|
| 164 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
warnings.filterwarnings("ignore", category=UserWarning, module="whisper")
|
| 167 |
|
|
|
|
| 168 |
app = Flask(__name__)
|
| 169 |
|
|
|
|
| 170 |
# Gemini API settings
|
| 171 |
load_dotenv()
|
| 172 |
API_KEY = os.getenv("FIRST_API_KEY")
|
|
|
|
| 189 |
return jsonify({"status": "success", "message": "API is running successfully!"}), 200
|
| 190 |
|
| 191 |
|
| 192 |
+
def process_video_in_background(video_file, temp_video_file_name, result_container):
|
| 193 |
"""
|
| 194 |
This function is executed in a separate thread to handle the long-running
|
| 195 |
video processing tasks such as transcription and querying the Gemini API.
|
|
|
|
| 198 |
transcription = transcribe_audio(temp_video_file_name)
|
| 199 |
|
| 200 |
if not transcription:
|
| 201 |
+
result_container["error"] = "Audio transcription failed"
|
| 202 |
return
|
| 203 |
|
| 204 |
structured_data = query_gemini_api(transcription)
|
| 205 |
|
| 206 |
+
# Save structured data to the result container to return later
|
| 207 |
+
result_container["data"] = structured_data
|
| 208 |
|
| 209 |
except Exception as e:
|
| 210 |
+
result_container["error"] = f"Error processing video: {e}"
|
| 211 |
|
| 212 |
finally:
|
| 213 |
# Clean up temporary files
|
|
|
|
| 221 |
return jsonify({"error": "No video file provided"}), 400
|
| 222 |
|
| 223 |
video_file = request.files['video']
|
| 224 |
+
result_container = {}
|
| 225 |
|
| 226 |
try:
|
| 227 |
# Save video to a temporary file
|
|
|
|
| 230 |
print(f"Video file saved: {temp_video_file.name}")
|
| 231 |
|
| 232 |
# Start the video processing in a background thread
|
| 233 |
+
threading.Thread(target=process_video_in_background, args=(video_file, temp_video_file.name, result_container)).start()
|
| 234 |
+
|
| 235 |
+
# Poll every 5 seconds to check if the result is available
|
| 236 |
+
while "data" not in result_container and "error" not in result_container:
|
| 237 |
+
print("Waiting for processing to complete...")
|
| 238 |
+
time.sleep(5) # Sleep for 5 seconds before checking again
|
| 239 |
|
| 240 |
+
# Check for the result
|
| 241 |
+
if "error" in result_container:
|
| 242 |
+
return jsonify({"error": result_container["error"]}), 500
|
| 243 |
+
else:
|
| 244 |
+
return jsonify({"message": "Processing complete", "data": result_container["data"]}), 200
|
| 245 |
|
| 246 |
except Exception as e:
|
| 247 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 295 |
)
|
| 296 |
response.raise_for_status()
|
| 297 |
|
| 298 |
+
# Polling for response (in case Gemini takes time to process)
|
| 299 |
+
polling_wait_time = 5 # Time to wait between polling attempts
|
| 300 |
+
polling_max_retries = 60 # Maximum number of retries
|
| 301 |
+
|
| 302 |
+
for attempt in range(polling_max_retries):
|
| 303 |
+
print(f"Attempt {attempt + 1} to fetch Gemini API response...")
|
| 304 |
+
response_data = response.json()
|
| 305 |
+
|
| 306 |
+
# Check if the response is ready
|
| 307 |
+
if "candidates" in response_data and len(response_data["candidates"]) > 0:
|
| 308 |
+
return response_data["candidates"][0].get("content", {}).get("parts", [{}])[0].get("text", "No result found")
|
| 309 |
+
|
| 310 |
+
time.sleep(polling_wait_time) # Wait before trying again
|
| 311 |
+
|
| 312 |
+
return "Gemini API response not ready after multiple attempts."
|
| 313 |
|
| 314 |
except requests.exceptions.RequestException as e:
|
| 315 |
print(f"Error querying Gemini API: {e}")
|
|
|
|
| 317 |
|
| 318 |
|
| 319 |
if __name__ == '__main__':
|
| 320 |
+
app.run(debug=True)
|