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