Update app.py
Browse files20 unmodified lines2122232425262723 unmodified lines5152535455565718 unmodified lines767778798081827 unmodified lines9091929394959697989910010110210310410510610710810911011147 unmodified lines15916016116220 unmodified lineslanguage_code: str = "en" # Default Englishdef download_audio_and_metadata(url: str) -> tuple[str, str, str]:temp_filename = f"/tmp/{uuid.uuid4()}.mp3"try:description = subprocess.check_output([23 unmodified linesreturn temp_filename, description, thumbnailexcept Exception as e:raise Exception(f"Failed to download TikTok video: {e}")def call_gemini(prompt: str, api_key: str) -> str:18 unmodified linesreturn cleaned@app.post("/analyze")async def analyze(request: AnalyzeRequest):audio_url = request.audio_url7 unmodified linesdescription = Nonethumbnail = Noneif "tiktok.com" in 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.namethumbnail = None # No thumbnail availableresult = model.transcribe(temp_path)os.remove(temp_path)text_to_process = description if description else result["text"]# PROMPT con idioma dinámicoprompt = f"""You are a professional cooking assistant AI. You are given a transcription or description of a recipe video.47 unmodified linesreturn structured_jsonexcept Exception as e:return JSONResponse(status_code=500, content={"error": str(e)})20 unmodified lines2122232425262723 unmodified lines5152535455565718 unmodified lines7677787980817 unmodified lines899091929394959697989910010110210310410510610710810911011111211347 unmodified lines16116216316420 unmodified lineslanguage_code: str = "en" # Default Englishdef download_video_audio_and_metadata(url: str) -> tuple[str, str, str]:temp_filename = f"/tmp/{uuid.uuid4()}.mp3"try:description = subprocess.check_output([23 unmodified linesreturn temp_filename, description, thumbnailexcept Exception as e:raise Exception(f"Failed to download video: {e}")def call_gemini(prompt: str, api_key: str) -> str:18 unmodified linesreturn cleaned@app.post("/analyze")async def analyze(request: AnalyzeRequest):audio_url = request.audio_url7 unmodified linesdescription = Nonethumbnail = Nonetry:temp_path, description, thumbnail = download_video_audio_and_metadata(audio_url)except Exception:if audio_url.endswith(".mp3"):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.namedescription = Nonethumbnail = Noneelse:raiseresult = 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.47 unmodified linesreturn structured_jsonexcept Exception as e:return JSONResponse(status_code=500, content={"error": str(e)})
app.py+16-14