Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,10 @@ import gradio as gr
|
|
2 |
from gradio_client import Client
|
3 |
import json
|
4 |
import logging
|
5 |
-
import ast
|
6 |
import openai
|
7 |
import os
|
8 |
-
import random
|
9 |
-
import re
|
10 |
|
|
|
11 |
logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG,
|
12 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
13 |
|
@@ -23,59 +21,53 @@ def parse_api_response(response):
|
|
23 |
raise ValueError(f"μμμΉ λͺ»ν μλ΅ νμμ
λλ€. λ°μ λ°μ΄ν° νμ
: {type(response)}")
|
24 |
return response
|
25 |
except Exception as e:
|
|
|
26 |
raise ValueError(f"API μλ΅ νμ± μ€ν¨: {str(e)}")
|
27 |
|
28 |
-
def split_sentences(text):
|
29 |
-
sentences = re.split(r"(λλ€|μμ|ꡬλ|ν΄μ|κ΅°μ|κ² μ΄μ|μμ€|ν΄λΌ|μμ|μμ|λ°μ|λμ|μΈμ|μ΄μ|κ²μ|ꡬμ|κ³ μ|λμ|νμ£ )(?![\w])", text)
|
30 |
-
combined_sentences = []
|
31 |
-
current_sentence = ""
|
32 |
-
for i in range(0, len(sentences), 2):
|
33 |
-
if i + 1 < len(sentences):
|
34 |
-
sentence = sentences[i] + sentences[i + 1]
|
35 |
-
else:
|
36 |
-
sentence = sentences[i]
|
37 |
-
if len(current_sentence) + len(sentence) > 100:
|
38 |
-
combined_sentences.append(current_sentence.strip())
|
39 |
-
current_sentence = sentence.strip()
|
40 |
-
else:
|
41 |
-
current_sentence += sentence
|
42 |
-
if sentence.endswith(('.', '?', '!')):
|
43 |
-
combined_sentences.append(current_sentence.strip())
|
44 |
-
current_sentence = ""
|
45 |
-
if current_sentence:
|
46 |
-
combined_sentences.append(current_sentence.strip())
|
47 |
-
return combined_sentences
|
48 |
-
|
49 |
def get_youtube_script(url):
|
50 |
logging.info(f"μ€ν¬λ¦½νΈ μΆμΆ μμ: URL = {url}")
|
51 |
-
|
52 |
client = Client("whispersound/YT_Ts_R")
|
53 |
-
|
54 |
try:
|
55 |
-
logging.debug("API νΈμΆ μμ")
|
56 |
result = client.predict(youtube_url=url, api_name="/predict")
|
57 |
-
logging.debug("API νΈμΆ μλ£")
|
58 |
-
|
59 |
parsed_result = parse_api_response(result)
|
60 |
|
61 |
if 'data' not in parsed_result or not parsed_result['data']:
|
62 |
raise ValueError("API μλ΅μ μ ν¨ν λ°μ΄ν°κ° μμ΅λλ€.")
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
67 |
|
68 |
if not transcription_text:
|
69 |
raise ValueError("μΆμΆλ μ€ν¬λ¦½νΈκ° μμ΅λλ€.")
|
70 |
|
71 |
logging.info("μ€ν¬λ¦½νΈ μΆμΆ μλ£")
|
72 |
return title, transcription_text, sections
|
73 |
-
|
74 |
except Exception as e:
|
75 |
-
|
76 |
-
logging.exception(error_msg)
|
77 |
raise
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def call_api(prompt, max_tokens, temperature, top_p):
|
80 |
try:
|
81 |
response = openai.ChatCompletion.create(
|
@@ -90,67 +82,6 @@ def call_api(prompt, max_tokens, temperature, top_p):
|
|
90 |
logging.exception("LLM API νΈμΆ μ€ μ€λ₯ λ°μ")
|
91 |
raise
|
92 |
|
93 |
-
def summarize_section(section_text):
|
94 |
-
prompt = f"""
|
95 |
-
λ€μ μ νλΈ λλ³Έ μΉμ
μ ν΅μ¬ λ΄μ©μ κ°κ²°νκ² μμ½νμΈμ:
|
96 |
-
1. νκΈλ‘ μμ±νμΈμ.
|
97 |
-
2. μ£Όμ λ
Όμ κ³Ό μ€μν μΈλΆμ¬νμ ν¬ν¨νμΈμ.
|
98 |
-
3. μμ½μ 2-3λ¬Έμ₯μΌλ‘ μ ννμΈμ.
|
99 |
-
|
100 |
-
μΉμ
λ΄μ©:
|
101 |
-
{section_text}
|
102 |
-
"""
|
103 |
-
return call_api(prompt, max_tokens=150, temperature=0.3, top_p=0.9)
|
104 |
-
|
105 |
-
def format_time(seconds):
|
106 |
-
minutes, seconds = divmod(seconds, 60)
|
107 |
-
hours, minutes = divmod(minutes, 60)
|
108 |
-
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
|
109 |
-
|
110 |
-
def generate_timeline_summary(sections):
|
111 |
-
combined_sections = "\n\n".join([f"{format_time(section['start_time'])}: {section['text']}" for section in sections])
|
112 |
-
|
113 |
-
prompt = f"""
|
114 |
-
λ€μμ μ νλΈ μμμ νμλΌμΈκ³Ό κ° μΉμ
μ λ΄μ©μ
λλ€. μ΄λ₯Ό λ°νμΌλ‘ νμλΌμΈ μμ½μ μμ±ν΄μ£ΌμΈμ:
|
115 |
-
|
116 |
-
1. κ° μΉμ
μ μμ μκ°μ μ μ§νλ©΄μ ν΅μ¬ λ΄μ©μ κ°κ²°νκ² μμ½νμΈμ.
|
117 |
-
2. μμ½μ νκΈλ‘ μμ±νμΈμ.
|
118 |
-
3. κ° μΉμ
μ μμ½μ 1-2λ¬Έμ₯μΌλ‘ μ ννμΈμ.
|
119 |
-
4. μ 체 λ§₯λ½μ κ³ λ €νμ¬ μμ½νλ, κ° μΉμ
μ κ³ μ ν λ΄μ©μ λμΉμ§ λ§μΈμ.
|
120 |
-
5. μΆλ ₯ νμμ λ€μκ³Ό κ°μ΄ μ μ§νμΈμ:
|
121 |
-
[μμ μκ°] μΉμ
μμ½
|
122 |
-
|
123 |
-
μΉμ
λ΄μ©:
|
124 |
-
{combined_sections}
|
125 |
-
"""
|
126 |
-
|
127 |
-
try:
|
128 |
-
response = call_api(prompt, max_tokens=1000, temperature=0.3, top_p=0.9)
|
129 |
-
|
130 |
-
# μλ΅μ μ€ λ¨μλ‘ λΆλ¦¬νκ³ κ° μ€μ HTML νμμΌλ‘ λ³ν
|
131 |
-
timeline_items = response.strip().split('\n')
|
132 |
-
formatted_timeline = []
|
133 |
-
|
134 |
-
for item in timeline_items:
|
135 |
-
if ':' in item: # μκ° μ λ³΄κ° μλ νλͺ©λ§ μ²λ¦¬
|
136 |
-
time, summary = item.split(':', 1)
|
137 |
-
formatted_timeline.append(f"<p><strong>{time.strip()}</strong>:{summary.strip()}</p>")
|
138 |
-
|
139 |
-
timeline_html = "\n".join(formatted_timeline)
|
140 |
-
|
141 |
-
if not timeline_html:
|
142 |
-
raise ValueError("μ ν¨ν νμλΌμΈ μμ½μ μμ±νμ§ λͺ»νμ΅λλ€.")
|
143 |
-
|
144 |
-
return f"""
|
145 |
-
<h3>νμλΌμΈ μμ½:</h3>
|
146 |
-
<div style="white-space: pre-wrap; max-height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px;">
|
147 |
-
{timeline_html}
|
148 |
-
</div>
|
149 |
-
"""
|
150 |
-
except Exception as e:
|
151 |
-
logging.exception("νμλΌμΈ μμ½ μμ± μ€ μ€λ₯ λ°μ")
|
152 |
-
return "<p>νμλΌμΈ μμ½μ μμ±νλ μ€ μ€λ₯κ° λ°μνμ΅λλ€. λ€μ μλν΄ μ£ΌμΈμ.</p>"
|
153 |
-
|
154 |
def summarize_text(text):
|
155 |
prompt = f"""
|
156 |
1. λ€μ μ£Όμ΄μ§λ μ νλΈ λλ³Έμ ν΅μ¬ μ£Όμ μ λͺ¨λ μ£Όμ λ΄μ©μ μμΈνκ² μμ½νλΌ
|
@@ -163,86 +94,55 @@ def summarize_text(text):
|
|
163 |
8. λλ³Έμμ μ λ¬νλ κ°μ μ΄λ λΆμκΈ°λ ν¬ν¨
|
164 |
9. λ°λμ κΈ°μ μ μ©μ΄λ μ λ¬Έ μ©μ΄κ° μμ κ²½μ°, μ΄λ₯Ό μ ννκ² μ¬μ©
|
165 |
10. λλ³Έμ λͺ©μ μ΄λ μλλ₯Ό νμ
νκ³ , μ΄λ₯Ό μμ½μ λ°λμ λ°μ
|
166 |
-
11. μ 체κΈμ 보κ³
|
167 |
|
168 |
-
|
|
|
|
|
|
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
173 |
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
|
|
176 |
with gr.Blocks() as demo:
|
177 |
gr.Markdown("## YouTube μ€ν¬λ¦½νΈ μΆμΆ λ° μμ½ λꡬ")
|
178 |
-
|
179 |
youtube_url_input = gr.Textbox(label="YouTube URL μ
λ ₯")
|
180 |
analyze_button = gr.Button("λΆμνκΈ°")
|
181 |
script_output = gr.HTML(label="μ€ν¬λ¦½νΈ")
|
182 |
timeline_output = gr.HTML(label="νμλΌμΈ μμ½")
|
183 |
summary_output = gr.HTML(label="μ 체 μμ½")
|
184 |
-
|
185 |
cached_data = gr.State({"url": "", "title": "", "script": "", "sections": []})
|
186 |
|
187 |
-
def extract_and_cache(url, cache):
|
188 |
-
if url == cache["url"]:
|
189 |
-
return cache["title"], cache["script"], cache["sections"], cache
|
190 |
-
|
191 |
-
try:
|
192 |
-
title, script, sections = get_youtube_script(url)
|
193 |
-
new_cache = {"url": url, "title": title, "script": script, "sections": sections}
|
194 |
-
return title, script, sections, new_cache
|
195 |
-
except Exception as e:
|
196 |
-
logging.exception("λ°μ΄ν° μΆμΆ μ€ μ€λ₯ λ°μ")
|
197 |
-
raise gr.Error(f"μ€ν¬λ¦½νΈ μΆμΆ μ€ν¨: {str(e)}")
|
198 |
-
|
199 |
-
def display_script(title, script):
|
200 |
-
formatted_script = "\n".join(split_sentences(script))
|
201 |
-
script_html = f"""<h2 style='font-size:24px;'>{title}</h2>
|
202 |
-
<details>
|
203 |
-
<summary><h3>μλ¬Έ μ€ν¬λ¦½νΈ (ν΄λ¦νμ¬ νΌμΉκΈ°)</h3></summary>
|
204 |
-
<div style="white-space: pre-wrap;">{formatted_script}</div>
|
205 |
-
</details>"""
|
206 |
-
return script_html
|
207 |
-
|
208 |
-
def display_timeline(sections):
|
209 |
-
timeline_summary = generate_timeline_summary(sections)
|
210 |
-
timeline_html = f"""
|
211 |
-
<h3>νμλΌμΈ μμ½:</h3>
|
212 |
-
<div style="white-space: pre-wrap; max-height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px;">
|
213 |
-
{timeline_summary}
|
214 |
-
</div>
|
215 |
-
"""
|
216 |
-
return timeline_html
|
217 |
-
|
218 |
-
def generate_summary(script):
|
219 |
-
summary = summarize_text(script)
|
220 |
-
summary_html = f"""
|
221 |
-
<h3>μ 체 μμ½:</h3>
|
222 |
-
<div style="white-space: pre-wrap; max-height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px;">
|
223 |
-
{summary}
|
224 |
-
</div>
|
225 |
-
"""
|
226 |
-
return summary_html
|
227 |
-
|
228 |
-
def analyze(url, cache):
|
229 |
-
try:
|
230 |
-
title, script, sections, new_cache = extract_and_cache(url, cache)
|
231 |
-
script_html = display_script(title, script)
|
232 |
-
timeline_html = generate_timeline_summary(sections)
|
233 |
-
summary_html = generate_summary(script)
|
234 |
-
return script_html, timeline_html, summary_html, new_cache
|
235 |
-
except gr.Error as e:
|
236 |
-
return str(e), "", "", cache
|
237 |
-
except Exception as e:
|
238 |
-
error_msg = f"μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}"
|
239 |
-
logging.exception(error_msg)
|
240 |
-
return error_msg, "", "", cache
|
241 |
-
|
242 |
analyze_button.click(
|
243 |
analyze,
|
244 |
inputs=[youtube_url_input, cached_data],
|
245 |
outputs=[script_output, timeline_output, summary_output, cached_data]
|
246 |
)
|
247 |
|
248 |
-
|
|
|
|
2 |
from gradio_client import Client
|
3 |
import json
|
4 |
import logging
|
|
|
5 |
import openai
|
6 |
import os
|
|
|
|
|
7 |
|
8 |
+
# λ‘κΉ
μ€μ
|
9 |
logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG,
|
10 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
|
|
|
21 |
raise ValueError(f"μμμΉ λͺ»ν μλ΅ νμμ
λλ€. λ°μ λ°μ΄ν° νμ
: {type(response)}")
|
22 |
return response
|
23 |
except Exception as e:
|
24 |
+
logging.error(f"API μλ΅ νμ± μ€ν¨: {str(e)}")
|
25 |
raise ValueError(f"API μλ΅ νμ± μ€ν¨: {str(e)}")
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def get_youtube_script(url):
|
28 |
logging.info(f"μ€ν¬λ¦½νΈ μΆμΆ μμ: URL = {url}")
|
|
|
29 |
client = Client("whispersound/YT_Ts_R")
|
|
|
30 |
try:
|
|
|
31 |
result = client.predict(youtube_url=url, api_name="/predict")
|
|
|
|
|
32 |
parsed_result = parse_api_response(result)
|
33 |
|
34 |
if 'data' not in parsed_result or not parsed_result['data']:
|
35 |
raise ValueError("API μλ΅μ μ ν¨ν λ°μ΄ν°κ° μμ΅λλ€.")
|
36 |
|
37 |
+
data = parsed_result["data"][0]
|
38 |
+
title = data.get("title", "μ λͺ© μμ")
|
39 |
+
transcription_text = data.get("transcriptionAsText", "")
|
40 |
+
sections = data.get("sections", [])
|
41 |
|
42 |
if not transcription_text:
|
43 |
raise ValueError("μΆμΆλ μ€ν¬λ¦½νΈκ° μμ΅λλ€.")
|
44 |
|
45 |
logging.info("μ€ν¬λ¦½νΈ μΆμΆ μλ£")
|
46 |
return title, transcription_text, sections
|
|
|
47 |
except Exception as e:
|
48 |
+
logging.exception("μ€ν¬λ¦½νΈ μΆμΆ μ€ μ€λ₯ λ°μ")
|
|
|
49 |
raise
|
50 |
|
51 |
+
def format_time(seconds):
|
52 |
+
minutes, seconds = divmod(seconds, 60)
|
53 |
+
hours, minutes = divmod(minutes, 60)
|
54 |
+
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"
|
55 |
+
|
56 |
+
def generate_timeline_summary(sections):
|
57 |
+
timeline_items = []
|
58 |
+
for section in sections:
|
59 |
+
start_time = format_time(section['start_time'])
|
60 |
+
text = section['text']
|
61 |
+
timeline_items.append(f"<p><strong>{start_time}</strong>: {text}</p>")
|
62 |
+
|
63 |
+
timeline_html = "\n".join(timeline_items)
|
64 |
+
return f"""
|
65 |
+
<h3>νμλΌμΈ μμ½:</h3>
|
66 |
+
<div style="white-space: pre-wrap; max-height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px;">
|
67 |
+
{timeline_html}
|
68 |
+
</div>
|
69 |
+
"""
|
70 |
+
|
71 |
def call_api(prompt, max_tokens, temperature, top_p):
|
72 |
try:
|
73 |
response = openai.ChatCompletion.create(
|
|
|
82 |
logging.exception("LLM API νΈμΆ μ€ μ€λ₯ λ°μ")
|
83 |
raise
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
def summarize_text(text):
|
86 |
prompt = f"""
|
87 |
1. λ€μ μ£Όμ΄μ§λ μ νλΈ λλ³Έμ ν΅μ¬ μ£Όμ μ λͺ¨λ μ£Όμ λ΄μ©μ μμΈνκ² μμ½νλΌ
|
|
|
94 |
8. λλ³Έμμ μ λ¬νλ κ°μ μ΄λ λΆμκΈ°λ ν¬ν¨
|
95 |
9. λ°λμ κΈ°μ μ μ©μ΄λ μ λ¬Έ μ©μ΄κ° μμ κ²½μ°, μ΄λ₯Ό μ ννκ² μ¬μ©
|
96 |
10. λλ³Έμ λͺ©μ μ΄λ μλλ₯Ό νμ
νκ³ , μ΄λ₯Ό μμ½μ λ°λμ λ°μ
|
|
|
97 |
|
98 |
+
λλ³Έ:
|
99 |
+
{text}
|
100 |
+
"""
|
101 |
+
return call_api(prompt, max_tokens=2000, temperature=0.3, top_p=0.9)
|
102 |
|
103 |
+
def display_script(title, script):
|
104 |
+
return f"""<h2 style='font-size:24px;'>{title}</h2>
|
105 |
+
<details>
|
106 |
+
<summary><h3>μλ¬Έ μ€ν¬λ¦½νΈ (ν΄λ¦νμ¬ νΌμΉκΈ°)</h3></summary>
|
107 |
+
<div style="white-space: pre-wrap;">{script}</div>
|
108 |
+
</details>"""
|
109 |
|
110 |
+
def analyze(url, cache):
|
111 |
+
try:
|
112 |
+
if url == cache["url"]:
|
113 |
+
logging.info(f"μΊμλ λ°μ΄ν° μ¬μ©: URL = {url}")
|
114 |
+
title, script, sections = cache["title"], cache["script"], cache["sections"]
|
115 |
+
else:
|
116 |
+
logging.info(f"μλ‘μ΄ λ°μ΄ν° μΆμΆ μμ: URL = {url}")
|
117 |
+
title, script, sections = get_youtube_script(url)
|
118 |
+
cache = {"url": url, "title": title, "script": script, "sections": sections}
|
119 |
+
|
120 |
+
script_html = display_script(title, script)
|
121 |
+
timeline_html = generate_timeline_summary(sections)
|
122 |
+
summary_html = summarize_text(script)
|
123 |
+
|
124 |
+
logging.info("λΆμ μλ£")
|
125 |
+
return script_html, timeline_html, summary_html, cache
|
126 |
+
except Exception as e:
|
127 |
+
error_msg = f"μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}"
|
128 |
+
logging.exception(error_msg)
|
129 |
+
return error_msg, "", "", cache
|
130 |
|
131 |
+
# Gradio μΈν°νμ΄μ€
|
132 |
with gr.Blocks() as demo:
|
133 |
gr.Markdown("## YouTube μ€ν¬λ¦½νΈ μΆμΆ λ° μμ½ λꡬ")
|
|
|
134 |
youtube_url_input = gr.Textbox(label="YouTube URL μ
λ ₯")
|
135 |
analyze_button = gr.Button("λΆμνκΈ°")
|
136 |
script_output = gr.HTML(label="μ€ν¬λ¦½νΈ")
|
137 |
timeline_output = gr.HTML(label="νμλΌμΈ μμ½")
|
138 |
summary_output = gr.HTML(label="μ 체 μμ½")
|
|
|
139 |
cached_data = gr.State({"url": "", "title": "", "script": "", "sections": []})
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
analyze_button.click(
|
142 |
analyze,
|
143 |
inputs=[youtube_url_input, cached_data],
|
144 |
outputs=[script_output, timeline_output, summary_output, cached_data]
|
145 |
)
|
146 |
|
147 |
+
if __name__ == "__main__":
|
148 |
+
demo.launch(share=True)
|