Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import json
|
|
4 |
import logging
|
5 |
import openai
|
6 |
import os
|
|
|
7 |
|
8 |
# λ‘κΉ
μ€μ
|
9 |
logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG,
|
@@ -11,6 +12,28 @@ logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG
|
|
11 |
|
12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def parse_api_response(response):
|
15 |
try:
|
16 |
if isinstance(response, str):
|
@@ -36,13 +59,14 @@ def get_youtube_script(url):
|
|
36 |
|
37 |
data = parsed_result["data"][0]
|
38 |
title = data.get("title", "μ λͺ© μμ")
|
|
|
39 |
transcription_text = data.get("transcriptionAsText", "")
|
40 |
|
41 |
if not transcription_text:
|
42 |
raise ValueError("μΆμΆλ μ€ν¬λ¦½νΈκ° μμ΅λλ€.")
|
43 |
|
44 |
logging.info("μ€ν¬λ¦½νΈ μΆμΆ μλ£")
|
45 |
-
return title, transcription_text
|
46 |
except Exception as e:
|
47 |
logging.exception("μ€ν¬λ¦½νΈ μΆμΆ μ€ μ€λ₯ λ°μ")
|
48 |
raise
|
@@ -61,9 +85,14 @@ def call_api(prompt, max_tokens, temperature, top_p):
|
|
61 |
logging.exception("LLM API νΈμΆ μ€ μ€λ₯ λ°μ")
|
62 |
raise
|
63 |
|
64 |
-
def summarize_text(text):
|
65 |
prompt = f"""
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
2. λ°λμ νκΈλ‘ μμ±νλΌ
|
68 |
3. μμ½λ¬Έλ§μΌλ‘λ μμμ μ§μ μμ²ν κ²κ³Ό λμΌν μμ€μΌλ‘ λ΄μ©μ μ΄ν΄ν μ μλλ‘ μμΈν μμ±
|
69 |
4. κΈμ λ무 μμΆνκ±°λ ν¨μΆνμ§ λ§κ³ , μ€μν λ΄μ©κ³Ό μΈλΆμ¬νμ λͺ¨λ ν¬ν¨
|
@@ -81,6 +110,10 @@ def summarize_text(text):
|
|
81 |
return call_api(prompt, max_tokens=2000, temperature=0.3, top_p=0.9)
|
82 |
|
83 |
def create_collapsible_section(section_title, video_title, content):
|
|
|
|
|
|
|
|
|
84 |
return f"""
|
85 |
<details>
|
86 |
<summary style="cursor: pointer; font-weight: bold;">{section_title}</summary>
|
@@ -95,18 +128,18 @@ def analyze(url, cache):
|
|
95 |
try:
|
96 |
if url == cache["url"]:
|
97 |
logging.info(f"μΊμλ λ°μ΄ν° μ¬μ©: URL = {url}")
|
98 |
-
title, script = cache["title"], cache["script"]
|
99 |
else:
|
100 |
logging.info(f"μλ‘μ΄ λ°μ΄ν° μΆμΆ μμ: URL = {url}")
|
101 |
-
title, script = get_youtube_script(url)
|
102 |
-
cache = {"url": url, "title": title, "script": script}
|
103 |
|
104 |
# μλ¬Έ μ€ν¬λ¦½νΈ μΉμ
μμ±
|
105 |
script_section = create_collapsible_section("μλ¬Έ μ€ν¬λ¦½νΈ", title, script)
|
106 |
yield script_section, cache
|
107 |
|
108 |
# μμ½ μμ± λ° μΉμ
μμ±
|
109 |
-
summary = summarize_text(script)
|
110 |
summary_section = create_collapsible_section("μμ½", title, summary)
|
111 |
yield script_section + summary_section, cache
|
112 |
|
@@ -121,7 +154,7 @@ with gr.Blocks() as demo:
|
|
121 |
youtube_url_input = gr.Textbox(label="YouTube URL μ
λ ₯")
|
122 |
analyze_button = gr.Button("λΆμνκΈ°")
|
123 |
content_output = gr.HTML(label="λ΄μ©")
|
124 |
-
cached_data = gr.State({"url": "", "title": "", "script": ""})
|
125 |
|
126 |
analyze_button.click(
|
127 |
analyze,
|
|
|
4 |
import logging
|
5 |
import openai
|
6 |
import os
|
7 |
+
import re
|
8 |
|
9 |
# λ‘κΉ
μ€μ
|
10 |
logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG,
|
|
|
12 |
|
13 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
14 |
|
15 |
+
# λ¬Έμ₯ κ΅¬λΆ ν¨μ
|
16 |
+
def split_sentences(text):
|
17 |
+
sentences = re.split(r"(λλ€|μμ|ꡬλ|ν΄μ|κ΅°μ|κ² μ΄μ|μμ€|ν΄λΌ|μμ|μμ|λ°μ|λμ|μΈμ|μ΄μ|κ²μ|ꡬμ|κ³ μ|λμ|νμ£ )(?![\w])", text)
|
18 |
+
combined_sentences = []
|
19 |
+
current_sentence = ""
|
20 |
+
for i in range(0, len(sentences), 2):
|
21 |
+
if i + 1 < len(sentences):
|
22 |
+
sentence = sentences[i] + sentences[i + 1]
|
23 |
+
else:
|
24 |
+
sentence = sentences[i]
|
25 |
+
if len(current_sentence) + len(sentence) > 100: # 100μλ₯Ό μ΄κ³Όν κ²½μ°
|
26 |
+
combined_sentences.append(current_sentence.strip())
|
27 |
+
current_sentence = sentence.strip()
|
28 |
+
else:
|
29 |
+
current_sentence += sentence
|
30 |
+
if sentence.endswith(('.', '?', '!')):
|
31 |
+
combined_sentences.append(current_sentence.strip())
|
32 |
+
current_sentence = ""
|
33 |
+
if current_sentence:
|
34 |
+
combined_sentences.append(current_sentence.strip())
|
35 |
+
return combined_sentences
|
36 |
+
|
37 |
def parse_api_response(response):
|
38 |
try:
|
39 |
if isinstance(response, str):
|
|
|
59 |
|
60 |
data = parsed_result["data"][0]
|
61 |
title = data.get("title", "μ λͺ© μμ")
|
62 |
+
description = data.get("description", "μ€λͺ
μμ")
|
63 |
transcription_text = data.get("transcriptionAsText", "")
|
64 |
|
65 |
if not transcription_text:
|
66 |
raise ValueError("μΆμΆλ μ€ν¬λ¦½νΈκ° μμ΅λλ€.")
|
67 |
|
68 |
logging.info("μ€ν¬λ¦½νΈ μΆμΆ μλ£")
|
69 |
+
return title, description, transcription_text
|
70 |
except Exception as e:
|
71 |
logging.exception("μ€ν¬λ¦½νΈ μΆμΆ μ€ μ€λ₯ λ°μ")
|
72 |
raise
|
|
|
85 |
logging.exception("LLM API νΈμΆ μ€ μ€λ₯ λ°μ")
|
86 |
raise
|
87 |
|
88 |
+
def summarize_text(title, description, text):
|
89 |
prompt = f"""
|
90 |
+
μ λͺ©: {title}
|
91 |
+
μ€λͺ
: {description}
|
92 |
+
|
93 |
+
μμ μ λͺ©κ³Ό μ€λͺ
μ μ΄ μ νλΈ μμμ μλ³Έ λ©νλ°μ΄ν°μ
λλ€. μ΄λ₯Ό μ°Έκ³ νμ¬ μλμ λλ³Έμ μμ½ν΄μ£ΌμΈμ.
|
94 |
+
|
95 |
+
1. μμ μ λͺ©κ³Ό μ€λͺ
μ μ°Έκ³ νμ¬ μ νλΈ λλ³Έμ ν΅μ¬ μ£Όμ μ λͺ¨λ μ£Όμ λ΄μ©μ μμΈνκ² μμ½νλΌ
|
96 |
2. λ°λμ νκΈλ‘ μμ±νλΌ
|
97 |
3. μμ½λ¬Έλ§μΌλ‘λ μμμ μ§μ μμ²ν κ²κ³Ό λμΌν μμ€μΌλ‘ λ΄μ©μ μ΄ν΄ν μ μλλ‘ μμΈν μμ±
|
98 |
4. κΈμ λ무 μμΆνκ±°λ ν¨μΆνμ§ λ§κ³ , μ€μν λ΄μ©κ³Ό μΈλΆμ¬νμ λͺ¨λ ν¬ν¨
|
|
|
110 |
return call_api(prompt, max_tokens=2000, temperature=0.3, top_p=0.9)
|
111 |
|
112 |
def create_collapsible_section(section_title, video_title, content):
|
113 |
+
if section_title == "μλ¬Έ μ€ν¬λ¦½νΈ":
|
114 |
+
sentences = split_sentences(content)
|
115 |
+
content = "\n".join(sentences)
|
116 |
+
|
117 |
return f"""
|
118 |
<details>
|
119 |
<summary style="cursor: pointer; font-weight: bold;">{section_title}</summary>
|
|
|
128 |
try:
|
129 |
if url == cache["url"]:
|
130 |
logging.info(f"μΊμλ λ°μ΄ν° μ¬μ©: URL = {url}")
|
131 |
+
title, description, script = cache["title"], cache["description"], cache["script"]
|
132 |
else:
|
133 |
logging.info(f"μλ‘μ΄ λ°μ΄ν° μΆμΆ μμ: URL = {url}")
|
134 |
+
title, description, script = get_youtube_script(url)
|
135 |
+
cache = {"url": url, "title": title, "description": description, "script": script}
|
136 |
|
137 |
# μλ¬Έ μ€ν¬λ¦½νΈ μΉμ
μμ±
|
138 |
script_section = create_collapsible_section("μλ¬Έ μ€ν¬λ¦½νΈ", title, script)
|
139 |
yield script_section, cache
|
140 |
|
141 |
# μμ½ μμ± λ° μΉμ
μμ±
|
142 |
+
summary = summarize_text(title, description, script)
|
143 |
summary_section = create_collapsible_section("μμ½", title, summary)
|
144 |
yield script_section + summary_section, cache
|
145 |
|
|
|
154 |
youtube_url_input = gr.Textbox(label="YouTube URL μ
λ ₯")
|
155 |
analyze_button = gr.Button("λΆμνκΈ°")
|
156 |
content_output = gr.HTML(label="λ΄μ©")
|
157 |
+
cached_data = gr.State({"url": "", "title": "", "description": "", "script": ""})
|
158 |
|
159 |
analyze_button.click(
|
160 |
analyze,
|