AIRider commited on
Commit
fe178da
โ€ข
1 Parent(s): e10e6bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -10,18 +10,19 @@ RAPIDAPI_HOST = "youtube-transcriptor.p.rapidapi.com"
10
 
11
  # ์œ ํŠœ๋ธŒ URL์—์„œ ๋น„๋””์˜ค ID๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
12
  def get_video_id(youtube_url):
13
- # ์ผ๋ฐ˜์ ์ธ YouTube URL ๋˜๋Š” youtu.be ๋‹จ์ถ• URL์—์„œ video_id ์ถ”์ถœ
14
  video_id_match = re.search(r"(?<=v=)[^#&?]*", youtube_url) or re.search(r"(?<=youtu.be/)[^#&?]*", youtube_url)
15
  return video_id_match.group(0) if video_id_match else None
16
 
17
  # ์ž๋ง‰ ์–ธ์–ด ์šฐ์„ ์ˆœ์œ„ ๋ฆฌ์ŠคํŠธ
18
  LANGUAGE_PRIORITY = ['ko', 'en', 'ja', 'zh']
19
 
20
- # ์œ ํŠœ๋ธŒ ์ž๋ง‰์„ ์š”์ฒญํ•˜๋Š” ํ•จ์ˆ˜
21
  def get_youtube_transcript(youtube_url):
22
  # ๋น„๋””์˜ค ID ์ถ”์ถœ
23
  video_id = get_video_id(youtube_url)
24
  if video_id is None:
 
