Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718191 unmodified line212223242526272829303132333435366 unmodified lines434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293import gradio as grimport subprocessimport osdef search_pinterest(query):if not query:return None, "β οΈ Please enter a search keyword."# Auto-add 'video' to help Pinterest search resultssearch_query = query if "video" in query.lower() else f"{query} video"print(f"--- Starting Scraper for: {search_query} ---")try:# Run the Node.js scraper# We set a 90-second timeout because Puppeteer can be slow on cloud serversresult = subprocess.run(["node", "scraper.js", search_query],capture_output=True,1 unmodified linetimeout=90)# Capture all output from Node.jsstdout_content = result.stdout.strip()stderr_content = result.stderr.strip()# Log to Hugging Face console for debuggingif stderr_content:print(f"Debug Logs:\n{stderr_content}")# Extract the URL from stdout# We split by lines and look for the one starting with 'http'lines = stdout_content.split('\n')video_url = Nonefor line in lines:6 unmodified linesprint(f"β Success! URL Found: {video_url}")return video_url, f"Found video for: {query}"else:print("β No URL found in Node output.")return None, f"Could not find a video. Output received: {stdout_content[:100]}..."except subprocess.TimeoutExpired:print("β Scraper timed out.")return None, "The search took too long. Please try again with a simpler keyword."except Exception as e:print(f"β System Error: {str(e)}")return None, f"System Error: {str(e)}"# Define the Gradio UI Theme and Layoutwith gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# π Pinterest Video Downloader")gr.Markdown("Enter a topic below. The system will search Pinterest and return the best quality video found.")with gr.Row():with gr.Column(scale=4):input_text = gr.Textbox(label="What are you looking for?",placeholder="e.g. Luxury Car, Funny Cat, Nature Aesthetic...",lines=1)with gr.Column(scale=1):submit_btn = gr.Button("π Search Video", variant="primary")with gr.Row():# The Video component handles the .mp4 URL directlyvideo_display = gr.Video(label="Result Video")status_msg = gr.Textbox(label="Status Log", interactive=False)# Link the button to the functionsubmit_btn.click(fn=search_pinterest,inputs=input_text,outputs=[video_display, status_msg])# Allow pressing "Enter" to searchinput_text.submit(fn=search_pinterest,inputs=input_text,outputs=[video_display, status_msg])# Launch the appif __name__ == "__main__":demo.launch()1234567891011121314151617181920212223242526272829303132331 unmodified line353637383940414243446 unmodified lines51525354555657585960616263646566676869707172737475767778798081import gradio as grimport subprocessimport osimport sys# --- NEW STARTUP BLOCK: Install Node Dependencies ---def install_node_deps():if not os.path.exists("node_modules"):print("--- Node.js dependencies not found. Installing... ---")try:# Run npm installsubprocess.run(["npm", "install"], check=True)print("--- Node.js dependencies installed successfully! ---")except Exception as e:print(f"--- Error installing Node dependencies: {e} ---")# Run the installationinstall_node_deps()# ----------------------------------------------------def search_pinterest(query):if not query:return None, "β οΈ Please enter a search keyword."search_query = query if "video" in query.lower() else f"{query} video"print(f"--- Starting Scraper for: {search_query} ---")try:# Run the Node.js scraperresult = subprocess.run(["node", "scraper.js", search_query],capture_output=True,1 unmodified linetimeout=90)stdout_content = result.stdout.strip()stderr_content = result.stderr.strip()# Look for the URL in stdoutlines = stdout_content.split('\n')video_url = Nonefor line in lines:6 unmodified linesprint(f"β Success! URL Found: {video_url}")return video_url, f"Found video for: {query}"else:# Print stderr to the console so you can see if Puppeteer crashedif stderr_content:print(f"Scraper Error Log:\n{stderr_content}")return None, f"Could not find a video. Error: {stderr_content[:200]}"except Exception as e:print(f"β System Error: {str(e)}")return None, f"System Error: {str(e)}"# Define the Gradio UIwith gr.Blocks() as demo:gr.Markdown("# π Pinterest Video Downloader")gr.Markdown("Searching Pinterest via Puppeteer (Node.js) on Hugging Face.")with gr.Row():input_text = gr.Textbox(label="Query", placeholder="e.g. Cat funny video")submit_btn = gr.Button("π Search Video", variant="primary")video_display = gr.Video(label="Result Video")status_msg = gr.Textbox(label="Status Log", interactive=False)submit_btn.click(fn=search_pinterest, inputs=input_text, outputs=[video_display, status_msg])input_text.submit(fn=search_pinterest, inputs=input_text, outputs=[video_display, status_msg])# Launch the appif __name__ == "__main__":# Note: theme moved to launch() for Gradio 6.0 compatibility as per your logsdemo.launch()
app.py+30-42