AIRider commited on
Commit
bb03802
Β·
verified Β·
1 Parent(s): 57d8b30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
app.py CHANGED
@@ -11,6 +11,8 @@ import re
11
  logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG,
12
  format='%(asctime)s - %(levelname)s - %(message)s')
13
 
 
 
14
  def parse_api_response(response):
15
  try:
16
  if isinstance(response, str):
@@ -68,8 +70,6 @@ def get_youtube_script(url):
68
  logging.exception(error_msg)
69
  return "", "", []
70
 
71
- openai.api_key = os.getenv("OPENAI_API_KEY")
72
-
73
  def call_api(prompt, max_tokens, temperature, top_p):
74
  try:
75
  response = openai.ChatCompletion.create(
@@ -155,6 +155,8 @@ with gr.Blocks() as demo:
155
  return title, script, sections, new_cache
156
 
157
  def display_script(title, script):
 
 
158
  formatted_script = "\n".join(split_sentences(script))
159
  script_html = f"""<h2 style='font-size:24px;'>{title}</h2>
160
  <details>
@@ -164,6 +166,8 @@ with gr.Blocks() as demo:
164
  return script_html
165
 
166
  def display_timeline(sections):
 
 
167
  timeline_summary = generate_timeline_summary(sections)
168
  timeline_html = f"""
169
  <h3>νƒ€μž„λΌμΈ μš”μ•½:</h3>
@@ -174,6 +178,8 @@ with gr.Blocks() as demo:
174
  return timeline_html
175
 
176
  def generate_summary(script):
 
 
177
  summary = summarize_text(script)
178
  summary_html = f"""
179
  <h3>전체 μš”μ•½:</h3>
@@ -187,21 +193,13 @@ with gr.Blocks() as demo:
187
  title, script, sections, new_cache = extract_and_cache(url, cache)
188
  script_html = display_script(title, script)
189
  timeline_html = display_timeline(sections)
190
- return script_html, timeline_html, new_cache
191
-
192
- def update_summary(cache):
193
- if not cache["script"]:
194
- return "μŠ€ν¬λ¦½νŠΈκ°€ μ—†μŠ΅λ‹ˆλ‹€. λ¨Όμ € YouTube URL을 μž…λ ₯ν•˜κ³  뢄석을 μ‹€ν–‰ν•΄μ£Όμ„Έμš”."
195
- return generate_summary(cache["script"])
196
 
197
  analyze_button.click(
198
  analyze,
199
  inputs=[youtube_url_input, cached_data],
200
- outputs=[script_output, timeline_output, cached_data]
201
- ).then(
202
- update_summary,
203
- inputs=[cached_data],
204
- outputs=summary_output
205
  )
206
 
207
  demo.launch(share=True)
 
11
  logging.basicConfig(filename='youtube_script_extractor.log', level=logging.DEBUG,
12
  format='%(asctime)s - %(levelname)s - %(message)s')
13
 
14
+ openai.api_key = os.getenv("OPENAI_API_KEY")
15
+
16
  def parse_api_response(response):
17
  try:
18
  if isinstance(response, str):
 
70
  logging.exception(error_msg)
71
  return "", "", []
72
 
 
 
73
  def call_api(prompt, max_tokens, temperature, top_p):
74
  try:
75
  response = openai.ChatCompletion.create(
 
155
  return title, script, sections, new_cache
156
 
157
  def display_script(title, script):
158
+ if not script:
159
+ return "<p>슀크립트λ₯Ό μΆ”μΆœν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. URL을 ν™•μΈν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.</p>"
160
  formatted_script = "\n".join(split_sentences(script))
161
  script_html = f"""<h2 style='font-size:24px;'>{title}</h2>
162
  <details>
 
166
  return script_html
167
 
168
  def display_timeline(sections):
169
+ if not sections:
170
+ return "<p>νƒ€μž„λΌμΈμ„ μƒμ„±ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. 슀크립트 μΆ”μΆœμ— μ‹€νŒ¨ν–ˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.</p>"
171
  timeline_summary = generate_timeline_summary(sections)
172
  timeline_html = f"""
173
  <h3>νƒ€μž„λΌμΈ μš”μ•½:</h3>
 
178
  return timeline_html
179
 
180
  def generate_summary(script):
181
+ if not script:
182
+ return "<p>전체 μš”μ•½μ„ μƒμ„±ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. 슀크립트 μΆ”μΆœμ— μ‹€νŒ¨ν–ˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.</p>"
183
  summary = summarize_text(script)
184
  summary_html = f"""
185
  <h3>전체 μš”μ•½:</h3>
 
193
  title, script, sections, new_cache = extract_and_cache(url, cache)
194
  script_html = display_script(title, script)
195
  timeline_html = display_timeline(sections)
196
+ summary_html = generate_summary(script)
197
+ return script_html, timeline_html, summary_html, new_cache
 
 
 
 
198
 
199
  analyze_button.click(
200
  analyze,
201
  inputs=[youtube_url_input, cached_data],
202
+ outputs=[script_output, timeline_output, summary_output, cached_data]
 
 
 
 
203
  )
204
 
205
  demo.launch(share=True)