nazib61 commited on
Commit
701b6ff
·
verified ·
1 Parent(s): b54296d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -32
app.py CHANGED
@@ -2,56 +2,92 @@ import gradio as gr
2
  import subprocess
3
  import os
4
 
5
- # Ensure npm dependencies are installed before starting (just in case)
6
- subprocess.run(["npm", "install"], check=True)
7
-
8
  def search_pinterest(query):
9
  if not query:
10
- return None, "Please enter a search term."
11
 
12
- if "video" not in query.lower():
13
- query += " video"
14
-
15
- print(f"Calling Node.js for: {query}")
16
 
17
  try:
18
- # Run the node script
19
- # We capture stdout (standard output) to get the URL
20
  result = subprocess.run(
21
- ["node", "scraper.js", query],
22
  capture_output=True,
23
  text=True,
24
- timeout=60 # 60 second timeout
25
  )
26
 
27
- output = result.stdout.strip()
28
- error = result.stderr.strip()
 
29
 
30
- print("Node Output:", output)
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- if "http" in output:
33
- return output, f"Found video for: {query}"
34
- elif "ERROR" in output:
35
- return None, output
36
  else:
37
- return None, "No video found or scraper failed. Try a different keyword."
 
38
 
 
 
 
39
  except Exception as e:
40
- return None, str(e)
 
41
 
42
- # Build the Interface
43
- with gr.Blocks() as demo:
44
- gr.Markdown("# 📌 Pinterest Video Finder")
45
- gr.Markdown("Type a keyword (e.g., 'Aesthetic Sunset') and the system will find a downloadable video.")
46
 
47
  with gr.Row():
48
- txt_input = gr.Textbox(label="Search Query", placeholder="cat funny video")
49
- btn_submit = gr.Button("Search Video")
 
 
 
 
 
 
50
 
51
- # Video Output
52
- video_output = gr.Video(label="Result Video")
53
- status_output = gr.Textbox(label="Status Log", interactive=False)
 
 
54
 
55
- btn_submit.click(search_pinterest, inputs=txt_input, outputs=[video_output, status_output])
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- demo.launch()
 
 
 
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,
20
  text=True,
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
+ # Log to Hugging Face console for debugging
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:
37
+ clean_line = line.strip()
38
+ if clean_line.startswith("http"):
39
+ video_url = clean_line
40
+ break
41
 
42
+ if video_url:
43
+ print(f" Success! URL Found: {video_url}")
44
+ return video_url, f"Found video for: {query}"
 
45
  else:
46
+ print("No URL found in Node output.")
47
+ return None, f"Could not find a video. Output received: {stdout_content[:100]}..."
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 Theme and Layout
57
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
+ gr.Markdown("# 📌 Pinterest Video Downloader")
59
+ gr.Markdown("Enter a topic below. The system will search Pinterest and return the best quality video found.")
60
 
61
  with gr.Row():
62
+ with gr.Column(scale=4):
63
+ input_text = gr.Textbox(
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
+ # Link the button to the function
78
+ submit_btn.click(
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()