Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def search_pinterest(query):
|
| 6 |
if not query:
|
| 7 |
return None, "β οΈ Please enter a search keyword."
|
| 8 |
|
| 9 |
-
# Auto-add 'video' to help Pinterest search results
|
| 10 |
search_query = query if "video" in query.lower() else f"{query} video"
|
| 11 |
|
| 12 |
print(f"--- Starting Scraper for: {search_query} ---")
|
| 13 |
|
| 14 |
try:
|
| 15 |
# Run the Node.js scraper
|
| 16 |
-
# We set a 90-second timeout because Puppeteer can be slow on cloud servers
|
| 17 |
result = subprocess.run(
|
| 18 |
["node", "scraper.js", search_query],
|
| 19 |
capture_output=True,
|
|
@@ -21,16 +35,10 @@ def search_pinterest(query):
|
|
| 21 |
timeout=90
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# Capture all output from Node.js
|
| 25 |
stdout_content = result.stdout.strip()
|
| 26 |
stderr_content = result.stderr.strip()
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
if stderr_content:
|
| 30 |
-
print(f"Debug Logs:\n{stderr_content}")
|
| 31 |
-
|
| 32 |
-
# Extract the URL from stdout
|
| 33 |
-
# We split by lines and look for the one starting with 'http'
|
| 34 |
lines = stdout_content.split('\n')
|
| 35 |
video_url = None
|
| 36 |
for line in lines:
|
|
@@ -43,51 +51,31 @@ def search_pinterest(query):
|
|
| 43 |
print(f"β
Success! URL Found: {video_url}")
|
| 44 |
return video_url, f"Found video for: {query}"
|
| 45 |
else:
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
except subprocess.TimeoutExpired:
|
| 50 |
-
print("β Scraper timed out.")
|
| 51 |
-
return None, "The search took too long. Please try again with a simpler keyword."
|
| 52 |
except Exception as e:
|
| 53 |
print(f"β System Error: {str(e)}")
|
| 54 |
return None, f"System Error: {str(e)}"
|
| 55 |
|
| 56 |
-
# Define the Gradio UI
|
| 57 |
-
with gr.Blocks(
|
| 58 |
gr.Markdown("# π Pinterest Video Downloader")
|
| 59 |
-
gr.Markdown("
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
label="What are you looking for?",
|
| 65 |
-
placeholder="e.g. Luxury Car, Funny Cat, Nature Aesthetic...",
|
| 66 |
-
lines=1
|
| 67 |
-
)
|
| 68 |
-
with gr.Column(scale=1):
|
| 69 |
-
submit_btn = gr.Button("π Search Video", variant="primary")
|
| 70 |
-
|
| 71 |
-
with gr.Row():
|
| 72 |
-
# The Video component handles the .mp4 URL directly
|
| 73 |
-
video_display = gr.Video(label="Result Video")
|
| 74 |
|
|
|
|
| 75 |
status_msg = gr.Textbox(label="Status Log", interactive=False)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
fn=search_pinterest,
|
| 80 |
-
inputs=input_text,
|
| 81 |
-
outputs=[video_display, status_msg]
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
# Allow pressing "Enter" to search
|
| 85 |
-
input_text.submit(
|
| 86 |
-
fn=search_pinterest,
|
| 87 |
-
inputs=input_text,
|
| 88 |
-
outputs=[video_display, status_msg]
|
| 89 |
-
)
|
| 90 |
|
| 91 |
# Launch the app
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 93 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# --- NEW STARTUP BLOCK: Install Node Dependencies ---
|
| 7 |
+
def install_node_deps():
|
| 8 |
+
if not os.path.exists("node_modules"):
|
| 9 |
+
print("--- Node.js dependencies not found. Installing... ---")
|
| 10 |
+
try:
|
| 11 |
+
# Run npm install
|
| 12 |
+
subprocess.run(["npm", "install"], check=True)
|
| 13 |
+
print("--- Node.js dependencies installed successfully! ---")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"--- Error installing Node dependencies: {e} ---")
|
| 16 |
+
|
| 17 |
+
# Run the installation
|
| 18 |
+
install_node_deps()
|
| 19 |
+
# ----------------------------------------------------
|
| 20 |
|
| 21 |
def search_pinterest(query):
|
| 22 |
if not query:
|
| 23 |
return None, "β οΈ Please enter a search keyword."
|
| 24 |
|
|
|
|
| 25 |
search_query = query if "video" in query.lower() else f"{query} video"
|
| 26 |
|
| 27 |
print(f"--- Starting Scraper for: {search_query} ---")
|
| 28 |
|
| 29 |
try:
|
| 30 |
# Run the Node.js scraper
|
|
|
|
| 31 |
result = subprocess.run(
|
| 32 |
["node", "scraper.js", search_query],
|
| 33 |
capture_output=True,
|
|
|
|
| 35 |
timeout=90
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
stdout_content = result.stdout.strip()
|
| 39 |
stderr_content = result.stderr.strip()
|
| 40 |
|
| 41 |
+
# Look for the URL in stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
lines = stdout_content.split('\n')
|
| 43 |
video_url = None
|
| 44 |
for line in lines:
|
|
|
|
| 51 |
print(f"β
Success! URL Found: {video_url}")
|
| 52 |
return video_url, f"Found video for: {query}"
|
| 53 |
else:
|
| 54 |
+
# Print stderr to the console so you can see if Puppeteer crashed
|
| 55 |
+
if stderr_content:
|
| 56 |
+
print(f"Scraper Error Log:\n{stderr_content}")
|
| 57 |
+
return None, f"Could not find a video. Error: {stderr_content[:200]}"
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
print(f"β System Error: {str(e)}")
|
| 61 |
return None, f"System Error: {str(e)}"
|
| 62 |
|
| 63 |
+
# Define the Gradio UI
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown("# π Pinterest Video Downloader")
|
| 66 |
+
gr.Markdown("Searching Pinterest via Puppeteer (Node.js) on Hugging Face.")
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
+
input_text = gr.Textbox(label="Query", placeholder="e.g. Cat funny video")
|
| 70 |
+
submit_btn = gr.Button("π Search Video", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
video_display = gr.Video(label="Result Video")
|
| 73 |
status_msg = gr.Textbox(label="Status Log", interactive=False)
|
| 74 |
|
| 75 |
+
submit_btn.click(fn=search_pinterest, inputs=input_text, outputs=[video_display, status_msg])
|
| 76 |
+
input_text.submit(fn=search_pinterest, inputs=input_text, outputs=[video_display, status_msg])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# Launch the app
|
| 79 |
if __name__ == "__main__":
|
| 80 |
+
# Note: theme moved to launch() for Gradio 6.0 compatibility as per your logs
|
| 81 |
demo.launch()
|