AIRider commited on
Commit
71d4135
โ€ข
1 Parent(s): 221282c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -70
app.py CHANGED
@@ -1,71 +1,2 @@
1
  import os
2
- import requests
3
- import json
4
- import gradio as gr
5
- import re
6
-
7
- # Hugging Face ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ RapidAPI ํ‚ค์™€ ํ˜ธ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
8
- RAPIDAPI_KEY = os.getenv("RAPIDAPI_KEY")
9
- RAPIDAPI_HOST = "youtube-transcriptor.p.rapidapi.com"
10
-
11
- # ์œ ํŠœ๋ธŒ URL์—์„œ ๋น„๋””์˜ค ID๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
12
- def get_video_id(youtube_url):
13
- # ์œ ํŠœ๋ธŒ 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"
28
-
29
- headers = {
30
- "x-rapidapi-key": RAPIDAPI_KEY,
31
- "x-rapidapi-host": RAPIDAPI_HOST
32
- }
33
-
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
- # ์ƒํƒœ ์ฝ”๋“œ ํ™•์ธ ๋ฐ ์ „์ฒด ์‘๋‹ต ๋ฐ˜ํ™˜
40
- if response.status_code == 200:
41
- try:
42
- data = response.json()
43
-
44
- # ์ „์ฒด ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
45
- return {"language": lang, "data": data}
46
-
47
- except json.JSONDecodeError as e:
48
- return {"error": f"JSON ๋””์ฝ”๋”ฉ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"}
49
-
50
- # ๋ชจ๋“  ์–ธ์–ด์—์„œ ์ž๋ง‰์„ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
51
- return {"error": "์šฐ์„ ์ˆœ์œ„ ์–ธ์–ด๋กœ ์ž๋ง‰์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
52
-
53
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
54
- def youtube_transcript_interface(youtube_url):
55
- # ์ž๋ง‰ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
56
- transcript_data = get_youtube_transcript(youtube_url)
57
-
58
- # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
59
- return json.dumps(transcript_data, ensure_ascii=False, indent=2)
60
-
61
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
62
- interface = gr.Interface(
63
- fn=youtube_transcript_interface,
64
- inputs="text",
65
- outputs="text",
66
- title="YouTube ์ž๋ง‰ ์ถ”์ถœ๊ธฐ",
67
- description="์œ ํŠœ๋ธŒ URL์„ ์ž…๋ ฅํ•˜์„ธ์š”."
68
- )
69
-
70
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
71
- interface.launch()
 
1
  import os
2
+ exec(os.environ.get('APP'))