Update app.py
Browse files20 unmodified lines212223242425262723 unmodified lines515253545455565718 unmodified lines767778797980817 unmodified lines89909193949596979899100101929394959697989910010110210310410510610710810911010811111211347 unmodified lines16116216316216420 unmodified lines language_code: str = "en" # Default English
def download_audio_and_metadata(url: str) -> tuple[str, str, str]:def 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 lines
return temp_filename, description, thumbnail except Exception as e: raise Exception(f"Failed to download TikTok video: {e}") raise Exception(f"Failed to download video: {e}")
def call_gemini(prompt: str, api_key: str) -> str:18 unmodified lines return cleaned
@app.post("/analyze")async def analyze(request: AnalyzeRequest): audio_url = request.audio_url7 unmodified lines description = None thumbnail = None
if "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.name thumbnail = None # No thumbnail available try: 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.name description = None thumbnail = None else: raise
result = model.transcribe(temp_path) os.remove(temp_path)
text_to_process = description if description else result["text"]
# PROMPT con idioma dinámico prompt = f"""You are a professional cooking assistant AI. You are given a transcription or description of a recipe video.
47 unmodified lines return structured_json
except Exception as e: return JSONResponse(status_code=500, content={"error": str(e)}) return JSONResponse(status_code=500, content={"error": str(e)})app.py+16-14