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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -41
app.py CHANGED
@@ -1,49 +1,71 @@
1
  import os
2
- import gradio as gr
3
- import urllib.request
4
- import urllib.parse
5
  import json
 
 
 
 
 
 
6
 
7
- # ๋„ค์ด๋ฒ„ API ์ธ์ฆ ์ •๋ณด๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค
8
- CLIENT_ID = os.getenv("NAVER_CLIENT_ID")
9
- CLIENT_SECRET = os.getenv("NAVER_CLIENT_SECRET")
 
 
10
 
11
- def search_naver_blog(query, display=10):
12
- encText = urllib.parse.quote(query)
13
- url = f"https://openapi.naver.com/v1/search/blog?query={encText}&display={display}"
 
 
 
 
 
 
 
 
14
 
15
- request = urllib.request.Request(url)
16
- request.add_header("X-Naver-Client-Id", CLIENT_ID)
17
- request.add_header("X-Naver-Client-Secret", CLIENT_SECRET)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- try:
20
- response = urllib.request.urlopen(request)
21
- rescode = response.getcode()
22
- if rescode == 200:
23
- response_body = response.read()
24
- result = json.loads(response_body.decode('utf-8'))
25
- return json.dumps(result, ensure_ascii=False, indent=2)
26
- else:
27
- return f"Error Code: {rescode}"
28
- except urllib.error.HTTPError as e:
29
- return f"HTTP Error: {e.code} - {e.reason}"
30
- except urllib.error.URLError as e:
31
- return f"URL Error: {e.reason}"
32
-
33
- def gradio_interface(query, num_results):
34
- if not CLIENT_ID or not CLIENT_SECRET:
35
- return "Error: Naver API credentials are not set. Please check your environment variables."
36
- return search_naver_blog(query, display=num_results)
37
-
38
- iface = gr.Interface(
39
- fn=gradio_interface,
40
- inputs=[
41
- gr.Textbox(label="๊ฒ€์ƒ‰์–ด"),
42
- gr.Slider(minimum=1, maximum=100, step=1, label="๊ฒฐ๊ณผ ์ˆ˜", value=10)
43
- ],
44
- outputs="json",
45
- title="๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ๊ฒ€์ƒ‰",
46
- description="๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ๊ฒ€์ƒ‰ API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค."
47
  )
48
 
49
- iface.launch()
 
 
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()