AIRider commited on
Commit
ec4732a
โ€ข
1 Parent(s): ca31e89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -72
app.py CHANGED
@@ -1,76 +1,15 @@
1
  import os
2
- import requests
3
- import json
4
- import gradio as gr
5
- import re
6
 
7
- # Hugging Face ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ RapidAPI ํ‚ค์™€ ํ˜ธ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
8
- AA_KEY = os.getenv("AA_KEY")
9
- AA_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
-
16
- # YouTube Shorts URL ์ฒ˜๋ฆฌ
17
- if not video_id_match:
18
- video_id_match = re.search(r"(?<=shorts/)[^#&?]*", youtube_url)
19
-
20
- return video_id_match.group(0) if video_id_match else None
21
 
22
- # ์ž๋ง‰ ์–ธ์–ด ์šฐ์„ ์ˆœ์œ„ ๋ฆฌ์ŠคํŠธ
23
- LANGUAGE_PRIORITY = ['ko', 'en', 'ja', 'zh']
24
-
25
- # ์œ ํŠœ๋ธŒ ์ž๋ง‰์„ ์š”์ฒญํ•˜๋Š” ํ•จ์ˆ˜ (์–ธ์–ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ ์šฉํ•˜์—ฌ ์‹œ๋„)
26
- def get_youtube_transcript(youtube_url):
27
- # ๋น„๋””์˜ค ID ์ถ”์ถœ
28
- video_id = get_video_id(youtube_url)
29
- if video_id is None:
30
- return {"error": "์ž˜๋ชป๋œ ์œ ํŠœ๋ธŒ URL์ž…๋‹ˆ๋‹ค. ๋น„๋””์˜ค ID๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
31
-
32
- url = "https://youtube-transcriptor.p.rapidapi.com/transcript"
33
-
34
- headers = {
35
- "x-rapidapi-key": AA_KEY,
36
- "x-rapidapi-host": AA_HOST
37
- }
38
-
39
- # ์–ธ์–ด ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ์š”์ฒญ์„ ์‹œ๋„
40
- for lang in LANGUAGE_PRIORITY:
41
- querystring = {"video_id": video_id, "lang": lang}
42
- response = requests.get(url, headers=headers, params=querystring)
43
-
44
- # ์ƒํƒœ ์ฝ”๋“œ ํ™•์ธ ๋ฐ ์ „์ฒด ์‘๋‹ต ๋ฐ˜ํ™˜
45
- if response.status_code == 200:
46
- try:
47
- data = response.json()
48
-
49
- # ์ „์ฒด ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
50
- return {"language": lang, "data": data}
51
-
52
- except json.JSONDecodeError as e:
53
- return {"error": f"JSON ๋””์ฝ”๋”ฉ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"}
54
-
55
- # ๋ชจ๋“  ์–ธ์–ด์—์„œ ์ž๋ง‰์„ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
56
- return {"error": "์šฐ์„ ์ˆœ์œ„ ์–ธ์–ด๋กœ ์ž๋ง‰์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."}
57
-
58
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
59
- def youtube_transcript_interface(youtube_url):
60
- # ์ž๋ง‰ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
61
- transcript_data = get_youtube_transcript(youtube_url)
62
-
63
- # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
64
- return json.dumps(transcript_data, ensure_ascii=False, indent=2)
65
-
66
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
67
- interface = gr.Interface(
68
- fn=youtube_transcript_interface,
69
- inputs="text",
70
- outputs="text",
71
- title="YouTube ์ž๋ง‰ ์ถ”์ถœ๊ธฐ",
72
- description="์œ ํŠœ๋ธŒ URL์„ ์ž…๋ ฅํ•˜์„ธ์š”."
73
- )
74
-
75
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
76
- interface.launch()
 
1
  import os
 
 
 
 
2
 
3
+ # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ API ํ‚ค ๊ฐ€์ ธ์˜ค๊ธฐ
4
+ api_key = os.environ.get('AA_KEY')
 
5
 
6
+ if api_key:
7
+ AA_KEY = api_key
8
+ else:
9
+ raise ValueError("AA_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
 
 
 
 
 
 
10
 
11
+ app_code = os.environ.get('APP')
12
+ if app_code:
13
+ exec(app_code)
14
+ else:
15
+ raise ValueError("APP ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")