Update app.py
Browse files17 unmodified lines18192021222323242526273 unmodified lines313233343536373839404142435 unmodified lines495051445253545519 unmodified lines757677787980818283848586877788899091929394959697989988100101911021039310410595106107108981091101001011111121131141151161173 unmodified lines1211221231121241251261274 unmodified lines13213313412312413513612613713813914017 unmodified linesclass AnalyzeRequest(BaseModel): audio_url: str gemini_api_key: str language_code: str = "en" # Default English
def download_audio_from_tiktok(url: str) -> tuple[str, str]:def download_audio_and_metadata(url: str) -> tuple[str, str, str]: temp_filename = f"/tmp/{uuid.uuid4()}.mp3" try: description = subprocess.check_output([3 unmodified lines url ], text=True).strip()
thumbnail = subprocess.check_output([ "yt-dlp", "--skip-download", "--print", "%(thumbnail)s", url ], text=True).strip()
command = [ "yt-dlp", "-x", "--audio-format", "mp3",5 unmodified lines if not os.path.exists(temp_filename): raise Exception("Audio file was not created.")
return temp_filename, description return temp_filename, description, thumbnail except Exception as e: raise Exception(f"Failed to download TikTok video: {e}")
19 unmodified linesasync def analyze(request: AnalyzeRequest): audio_url = request.audio_url gemini_api_key = request.gemini_api_key language_code = request.language_code or "en"
if not audio_url or not gemini_api_key: return JSONResponse(status_code=400, content={"error": "Missing audio_url or gemini_api_key"})
try: description = None thumbnail = None
if "tiktok.com" in audio_url: temp_path, description = download_audio_from_tiktok(audio_url) temp_path, description, thumbnail = download_audio_and_metadata(audio_url) else: response = requests.get(audio_url) response.raise_for_status() with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f: f.write(response.content) temp_path = f.name thumbnail = None # No thumbnail available
result = model.transcribe(temp_path) os.remove(temp_path)
# Texto para procesar text_to_process = description if description else result["text"]
# Prompt completo # PROMPT con idioma dinámico prompt = f"""You are an expert cooking assistant AI. You will receive the text or transcript of a recipe from a video.You are an expert cooking assistant AI. You receive a recipe transcript or description.
Return ONLY valid JSON with the following structure:You must generate a JSON response in {language_code} with the following structure:
{{ "title": "Recipe title here", "title": "Translated recipe title", "video_description": "Full video transcription or description", "description": "A short summary description of the recipe", "author": "If known or mentioned, else null", "description": "Short summary of the recipe (max 2 lines)", "author": "If known, else null", "video_url": "{audio_url}", "thumbnail_url": "{thumbnail if thumbnail else 'null'}", "duration_minutes": 25, "kcal": 600, "difficulty": "easy",3 unmodified lines {{ "quantity": 1.5, "unit": "tsp", "name": "salt", "name": "translated name", "icon": "🧂" }} ]4 unmodified lines ]}}
If any field cannot be confidently extracted, set it to null.Infer values logically even if they are not explicitly written, based on the text.If any value cannot be confidently inferred, return it as null.
Here is the text to analyze:Text to analyze:
\"\"\"{text_to_process}\"\"\""""app.py+25-14