SUHHHH commited on
Commit
8b1a899
β€’
1 Parent(s): 5560021

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from pytube import YouTube
2
  from google.cloud import speech_v1p1beta1 as speech
 
3
  import io
4
  import openai
5
  import os
@@ -24,12 +25,18 @@ def get_yt_video_id(url):
24
  else:
25
  raise ValueError("μœ νš¨ν•œ 유튜브 링크가 μ•„λ‹™λ‹ˆλ‹€.")
26
 
27
- # μ˜€λ””μ˜€ μΆ”μΆœ ν•¨μˆ˜
28
- def download_audio(youtube_url):
29
  yt = YouTube(youtube_url)
30
  stream = yt.streams.filter(only_audio=True).first()
31
  audio_path = stream.download(filename="audio.mp4")
32
- return audio_path
 
 
 
 
 
 
33
 
34
  # Google Speech-to-Text APIλ₯Ό μ‚¬μš©ν•˜μ—¬ μ˜€λ””μ˜€λ₯Ό ν…μŠ€νŠΈλ‘œ λ³€ν™˜
35
  def speech_to_text(audio_path):
@@ -40,8 +47,8 @@ def speech_to_text(audio_path):
40
 
41
  audio = speech.RecognitionAudio(content=content)
42
  config = speech.RecognitionConfig(
43
- encoding=speech.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED,
44
- sample_rate_hertz=16000,
45
  language_code="ko-KR" # ν•œκ΅­μ–΄ 인식
46
  )
47
 
@@ -70,8 +77,8 @@ def textToSummary(text):
70
  # 전체 μš”μ•½ ν”„λ‘œμ„ΈμŠ€λ₯Ό μ²˜λ¦¬ν•˜λŠ” ν•¨μˆ˜
71
  def summarize(url):
72
  try:
73
- # 유튜브 μ˜€λ””μ˜€ λ‹€μš΄λ‘œλ“œ
74
- audio_path = download_audio(url)
75
  # μŒμ„±μ„ ν…μŠ€νŠΈλ‘œ λ³€ν™˜
76
  transcript = speech_to_text(audio_path)
77
  # ν…μŠ€νŠΈ μš”μ•½
 
1
  from pytube import YouTube
2
  from google.cloud import speech_v1p1beta1 as speech
3
+ from pydub import AudioSegment # μ˜€λ””μ˜€ λ³€ν™˜μ„ μœ„ν•œ 라이브러리
4
  import io
5
  import openai
6
  import os
 
25
  else:
26
  raise ValueError("μœ νš¨ν•œ 유튜브 링크가 μ•„λ‹™λ‹ˆλ‹€.")
27
 
28
+ # μ˜€λ””μ˜€ μΆ”μΆœ 및 λ³€ν™˜ ν•¨μˆ˜ (WAV ν˜•μ‹μœΌλ‘œ λ³€ν™˜)
29
+ def download_and_convert_audio(youtube_url):
30
  yt = YouTube(youtube_url)
31
  stream = yt.streams.filter(only_audio=True).first()
32
  audio_path = stream.download(filename="audio.mp4")
33
+
34
+ # μ˜€λ””μ˜€ νŒŒμΌμ„ WAV둜 λ³€ν™˜ (16000Hz μƒ˜ν”Œ 레이트)
35
+ audio = AudioSegment.from_file(audio_path)
36
+ wav_audio_path = "converted_audio.wav"
37
+ audio.set_frame_rate(16000).export(wav_audio_path, format="wav")
38
+
39
+ return wav_audio_path
40
 
41
  # Google Speech-to-Text APIλ₯Ό μ‚¬μš©ν•˜μ—¬ μ˜€λ””μ˜€λ₯Ό ν…μŠ€νŠΈλ‘œ λ³€ν™˜
42
  def speech_to_text(audio_path):
 
47
 
48
  audio = speech.RecognitionAudio(content=content)
49
  config = speech.RecognitionConfig(
50
+ encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
51
+ sample_rate_hertz=16000, # 16000Hz μƒ˜ν”Œ 레이트
52
  language_code="ko-KR" # ν•œκ΅­μ–΄ 인식
53
  )
54
 
 
77
  # 전체 μš”μ•½ ν”„λ‘œμ„ΈμŠ€λ₯Ό μ²˜λ¦¬ν•˜λŠ” ν•¨μˆ˜
78
  def summarize(url):
79
  try:
80
+ # 유튜브 μ˜€λ””μ˜€ λ‹€μš΄λ‘œλ“œ 및 λ³€ν™˜
81
+ audio_path = download_and_convert_audio(url)
82
  # μŒμ„±μ„ ν…μŠ€νŠΈλ‘œ λ³€ν™˜
83
  transcript = speech_to_text(audio_path)
84
  # ν…μŠ€νŠΈ μš”μ•½