Update app.py
Browse files37 unmodified lines3839404142435 unmodified lines4950515253545534 unmodified lines909192939495969 unmodified lines10610710810911011111211311411511611711811912012112212312412512612712812911 unmodified lines14114214314414514612 unmodified lines15916016116216316437 unmodified linesurl], text=True).strip()command = ["yt-dlp","-x", "--audio-format", "mp3",5 unmodified linesif not os.path.exists(temp_filename):raise Exception("Audio file was not created.")return temp_filename, description, thumbnailexcept Exception as e:raise Exception(f"Failed to download video: {e}")34 unmodified linesthumbnail = Nonetry:temp_path, description, thumbnail = download_video_audio_and_metadata(audio_url)except Exception:if audio_url.endswith(".mp3"):response = requests.get(audio_url)9 unmodified linesresult = model.transcribe(temp_path)os.remove(temp_path)text_to_process = description if description else result["text"]prompt = f"""You are a professional cooking assistant AI. You are given a transcription or description of a recipe video.Your task is to extract and return ONLY valid JSON in the following format. The content must be written in the following language: **{language_code}**.If the original text does not mention exact values (duration, kcal, servings, etc.), you must estimate reasonable values based on the ingredients and preparation steps.Respond ONLY with valid JSON like this:{{"title": "Translated recipe title in {language_code}","video_description": "Full transcription or description from the video","description": "A short summary of the recipe (max 2 lines) in {language_code}","author": "Mentioned author or null","video_url": "{audio_url}","thumbnail_url": "{thumbnail if thumbnail else 'null'}","duration_minutes": 25,"kcal": 600,"difficulty": "easy", // one of: easy, medium, difficult11 unmodified lines"steps": ["Step 1 in {language_code}...","Step 2 in {language_code}..."]}}12 unmodified linesreturn JSONResponse(status_code=500, content={"error": "Invalid JSON from Gemini", "raw": structured_response})structured_json["video_transcription"] = result["text"]return structured_jsonexcept Exception as e:37 unmodified lines383940414243444546474849505 unmodified lines5657585960616234 unmodified lines9798991001011021039 unmodified lines11311411511611711811912012112212312412512612712812913013113213313413513613711 unmodified lines14915015115215315415515615715812 unmodified lines17117217317417517617717817918037 unmodified linesurl], text=True).strip()uploader = subprocess.check_output(["yt-dlp","--skip-download","--print", "%(uploader)s",url], text=True).strip()command = ["yt-dlp","-x", "--audio-format", "mp3",5 unmodified linesif not os.path.exists(temp_filename):raise Exception("Audio file was not created.")return temp_filename, description, thumbnail, uploaderexcept Exception as e:raise Exception(f"Failed to download video: {e}")34 unmodified linesthumbnail = Nonetry:temp_path, description, thumbnail, author = download_video_audio_and_metadata(audio_url)except Exception:if audio_url.endswith(".mp3"):response = requests.get(audio_url)9 unmodified linesresult = model.transcribe(temp_path)os.remove(temp_path)if description:text_to_process = f"Description:\n{description}\n\nTranscription:\n{result['text']}"else:text_to_process = result["text"]prompt = f"""You are a professional cooking assistant AI. You are given a transcription and a description of a recipe video if available.Your task is to extract and return ONLY valid JSON in the following format. The content must be written in the following language: **{language_code}**.If the original text does not mention exact values (duration, kcal, servings, etc.), you must estimate reasonable values based on the ingredients and preparation steps.You must also infer what kitchen equipment or tools are required to prepare the recipe. Do NOT include basic/common items like knives, spoons, plates, or glasses. Include only special equipment like oven, blender, air fryer, etc.Respond ONLY with valid JSON like this:{{"title": "Translated recipe title in {language_code}","description": "A short summary of the recipe (max 2 lines) in {language_code}","duration_minutes": 25,"kcal": 600,"difficulty": "easy", // one of: easy, medium, difficult11 unmodified lines"steps": ["Step 1 in {language_code}...","Step 2 in {language_code}..."],"equipment": [{{ "name": "oven", "icon": "🔥" }},{{ "name": "blender", "icon": "🌀" }}]}}12 unmodified linesreturn JSONResponse(status_code=500, content={"error": "Invalid JSON from Gemini", "raw": structured_response})structured_json["video_transcription"] = result["text"]structured_json["video_description"] = descriptionstructured_json["video_url"] = audio_urlstructured_json["thumbnail_url"] = thumbnailstructured_json["author"] = authorreturn structured_jsonexcept Exception as e:
app.py+24-8