Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ try:
|
|
13 |
except:
|
14 |
WHISPER_AVAILABLE = False
|
15 |
|
16 |
-
#
|
17 |
def download_audio(url, cookies_path=None):
|
18 |
try:
|
19 |
temp_dir = tempfile.mkdtemp()
|
@@ -25,15 +25,9 @@ def download_audio(url, cookies_path=None):
|
|
25 |
'quiet': True,
|
26 |
'noplaylist': True,
|
27 |
'cookiefile': cookies_path if cookies_path else None,
|
28 |
-
'user_agent': 'Mozilla/5.0
|
29 |
'referer': 'https://www.youtube.com/',
|
30 |
'force_ipv4': True,
|
31 |
-
'http_headers': {
|
32 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
33 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
34 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
35 |
-
'Referer': 'https://www.youtube.com/'
|
36 |
-
},
|
37 |
}
|
38 |
|
39 |
with YoutubeDL(ydl_opts) as ydl:
|
@@ -50,7 +44,7 @@ def download_audio(url, cookies_path=None):
|
|
50 |
traceback.print_exc()
|
51 |
return None, f"β Download error: {str(e)}"
|
52 |
|
53 |
-
#
|
54 |
def transcribe_audio(path):
|
55 |
if not WHISPER_AVAILABLE:
|
56 |
return "β Whisper not available. Please install openai-whisper."
|
@@ -62,13 +56,13 @@ def transcribe_audio(path):
|
|
62 |
traceback.print_exc()
|
63 |
return f"β Transcription failed: {str(e)}"
|
64 |
|
65 |
-
#
|
66 |
def extract_stock_info(text):
|
67 |
try:
|
68 |
companies = re.findall(r'\b[A-Z][a-z]+(?: [A-Z][a-z]+)*\b', text)
|
69 |
symbols = re.findall(r'\b[A-Z]{2,5}\b', text)
|
70 |
prices = re.findall(r'\$\d+(?:\.\d{1,2})?', text)
|
71 |
-
actions = re.findall(r'\b(buy|sell|hold|target|bullish|bearish|stop loss)\b', text, re.IGNORECASE)
|
72 |
|
73 |
result = "=== STOCK RECOMMENDATION ANALYSIS ===\n\n"
|
74 |
if companies:
|
@@ -82,7 +76,7 @@ def extract_stock_info(text):
|
|
82 |
|
83 |
recommendations = []
|
84 |
for line in text.split("."):
|
85 |
-
if any(word in line.lower() for word in ['buy', 'sell', 'target', 'hold']):
|
86 |
recommendations.append(line.strip())
|
87 |
|
88 |
if recommendations:
|
@@ -98,7 +92,7 @@ def extract_stock_info(text):
|
|
98 |
except Exception as e:
|
99 |
return f"β Stock info extraction failed: {str(e)}"
|
100 |
|
101 |
-
#
|
102 |
def save_cookies(file):
|
103 |
if file is None:
|
104 |
return None
|
@@ -115,11 +109,11 @@ def save_cookies(file):
|
|
115 |
print(f"β Failed to handle cookies.txt: {e}")
|
116 |
return None
|
117 |
|
118 |
-
#
|
119 |
-
def run_pipeline(url, cookies_file):
|
120 |
try:
|
121 |
if not WHISPER_AVAILABLE:
|
122 |
-
return "β Whisper
|
123 |
if not url:
|
124 |
return "β YouTube URL required", ""
|
125 |
|
@@ -133,22 +127,24 @@ def run_pipeline(url, cookies_file):
|
|
133 |
return transcript, ""
|
134 |
|
135 |
stock_info = extract_stock_info(transcript)
|
136 |
-
|
|
|
|
|
|
|
137 |
|
138 |
except Exception as e:
|
139 |
tb = traceback.format_exc()
|
140 |
print(tb)
|
141 |
return f"β Unhandled Error:\n{tb}", ""
|
142 |
|
143 |
-
#
|
144 |
-
def run_pipeline_audio(audio_file):
|
145 |
try:
|
146 |
if not WHISPER_AVAILABLE:
|
147 |
-
return "β Whisper
|
148 |
if audio_file is None:
|
149 |
return "β No audio file uploaded", ""
|
150 |
|
151 |
-
# Handle both file-like and NamedString (path string)
|
152 |
temp_audio_path = tempfile.mktemp(suffix=os.path.splitext(str(audio_file))[-1])
|
153 |
if hasattr(audio_file, "read"):
|
154 |
with open(temp_audio_path, "wb") as f:
|
@@ -161,35 +157,40 @@ def run_pipeline_audio(audio_file):
|
|
161 |
return transcript, ""
|
162 |
|
163 |
stock_info = extract_stock_info(transcript)
|
164 |
-
|
|
|
|
|
|
|
165 |
|
166 |
except Exception as e:
|
167 |
tb = traceback.format_exc()
|
168 |
print(tb)
|
169 |
return f"β Unhandled Error:\n{tb}", ""
|
170 |
|
171 |
-
#
|
172 |
with gr.Blocks(title="Stock Insights from YouTube or Audio") as demo:
|
173 |
gr.Markdown("""
|
174 |
-
#
|
175 |
-
Upload a YouTube
|
176 |
""")
|
177 |
|
178 |
with gr.Tab("πΊ From YouTube Video"):
|
179 |
with gr.Row():
|
180 |
-
url_input = gr.Textbox(label="π₯ YouTube
|
181 |
cookie_input = gr.File(label="cookies.txt (optional)", file_types=[".txt"])
|
|
|
182 |
yt_run_btn = gr.Button("π Extract from YouTube")
|
183 |
yt_status = gr.Textbox(label="Status")
|
184 |
-
yt_result = gr.Textbox(label="Stock Info", lines=
|
185 |
-
yt_run_btn.click(fn=run_pipeline, inputs=[url_input, cookie_input], outputs=[yt_status, yt_result])
|
186 |
|
187 |
with gr.Tab("π΅ From Uploaded Audio"):
|
188 |
audio_input = gr.File(label="Upload Audio File", file_types=[".mp3", ".wav", ".m4a", ".webm"])
|
|
|
189 |
audio_run_btn = gr.Button("π Extract from Audio")
|
190 |
audio_status = gr.Textbox(label="Status")
|
191 |
-
audio_result = gr.Textbox(label="Stock Info", lines=
|
192 |
-
audio_run_btn.click(fn=run_pipeline_audio, inputs=[audio_input], outputs=[audio_status, audio_result])
|
193 |
|
194 |
if __name__ == "__main__":
|
195 |
demo.launch(debug=True)
|
|
|
13 |
except:
|
14 |
WHISPER_AVAILABLE = False
|
15 |
|
16 |
+
# Download audio from YouTube
|
17 |
def download_audio(url, cookies_path=None):
|
18 |
try:
|
19 |
temp_dir = tempfile.mkdtemp()
|
|
|
25 |
'quiet': True,
|
26 |
'noplaylist': True,
|
27 |
'cookiefile': cookies_path if cookies_path else None,
|
28 |
+
'user_agent': 'Mozilla/5.0',
|
29 |
'referer': 'https://www.youtube.com/',
|
30 |
'force_ipv4': True,
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
}
|
32 |
|
33 |
with YoutubeDL(ydl_opts) as ydl:
|
|
|
44 |
traceback.print_exc()
|
45 |
return None, f"β Download error: {str(e)}"
|
46 |
|
47 |
+
# Transcribe using Whisper
|
48 |
def transcribe_audio(path):
|
49 |
if not WHISPER_AVAILABLE:
|
50 |
return "β Whisper not available. Please install openai-whisper."
|
|
|
56 |
traceback.print_exc()
|
57 |
return f"β Transcription failed: {str(e)}"
|
58 |
|
59 |
+
# Extract stock insights
|
60 |
def extract_stock_info(text):
|
61 |
try:
|
62 |
companies = re.findall(r'\b[A-Z][a-z]+(?: [A-Z][a-z]+)*\b', text)
|
63 |
symbols = re.findall(r'\b[A-Z]{2,5}\b', text)
|
64 |
prices = re.findall(r'\$\d+(?:\.\d{1,2})?', text)
|
65 |
+
actions = re.findall(r'\b(buy|sell|hold|target|bullish|bearish|stop loss|accumulate|short|take profit|entry|exit)\b', text, re.IGNORECASE)
|
66 |
|
67 |
result = "=== STOCK RECOMMENDATION ANALYSIS ===\n\n"
|
68 |
if companies:
|
|
|
76 |
|
77 |
recommendations = []
|
78 |
for line in text.split("."):
|
79 |
+
if any(word in line.lower() for word in ['buy', 'sell', 'target', 'hold', 'accumulate', 'short', 'entry', 'exit']):
|
80 |
recommendations.append(line.strip())
|
81 |
|
82 |
if recommendations:
|
|
|
92 |
except Exception as e:
|
93 |
return f"β Stock info extraction failed: {str(e)}"
|
94 |
|
95 |
+
# Save cookies
|
96 |
def save_cookies(file):
|
97 |
if file is None:
|
98 |
return None
|
|
|
109 |
print(f"β Failed to handle cookies.txt: {e}")
|
110 |
return None
|
111 |
|
112 |
+
# YouTube flow
|
113 |
+
def run_pipeline(url, cookies_file, show_transcript):
|
114 |
try:
|
115 |
if not WHISPER_AVAILABLE:
|
116 |
+
return "β Whisper not installed", ""
|
117 |
if not url:
|
118 |
return "β YouTube URL required", ""
|
119 |
|
|
|
127 |
return transcript, ""
|
128 |
|
129 |
stock_info = extract_stock_info(transcript)
|
130 |
+
if show_transcript:
|
131 |
+
return "β
Complete", f"π Transcript:\n\n{transcript}\n\n\n{stock_info}"
|
132 |
+
else:
|
133 |
+
return "β
Complete", stock_info
|
134 |
|
135 |
except Exception as e:
|
136 |
tb = traceback.format_exc()
|
137 |
print(tb)
|
138 |
return f"β Unhandled Error:\n{tb}", ""
|
139 |
|
140 |
+
# Audio upload flow
|
141 |
+
def run_pipeline_audio(audio_file, show_transcript):
|
142 |
try:
|
143 |
if not WHISPER_AVAILABLE:
|
144 |
+
return "β Whisper not installed", ""
|
145 |
if audio_file is None:
|
146 |
return "β No audio file uploaded", ""
|
147 |
|
|
|
148 |
temp_audio_path = tempfile.mktemp(suffix=os.path.splitext(str(audio_file))[-1])
|
149 |
if hasattr(audio_file, "read"):
|
150 |
with open(temp_audio_path, "wb") as f:
|
|
|
157 |
return transcript, ""
|
158 |
|
159 |
stock_info = extract_stock_info(transcript)
|
160 |
+
if show_transcript:
|
161 |
+
return "β
Complete", f"π Transcript:\n\n{transcript}\n\n\n{stock_info}"
|
162 |
+
else:
|
163 |
+
return "β
Complete", stock_info
|
164 |
|
165 |
except Exception as e:
|
166 |
tb = traceback.format_exc()
|
167 |
print(tb)
|
168 |
return f"β Unhandled Error:\n{tb}", ""
|
169 |
|
170 |
+
# Gradio UI
|
171 |
with gr.Blocks(title="Stock Insights from YouTube or Audio") as demo:
|
172 |
gr.Markdown("""
|
173 |
+
# π Extract Stock Recommendations from YouTube or Uploaded Audio
|
174 |
+
Upload a YouTube video or audio file. We'll transcribe it using Whisper and extract stock insights.
|
175 |
""")
|
176 |
|
177 |
with gr.Tab("πΊ From YouTube Video"):
|
178 |
with gr.Row():
|
179 |
+
url_input = gr.Textbox(label="π₯ YouTube URL")
|
180 |
cookie_input = gr.File(label="cookies.txt (optional)", file_types=[".txt"])
|
181 |
+
show_transcript_yt = gr.Checkbox(label="Show Transcript", value=False)
|
182 |
yt_run_btn = gr.Button("π Extract from YouTube")
|
183 |
yt_status = gr.Textbox(label="Status")
|
184 |
+
yt_result = gr.Textbox(label="Transcript & Stock Info", lines=15)
|
185 |
+
yt_run_btn.click(fn=run_pipeline, inputs=[url_input, cookie_input, show_transcript_yt], outputs=[yt_status, yt_result])
|
186 |
|
187 |
with gr.Tab("π΅ From Uploaded Audio"):
|
188 |
audio_input = gr.File(label="Upload Audio File", file_types=[".mp3", ".wav", ".m4a", ".webm"])
|
189 |
+
show_transcript_audio = gr.Checkbox(label="Show Transcript", value=False)
|
190 |
audio_run_btn = gr.Button("π Extract from Audio")
|
191 |
audio_status = gr.Textbox(label="Status")
|
192 |
+
audio_result = gr.Textbox(label="Transcript & Stock Info", lines=15)
|
193 |
+
audio_run_btn.click(fn=run_pipeline_audio, inputs=[audio_input, show_transcript_audio], outputs=[audio_status, audio_result])
|
194 |
|
195 |
if __name__ == "__main__":
|
196 |
demo.launch(debug=True)
|