Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_client import Client
|
3 |
+
import json
|
4 |
+
import logging
|
5 |
+
import ast
|
6 |
+
import openai # OpenAI λΌμ΄λΈλ¬λ¦¬ μΆκ°
|
7 |
+
import os
|
8 |
+
import random
|
9 |
+
import re
|
10 |
+
|
11 |
+
# λ‘κΉ
μ€μ
|
12 |
+
logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG,
|
13 |
+
format='%(asctime)s - %(levelname)s - %(message)s')
|
14 |
+
|
15 |
+
def parse_api_response(response):
|
16 |
+
try:
|
17 |
+
if isinstance(response, str):
|
18 |
+
response = ast.literal_eval(response)
|
19 |
+
if isinstance(response, list) and len(response) > 0:
|
20 |
+
response = response[0]
|
21 |
+
if not isinstance(response, dict):
|
22 |
+
raise ValueError(f"μμμΉ λͺ»ν μλ΅ νμμ
λλ€. λ°μ λ°μ΄ν° νμ
: {type(response)}")
|
23 |
+
return response
|
24 |
+
except Exception as e:
|
25 |
+
raise ValueError(f"API μλ΅ νμ± μ€ν¨: {str(e)}")
|
26 |
+
|
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: # 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 |
+
# μλν¬μΈνΈλ₯Ό μλ‘μ΄ κ²μΌλ‘ λ³κ²½
|
53 |
+
client = Client("whispersound/YT_Ts_R")
|
54 |
+
|
55 |
+
try:
|
56 |
+
logging.debug("API νΈμΆ μμ")
|
57 |
+
result = client.predict(youtube_url=url, api_name="/predict")
|
58 |
+
logging.debug("API νΈμΆ μλ£")
|
59 |
+
|
60 |
+
# μλ΅ νμ±
|
61 |
+
parsed_result = parse_api_response(result)
|
62 |
+
|
63 |
+
title = parsed_result["data"][0]["title"]
|
64 |
+
transcription_text = parsed_result["data"][0]["transcriptionAsText"]
|
65 |
+
|
66 |
+
logging.info("μ€ν¬λ¦½νΈ μΆμΆ μλ£")
|
67 |
+
return title, transcription_text
|
68 |
+
|
69 |
+
except Exception as e:
|
70 |
+
error_msg = f"μ€ν¬λ¦½νΈ μΆμΆ μ€ μ€λ₯ λ°μ: {str(e)}"
|
71 |
+
logging.exception(error_msg)
|
72 |
+
return "", ""
|
73 |
+
|
74 |
+
# OpenAI API ν€ μ€μ
|
75 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
76 |
+
|
77 |
+
# LLM API νΈμΆ ν¨μ
|
78 |
+
def call_api(prompt, max_tokens, temperature, top_p):
|
79 |
+
try:
|
80 |
+
response = openai.ChatCompletion.create(
|
81 |
+
model="gpt-4o-mini", # λͺ¨λΈμ gpt-4o-miniλ‘ λ³κ²½
|
82 |
+
messages=[{"role": "user", "content": prompt}],
|
83 |
+
max_tokens=max_tokens,
|
84 |
+
temperature=temperature,
|
85 |
+
top_p=top_p
|
86 |
+
)
|
87 |
+
return response['choices'][0]['message']['content']
|
88 |
+
except Exception as e:
|
89 |
+
logging.exception("LLM API νΈμΆ μ€ μ€λ₯ λ°μ")
|
90 |
+
return "μμ½μ μμ±νλ λμ μ€λ₯κ° λ°μνμ΅λλ€. λμ€μ λ€μ μλν΄ μ£ΌμΈμ."
|
91 |
+
|
92 |
+
# ν
μ€νΈ μμ½ ν¨μ
|
93 |
+
def summarize_text(text):
|
94 |
+
prompt = f"""λΉμ μ YouTube λΉλμ€ μ€ν¬λ¦½νΈλ₯Ό μμ½νλ AI μ΄μμ€ν΄νΈμ
λλ€.
|
95 |
+
μλ μ 곡λ μ€ν¬λ¦½νΈλ₯Ό κ°κ²°νλ©΄μλ ν¬κ΄μ μΌλ‘ μμ½ν΄μ£ΌμΈμ.
|
96 |
+
λΉλμ€μ μ£Όμ μ£Όμ , ν΅μ¬ ν¬μΈνΈ, μ λ°μ μΈ λ©μμ§μ μ΄μ μ λ§μΆμΈμ.
|
97 |
+
μμ½μ μ ꡬ쑰νλκ³ μ΄ν΄νκΈ° μ¬μμΌ νλ©°, λ΄μ©μ λ³Έμ§μ ν¬μ°©ν΄μΌ ν©λλ€.
|
98 |
+
λ°λμ νκ΅μ΄λ‘ μμ½μ μ 곡νμΈμ.
|
99 |
+
λ€μ κ΅¬μ‘°λ‘ μμ½μ μμ±ν΄μ£ΌμΈμ:
|
100 |
+
1. λΉλμ€μ μ£Όμ μ£Όμ λλ ν
λ§
|
101 |
+
2. μ μλ μ£Όμ ν¬μΈνΈ λλ λ
Όμ
|
102 |
+
3. μ€μν κ²°λ‘ λλ μμ¬μ
|
103 |
+
μμ½ν μ€ν¬λ¦½νΈ:
|
104 |
+
{text}
|
105 |
+
μ μ€ν¬λ¦½νΈμ λν μμ½μ μ 곡ν΄μ£ΌμΈμ. μμ½μ κ°κ²°νλ©΄μλ μ λ³΄κ° νλΆν΄μΌ νλ©°, λΉλμ€ λ΄μ©μ ν΅μ¬μ ν¬μ°©ν΄μΌ ν©λλ€."""
|
106 |
+
|
107 |
+
try:
|
108 |
+
return call_api(prompt, max_tokens=2000, temperature=0.3, top_p=0.9)
|
109 |
+
except Exception as e:
|
110 |
+
logging.exception("μμ½ μμ± μ€ μ€λ₯ λ°μ")
|
111 |
+
return "μμ½μ μμ±νλ λμ μ€λ₯κ° λ°μνμ΅λλ€. λμ€μ λ€μ μλν΄ μ£ΌμΈμ."
|
112 |
+
|
113 |
+
# Gradio μΈν°νμ΄μ€ μ€μ
|
114 |
+
with gr.Blocks() as demo:
|
115 |
+
gr.Markdown("## YouTube Script Extractor with Summary")
|
116 |
+
|
117 |
+
youtube_url_input = gr.Textbox(label="YouTube URL μ
λ ₯")
|
118 |
+
analyze_button = gr.Button("λΆμνκΈ°")
|
119 |
+
script_output = gr.HTML(label="μ€ν¬λ¦½νΈ")
|
120 |
+
summary_output = gr.HTML(label="μμ½")
|
121 |
+
|
122 |
+
# μΊμλ₯Ό μν μν λ³μ
|
123 |
+
cached_data = gr.State({"url": "", "title": "", "script": ""})
|
124 |
+
|
125 |
+
def extract_and_cache(url, cache):
|
126 |
+
if url == cache["url"]:
|
127 |
+
return cache["title"], cache["script"], cache
|
128 |
+
|
129 |
+
title, script = get_youtube_script(url)
|
130 |
+
new_cache = {"url": url, "title": title, "script": script}
|
131 |
+
return title, script, new_cache
|
132 |
+
|
133 |
+
def display_script(title, script):
|
134 |
+
formatted_script = "\n".join(split_sentences(script))
|
135 |
+
script_html = f"""<h2 style='font-size:24px;'>{title}</h2>
|
136 |
+
<details>
|
137 |
+
<summary><h3>μλ¬Έ μ€ν¬λ¦½νΈ (ν΄λ¦νμ¬ νΌμΉκΈ°)</h3></summary>
|
138 |
+
<pre>{formatted_script}</pre>
|
139 |
+
</details>"""
|
140 |
+
return script_html
|
141 |
+
|
142 |
+
def generate_summary(script):
|
143 |
+
summary = summarize_text(script)
|
144 |
+
summary_html = f"<h3>μμ½:</h3>\n<pre>{summary}</pre>"
|
145 |
+
return summary_html
|
146 |
+
|
147 |
+
def analyze(url, cache):
|
148 |
+
title, script, new_cache = extract_and_cache(url, cache)
|
149 |
+
script_html = display_script(title, script)
|
150 |
+
return script_html, new_cache
|
151 |
+
|
152 |
+
def update_summary(cache):
|
153 |
+
if not cache["script"]:
|
154 |
+
return "μ€ν¬λ¦½νΈκ° μμ΅λλ€. λ¨Όμ YouTube URLμ μ
λ ₯νκ³ λΆμμ μ€νν΄μ£ΌμΈμ."
|
155 |
+
return generate_summary(cache["script"])
|
156 |
+
|
157 |
+
# λ²νΌ ν΄λ¦ μ μ€ν¬λ¦½νΈ μΆμΆ
|
158 |
+
analyze_button.click(
|
159 |
+
analyze,
|
160 |
+
inputs=[youtube_url_input, cached_data],
|
161 |
+
outputs=[script_output, cached_data]
|
162 |
+
).then(
|
163 |
+
update_summary,
|
164 |
+
inputs=[cached_data],
|
165 |
+
outputs=summary_output
|
166 |
+
)
|
167 |
+
|
168 |
+
# μΈν°νμ΄μ€ μ€ν
|
169 |
+
demo.launch(share=True)
|