25
  return {"error": "์ž˜๋ชป๋œ ์œ ํŠœ๋ธŒ URL์ž…๋‹ˆ๋‹ค. ๋น„๋””์˜ค ID๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
26
 
27
  url = "https://youtube-transcriptor.p.rapidapi.com/transcript"
@@ -34,21 +35,39 @@ def get_youtube_transcript(youtube_url):
34
  # ์–ธ์–ด ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ์š”์ฒญ์„ ์‹œ๋„
35
  for lang in LANGUAGE_PRIORITY:
36
  querystring = {"video_id": video_id, "lang": lang}
 
 
37
  response = requests.get(url, headers=headers, params=querystring)
38
 
 
 
 
 
 
 
 
39
  if response.status_code == 200:
40
  try:
 
41
  data = response.json()
42
- # transcription ํ•„๋“œ๋ฅผ ํ†ตํ•ด ์ž๋ง‰ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
 
 
 
 
43
  if "transcription" in data and isinstance(data["transcription"], list):
44
- # JSON ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ž๋ง‰ ์ถœ๋ ฅ
45
  return {"language": lang, "transcription": data["transcription"]}
46
  else:
47
- return {"error": "์ž๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
48
- except Exception as e:
49
- return {"error": f"์‘๋‹ต ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"}
50
-
 
 
 
51
  # ์ž๋ง‰์„ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
 
52
  return {"error": "์šฐ์„ ์ˆœ์œ„ ์–ธ์–ด๋กœ ์ž๋ง‰์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
53
 
54
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
@@ -56,7 +75,7 @@ def youtube_transcript_interface(youtube_url):
56
  # ์ž๋ง‰ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
57
  transcript_data = get_youtube_transcript(youtube_url)
58
 
59
- # JSON ํฌ๋งท์œผ๋กœ ์ถœ๋ ฅ
60
  return json.dumps(transcript_data, ensure_ascii=False, indent=2)
61
 
62
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
 
10
 
11
  # ์œ ํŠœ๋ธŒ URL์—์„œ ๋น„๋””์˜ค ID๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
12
  def get_video_id(youtube_url):
13
+ # ์œ ํŠœ๋ธŒ URL์—์„œ video_id ์ถ”์ถœ (์ผ๋ฐ˜์ ์ธ YouTube URL ๋˜๋Š” youtu.be ๋‹จ์ถ• URL ์ง€์›)
14
  video_id_match = re.search(r"(?<=v=)[^#&?]*", youtube_url) or re.search(r"(?<=youtu.be/)[^#&?]*", youtube_url)
15
  return video_id_match.group(0) if video_id_match else None
16
 
17
  # ์ž๋ง‰ ์–ธ์–ด ์šฐ์„ ์ˆœ์œ„ ๋ฆฌ์ŠคํŠธ
18
  LANGUAGE_PRIORITY = ['ko', 'en', 'ja', 'zh']
19
 
20
+ # ์œ ํŠœ๋ธŒ ์ž๋ง‰์„ ์š”์ฒญํ•˜๋Š” ํ•จ์ˆ˜ (์–ธ์–ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ ์šฉํ•˜์—ฌ ์‹œ๋„)
21
  def get_youtube_transcript(youtube_url):
22
  # ๋น„๋””์˜ค ID ์ถ”์ถœ
23
  video_id = get_video_id(youtube_url)
24
  if video_id is None:
25
+ print("ERROR: ์ž˜๋ชป๋œ ์œ ํŠœ๋ธŒ URL์ž…๋‹ˆ๋‹ค. ๋น„๋””์˜ค ID๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
26
  return {"error": "์ž˜๋ชป๋œ ์œ ํŠœ๋ธŒ URL์ž…๋‹ˆ๋‹ค. ๋น„๋””์˜ค ID๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
27
 
28
  url = "https://youtube-transcriptor.p.rapidapi.com/transcript"
 
35
  # ์–ธ์–ด ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ์š”์ฒญ์„ ์‹œ๋„
36
  for lang in LANGUAGE_PRIORITY:
37
  querystring = {"video_id": video_id, "lang": lang}
38
+ print(f"์š”์ฒญ ์ค‘: {lang} ์–ธ์–ด์— ๋Œ€ํ•œ ์ž๋ง‰ ์š”์ฒญ")
39
+
40
  response = requests.get(url, headers=headers, params=querystring)
41
 
42
+ # ์ƒํƒœ ์ฝ”๋“œ ์ถœ๋ ฅ
43
+ print(f"Response status code: {response.status_code}")
44
+
45
+ # ์›์‹œ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅ
46
+ print("Raw response content:", response.text)
47
+
48
+ # ์ƒํƒœ ์ฝ”๋“œ๊ฐ€ 200์ผ ๋•Œ๋งŒ ์ฒ˜๋ฆฌ
49
  if response.status_code == 200:
50
  try:
51
+ # JSON์œผ๋กœ ์‘๋‹ต์„ ํŒŒ์‹ฑ
52
  data = response.json()
53
+
54
+ # ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
55
+ print("Parsed JSON response:", json.dumps(data, indent=2, ensure_ascii=False))
56
+
57
+ # transcription ํ•„๋“œ๋ฅผ ํ†ตํ•ด ์ž๋ง‰ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
58
  if "transcription" in data and isinstance(data["transcription"], list):
59
+ # ์ž๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ JSON ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜
60
  return {"language": lang, "transcription": data["transcription"]}
61
  else:
62
+ print(f"ERROR: ์ž๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ {lang} ์–ธ์–ด๋กœ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
63
+ return {"error": f"{lang} ์–ธ์–ด๋กœ ์ž๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
64
+
65
+ except json.JSONDecodeError as e:
66
+ print(f"ERROR: JSON ๋””์ฝ”๋”ฉ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}")
67
+ return {"error": f"JSON ๋””์ฝ”๋”ฉ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"}
68
+
69
  # ์ž๋ง‰์„ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
70
+ print("ERROR: ์šฐ์„ ์ˆœ์œ„ ์–ธ์–ด๋กœ ์ž๋ง‰์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
71
  return {"error": "์šฐ์„ ์ˆœ์œ„ ์–ธ์–ด๋กœ ์ž๋ง‰์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
72
 
73
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
 
75
  # ์ž๋ง‰ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
76
  transcript_data = get_youtube_transcript(youtube_url)
77
 
78
+ # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
79
  return json.dumps(transcript_data, ensure_ascii=False, indent=2)
80
 
81
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