Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
-
# β
Gemini-Based Stock Recommendation Extractor (
|
2 |
-
# Uses video metadata (title + description) + Gemini Flash to extract stock info
|
3 |
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
import tempfile
|
7 |
import json
|
|
|
8 |
import google.generativeai as genai
|
9 |
from yt_dlp import YoutubeDL
|
10 |
|
@@ -25,16 +25,25 @@ def configure_gemini(api_key):
|
|
25 |
def extract_metadata(url, cookies_file=None):
|
26 |
try:
|
27 |
ydl_opts = {
|
28 |
-
'quiet':
|
29 |
'skip_download': True,
|
30 |
'noplaylist': True,
|
|
|
31 |
}
|
|
|
32 |
if cookies_file and os.path.exists(cookies_file):
|
|
|
33 |
ydl_opts['cookiefile'] = cookies_file
|
|
|
|
|
|
|
|
|
34 |
|
35 |
with YoutubeDL(ydl_opts) as ydl:
|
36 |
info = ydl.extract_info(url, download=False)
|
37 |
|
|
|
|
|
38 |
return {
|
39 |
'title': info.get("title", ""),
|
40 |
'description': info.get("description", ""),
|
@@ -45,6 +54,7 @@ def extract_metadata(url, cookies_file=None):
|
|
45 |
}, "β
Video metadata extracted"
|
46 |
|
47 |
except Exception as e:
|
|
|
48 |
return None, f"β Metadata extraction failed: {str(e)}"
|
49 |
|
50 |
# β
Gemini Prompt for Stock Extraction
|
@@ -69,13 +79,21 @@ def query_gemini_stock_analysis(meta):
|
|
69 |
|
70 |
try:
|
71 |
response = GEMINI_MODEL.generate_content(prompt)
|
72 |
-
|
|
|
|
|
|
|
73 |
except Exception as e:
|
|
|
74 |
return f"β Gemini query failed: {str(e)}"
|
75 |
|
76 |
# β
Main Pipeline
|
77 |
|
78 |
def run_pipeline(api_key, url, cookies):
|
|
|
|
|
|
|
|
|
79 |
status = configure_gemini(api_key)
|
80 |
if not status.startswith("β
"):
|
81 |
return status, ""
|
@@ -83,19 +101,26 @@ def run_pipeline(api_key, url, cookies):
|
|
83 |
# Save cookies if provided
|
84 |
cookie_path = None
|
85 |
if cookies:
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
89 |
|
90 |
metadata, meta_status = extract_metadata(url, cookie_path)
|
91 |
if not metadata:
|
92 |
return meta_status, ""
|
93 |
|
|
|
|
|
|
|
94 |
result = query_gemini_stock_analysis(metadata)
|
95 |
return meta_status, result
|
96 |
|
97 |
# β
Gradio UI
|
98 |
-
with gr.Blocks(title="Gemini Stock Extractor") as demo:
|
99 |
gr.Markdown("""
|
100 |
# π Gemini-Based Stock Recommendation Extractor
|
101 |
Paste a YouTube link and get stock-related insights using only the title + description.
|
|
|
1 |
+
# β
Gemini-Based Stock Recommendation Extractor (Debug-Enhanced)
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
import tempfile
|
6 |
import json
|
7 |
+
import traceback
|
8 |
import google.generativeai as genai
|
9 |
from yt_dlp import YoutubeDL
|
10 |
|
|
|
25 |
def extract_metadata(url, cookies_file=None):
|
26 |
try:
|
27 |
ydl_opts = {
|
28 |
+
'quiet': False,
|
29 |
'skip_download': True,
|
30 |
'noplaylist': True,
|
31 |
+
'extract_flat': False
|
32 |
}
|
33 |
+
|
34 |
if cookies_file and os.path.exists(cookies_file):
|
35 |
+
print(f"π Using cookies file: {cookies_file}")
|
36 |
ydl_opts['cookiefile'] = cookies_file
|
37 |
+
else:
|
38 |
+
print("β οΈ No cookies file provided or path invalid.")
|
39 |
+
|
40 |
+
print("π yt-dlp options:", ydl_opts)
|
41 |
|
42 |
with YoutubeDL(ydl_opts) as ydl:
|
43 |
info = ydl.extract_info(url, download=False)
|
44 |
|
45 |
+
print("β
Metadata fetched successfully")
|
46 |
+
|
47 |
return {
|
48 |
'title': info.get("title", ""),
|
49 |
'description': info.get("description", ""),
|
|
|
54 |
}, "β
Video metadata extracted"
|
55 |
|
56 |
except Exception as e:
|
57 |
+
traceback.print_exc()
|
58 |
return None, f"β Metadata extraction failed: {str(e)}"
|
59 |
|
60 |
# β
Gemini Prompt for Stock Extraction
|
|
|
79 |
|
80 |
try:
|
81 |
response = GEMINI_MODEL.generate_content(prompt)
|
82 |
+
if response and response.text:
|
83 |
+
return response.text.strip()
|
84 |
+
else:
|
85 |
+
return "β οΈ Gemini returned no content."
|
86 |
except Exception as e:
|
87 |
+
traceback.print_exc()
|
88 |
return f"β Gemini query failed: {str(e)}"
|
89 |
|
90 |
# β
Main Pipeline
|
91 |
|
92 |
def run_pipeline(api_key, url, cookies):
|
93 |
+
print("π Starting analysis...")
|
94 |
+
if not url or not api_key:
|
95 |
+
return "β Please provide both API key and YouTube URL", ""
|
96 |
+
|
97 |
status = configure_gemini(api_key)
|
98 |
if not status.startswith("β
"):
|
99 |
return status, ""
|
|
|
101 |
# Save cookies if provided
|
102 |
cookie_path = None
|
103 |
if cookies:
|
104 |
+
try:
|
105 |
+
cookie_path = tempfile.mktemp(suffix=".txt")
|
106 |
+
with open(cookie_path, "wb") as f:
|
107 |
+
f.write(cookies.read())
|
108 |
+
print(f"β
Saved uploaded cookies to: {cookie_path}")
|
109 |
+
except Exception as e:
|
110 |
+
print(f"β Failed to save cookies: {e}")
|
111 |
|
112 |
metadata, meta_status = extract_metadata(url, cookie_path)
|
113 |
if not metadata:
|
114 |
return meta_status, ""
|
115 |
|
116 |
+
print(f"π Title: {metadata['title']}")
|
117 |
+
print(f"π Description length: {len(metadata['description'])} characters")
|
118 |
+
|
119 |
result = query_gemini_stock_analysis(metadata)
|
120 |
return meta_status, result
|
121 |
|
122 |
# β
Gradio UI
|
123 |
+
with gr.Blocks(title="Gemini Stock Extractor (Debug Mode)") as demo:
|
124 |
gr.Markdown("""
|
125 |
# π Gemini-Based Stock Recommendation Extractor
|
126 |
Paste a YouTube link and get stock-related insights using only the title + description.
|