Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -227,319 +227,3 @@ if __name__ == '__main__':
|
|
227 |
|
228 |
|
229 |
|
230 |
-
|
231 |
-
|
232 |
-
# # Above code is without polling and sleep
|
233 |
-
# import os
|
234 |
-
# import whisper
|
235 |
-
# import requests
|
236 |
-
# from flask import Flask, request, jsonify, render_template
|
237 |
-
# import tempfile
|
238 |
-
# import warnings
|
239 |
-
# warnings.filterwarnings("ignore", message="FP16 is not supported on CPU; using FP32 instead")
|
240 |
-
|
241 |
-
# app = Flask(__name__)
|
242 |
-
# print("APP IS RUNNING, ANIKET")
|
243 |
-
|
244 |
-
# # Gemini API settings
|
245 |
-
# from dotenv import load_dotenv
|
246 |
-
# # Load the .env file
|
247 |
-
# load_dotenv()
|
248 |
-
|
249 |
-
# print("ENV LOADED, ANIKET")
|
250 |
-
|
251 |
-
# # Fetch the API key from the .env file
|
252 |
-
# API_KEY = os.getenv("FIRST_API_KEY")
|
253 |
-
|
254 |
-
# # Ensure the API key is loaded correctly
|
255 |
-
# if not API_KEY:
|
256 |
-
# raise ValueError("API Key not found. Make sure it is set in the .env file.")
|
257 |
-
|
258 |
-
# GEMINI_API_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
|
259 |
-
# GEMINI_API_KEY = API_KEY
|
260 |
-
|
261 |
-
|
262 |
-
# # Load Whisper AI model at startup
|
263 |
-
# print("Loading Whisper AI model..., ANIKET")
|
264 |
-
# whisper_model = whisper.load_model("base") # Choose model size: tiny, base, small, medium, large
|
265 |
-
# print("Whisper AI model loaded successfully, ANIKET")
|
266 |
-
|
267 |
-
|
268 |
-
# # Define the "/" endpoint for health check
|
269 |
-
# @app.route("/", methods=["GET"])
|
270 |
-
# def health_check():
|
271 |
-
# return jsonify({"status": "success", "message": "API is running successfully!"}), 200
|
272 |
-
|
273 |
-
# @app.route("/mbsa")
|
274 |
-
# def mbsa():
|
275 |
-
# return render_template("mbsa.html")
|
276 |
-
|
277 |
-
# @app.route('/process-audio', methods=['POST'])
|
278 |
-
# def process_audio():
|
279 |
-
# print("GOT THE PROCESS AUDIO REQUEST, ANIKET")
|
280 |
-
# """
|
281 |
-
# Flask endpoint to process audio:
|
282 |
-
# 1. Transcribe provided audio file using Whisper AI.
|
283 |
-
# 2. Send transcription to Gemini API for recipe information extraction.
|
284 |
-
# 3. Return structured data in the response.
|
285 |
-
# """
|
286 |
-
|
287 |
-
# if 'audio' not in request.files:
|
288 |
-
# return jsonify({"error": "No audio file provided"}), 400
|
289 |
-
|
290 |
-
# audio_file = request.files['audio']
|
291 |
-
# print("AUDIO FILE NAME: ", audio_file)
|
292 |
-
|
293 |
-
# try:
|
294 |
-
# print("STARTING TRANSCRIPTION, ANIKET")
|
295 |
-
# # Step 1: Transcribe the uploaded audio file directly
|
296 |
-
# audio_file = request.files['audio']
|
297 |
-
# transcription = transcribe_audio(audio_file)
|
298 |
-
|
299 |
-
# print("BEFORE THE transcription FAILED ERROR, CHECKING IF I GOT THE TRANSCRIPTION", transcription)
|
300 |
-
|
301 |
-
# if not transcription:
|
302 |
-
# return jsonify({"error": "Audio transcription failed"}), 500
|
303 |
-
|
304 |
-
# print("GOT THE transcription")
|
305 |
-
|
306 |
-
# print("Starting the GEMINI REQUEST TO STRUCTURE IT")
|
307 |
-
# # Step 2: Generate structured recipe information using Gemini API
|
308 |
-
# structured_data = query_gemini_api(transcription)
|
309 |
-
|
310 |
-
# print("GOT THE STRUCTURED DATA", structured_data)
|
311 |
-
# # Step 3: Return the structured data
|
312 |
-
# return jsonify(structured_data)
|
313 |
-
|
314 |
-
# except Exception as e:
|
315 |
-
# return jsonify({"error": str(e)}), 500
|
316 |
-
|
317 |
-
# def transcribe_audio(audio_path):
|
318 |
-
# """
|
319 |
-
# Transcribe audio using Whisper AI.
|
320 |
-
# """
|
321 |
-
# print("CAME IN THE transcribe audio function")
|
322 |
-
# try:
|
323 |
-
# # Transcribe audio using Whisper AI
|
324 |
-
# print("Transcribing audio...")
|
325 |
-
# result = whisper_model.transcribe(audio_path)
|
326 |
-
# print("THE RESULTS ARE", result)
|
327 |
-
|
328 |
-
# return result.get("text", "").strip()
|
329 |
-
|
330 |
-
# except Exception as e:
|
331 |
-
# print(f"Error in transcription: {e}")
|
332 |
-
# return None
|
333 |
-
|
334 |
-
|
335 |
-
# def query_gemini_api(transcription):
|
336 |
-
# """
|
337 |
-
# Send transcription text to Gemini API and fetch structured recipe information.
|
338 |
-
# """
|
339 |
-
# try:
|
340 |
-
# # Define the structured prompt
|
341 |
-
# prompt = (
|
342 |
-
# "Analyze the provided cooking video transcription and extract the following structured information:\n"
|
343 |
-
# "1. Recipe Name: Identify the name of the dish being prepared.\n"
|
344 |
-
# "2. Ingredients List: Extract a detailed list of ingredients with their respective quantities (if mentioned).\n"
|
345 |
-
# "3. Steps for Preparation: Provide a step-by-step breakdown of the recipe's preparation process, organized and numbered sequentially.\n"
|
346 |
-
# "4. Cooking Techniques Used: Highlight the cooking techniques demonstrated in the video, such as searing, blitzing, wrapping, etc.\n"
|
347 |
-
# "5. Equipment Needed: List all tools, appliances, or utensils mentioned, e.g., blender, hot pan, cling film, etc.\n"
|
348 |
-
# "6. Nutritional Information (if inferred): Provide an approximate calorie count or nutritional breakdown based on the ingredients used.\n"
|
349 |
-
# "7. Serving size: In count of people or portion size.\n"
|
350 |
-
# "8. Special Notes or Variations: Include any specific tips, variations, or alternatives mentioned.\n"
|
351 |
-
# "9. Festive or Thematic Relevance: Note if the recipe has any special relevance to holidays, events, or seasons.\n"
|
352 |
-
# f"Text: {transcription}\n"
|
353 |
-
# )
|
354 |
-
|
355 |
-
# # Prepare the payload and headers
|
356 |
-
# payload = {
|
357 |
-
# "contents": [
|
358 |
-
# {
|
359 |
-
# "parts": [
|
360 |
-
# {"text": prompt}
|
361 |
-
# ]
|
362 |
-
# }
|
363 |
-
# ]
|
364 |
-
# }
|
365 |
-
# headers = {"Content-Type": "application/json"}
|
366 |
-
|
367 |
-
# # Send request to Gemini API and wait for the response
|
368 |
-
# print("Querying Gemini API...")
|
369 |
-
# response = requests.post(
|
370 |
-
# f"{GEMINI_API_ENDPOINT}?key={GEMINI_API_KEY}",
|
371 |
-
# json=payload,
|
372 |
-
# headers=headers,
|
373 |
-
# timeout=60 # 60 seconds timeout for the request
|
374 |
-
# )
|
375 |
-
# response.raise_for_status()
|
376 |
-
|
377 |
-
# # Extract and return the structured data
|
378 |
-
# data = response.json()
|
379 |
-
# return data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "No result found")
|
380 |
-
|
381 |
-
# except requests.exceptions.RequestException as e:
|
382 |
-
# print(f"Error querying Gemini API: {e}")
|
383 |
-
# return {"error": str(e)}
|
384 |
-
|
385 |
-
|
386 |
-
# if __name__ == '__main__':
|
387 |
-
# app.run(debug=True)
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
# import os
|
396 |
-
# import subprocess
|
397 |
-
# import whisper
|
398 |
-
# import requests
|
399 |
-
# import tempfile
|
400 |
-
# import warnings
|
401 |
-
# import threading
|
402 |
-
# from flask import Flask, request, jsonify, send_file, render_template
|
403 |
-
|
404 |
-
# from dotenv import load_dotenv
|
405 |
-
# import requests
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
# warnings.filterwarnings("ignore", category=UserWarning, module="whisper")
|
411 |
-
|
412 |
-
|
413 |
-
# app = Flask(__name__)
|
414 |
-
|
415 |
-
|
416 |
-
# # Gemini API settings
|
417 |
-
# load_dotenv()
|
418 |
-
# API_KEY = os.getenv("FIRST_API_KEY")
|
419 |
-
|
420 |
-
# # Ensure the API key is loaded correctly
|
421 |
-
# if not API_KEY:
|
422 |
-
# raise ValueError("API Key not found. Make sure it is set in the .env file.")
|
423 |
-
|
424 |
-
# GEMINI_API_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
|
425 |
-
# GEMINI_API_KEY = API_KEY
|
426 |
-
|
427 |
-
# # Load Whisper AI model at startup
|
428 |
-
# print("Loading Whisper AI model...")
|
429 |
-
# whisper_model = whisper.load_model("base")
|
430 |
-
# print("Whisper AI model loaded successfully.")
|
431 |
-
|
432 |
-
# # Define the "/" endpoint for health check
|
433 |
-
# @app.route("/", methods=["GET"])
|
434 |
-
# def health_check():
|
435 |
-
# return jsonify({"status": "success", "message": "API is running successfully!"}), 200
|
436 |
-
|
437 |
-
|
438 |
-
# def process_video_in_background(video_file, temp_video_file_name):
|
439 |
-
# """
|
440 |
-
# This function is executed in a separate thread to handle the long-running
|
441 |
-
# video processing tasks such as transcription and querying the Gemini API.
|
442 |
-
# """
|
443 |
-
# try:
|
444 |
-
# transcription = transcribe_audio(temp_video_file_name)
|
445 |
-
|
446 |
-
# if not transcription:
|
447 |
-
# print("Audio transcription failed")
|
448 |
-
# return
|
449 |
-
|
450 |
-
# structured_data = query_gemini_api(transcription)
|
451 |
-
|
452 |
-
# # Send structured data back or store it in a database, depending on your use case
|
453 |
-
# print("Processing complete. Structured data:", structured_data)
|
454 |
-
|
455 |
-
# except Exception as e:
|
456 |
-
# print(f"Error processing video: {e}")
|
457 |
-
|
458 |
-
# finally:
|
459 |
-
# # Clean up temporary files
|
460 |
-
# if os.path.exists(temp_video_file_name):
|
461 |
-
# os.remove(temp_video_file_name)
|
462 |
-
|
463 |
-
|
464 |
-
# @app.route('/process-video', methods=['POST'])
|
465 |
-
# def process_video():
|
466 |
-
# if 'video' not in request.files:
|
467 |
-
# return jsonify({"error": "No video file provided"}), 400
|
468 |
-
|
469 |
-
# video_file = request.files['video']
|
470 |
-
|
471 |
-
# try:
|
472 |
-
# # Save video to a temporary file
|
473 |
-
# with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video_file:
|
474 |
-
# video_file.save(temp_video_file.name)
|
475 |
-
# print(f"Video file saved: {temp_video_file.name}")
|
476 |
-
|
477 |
-
# # Start the video processing in a background thread
|
478 |
-
# threading.Thread(target=process_video_in_background, args=(video_file, temp_video_file.name)).start()
|
479 |
-
|
480 |
-
# return jsonify({"message": "Video is being processed in the background."}), 202
|
481 |
-
|
482 |
-
# except Exception as e:
|
483 |
-
# return jsonify({"error": str(e)}), 500
|
484 |
-
|
485 |
-
|
486 |
-
# def transcribe_audio(video_path):
|
487 |
-
# """
|
488 |
-
# Transcribe audio directly from a video file using Whisper AI.
|
489 |
-
# """
|
490 |
-
# try:
|
491 |
-
# print(f"Transcribing video: {video_path}")
|
492 |
-
# result = whisper_model.transcribe(video_path)
|
493 |
-
# return result['text']
|
494 |
-
# except Exception as e:
|
495 |
-
# print(f"Error in transcription: {e}")
|
496 |
-
# return None
|
497 |
-
|
498 |
-
|
499 |
-
# def query_gemini_api(transcription):
|
500 |
-
# """
|
501 |
-
# Send transcription text to Gemini API and fetch structured recipe information.
|
502 |
-
# """
|
503 |
-
# try:
|
504 |
-
# # Define the structured prompt
|
505 |
-
# prompt = (
|
506 |
-
# "Analyze the provided cooking video transcription and extract the following structured information:\n"
|
507 |
-
# "1. Recipe Name: Identify the name of the dish being prepared.\n"
|
508 |
-
# "2. Ingredients List: Extract a detailed list of ingredients with their respective quantities (if mentioned).\n"
|
509 |
-
# "3. Steps for Preparation: Provide a step-by-step breakdown of the recipe's preparation process, organized and numbered sequentially.\n"
|
510 |
-
# "4. Cooking Techniques Used: Highlight the cooking techniques demonstrated in the video, such as searing, blitzing, wrapping, etc.\n"
|
511 |
-
# "5. Equipment Needed: List all tools, appliances, or utensils mentioned, e.g., blender, hot pan, cling film, etc.\n"
|
512 |
-
# "6. Nutritional Information (if inferred): Provide an approximate calorie count or nutritional breakdown based on the ingredients used.\n"
|
513 |
-
# "7. Serving size: In count of people or portion size.\n"
|
514 |
-
# "8. Special Notes or Variations: Include any specific tips, variations, or alternatives mentioned.\n"
|
515 |
-
# "9. Festive or Thematic Relevance: Note if the recipe has any special relevance to holidays, events, or seasons.\n"
|
516 |
-
# f"Text: {transcription}\n"
|
517 |
-
# )
|
518 |
-
|
519 |
-
# payload = {
|
520 |
-
# "contents": [
|
521 |
-
# {"parts": [{"text": prompt}]}
|
522 |
-
# ]
|
523 |
-
# }
|
524 |
-
# headers = {"Content-Type": "application/json"}
|
525 |
-
|
526 |
-
# # Send request to Gemini API
|
527 |
-
# response = requests.post(
|
528 |
-
# f"{GEMINI_API_ENDPOINT}?key={GEMINI_API_KEY}",
|
529 |
-
# json=payload,
|
530 |
-
# headers=headers
|
531 |
-
# )
|
532 |
-
# response.raise_for_status()
|
533 |
-
|
534 |
-
# # Extract and return the structured data
|
535 |
-
# data = response.json()
|
536 |
-
# return data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "No result found")
|
537 |
-
|
538 |
-
# except requests.exceptions.RequestException as e:
|
539 |
-
# print(f"Error querying Gemini API: {e}")
|
540 |
-
# return {"error": str(e)}
|
541 |
-
|
542 |
-
|
543 |
-
# if __name__ == '__main__':
|
544 |
-
# app.run(debug=True)
|
545 |
-
|
|
|
227 |
|
228 |
|
229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|