developer28 commited on
Commit
e79671d
Β·
verified Β·
1 Parent(s): b5faa4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -1,10 +1,10 @@
1
- # βœ… Gemini-Based Stock Recommendation Extractor (No Audio, No Whisper)
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': True,
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
- return response.text if response else "⚠️ No response from Gemini."
 
 
 
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
- cookie_path = tempfile.mktemp(suffix=".txt")
87
- with open(cookie_path, "wb") as f:
88
- f.write(cookies.read())
 
 
 
 
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.