Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,71 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import urllib.request
|
| 4 |
-
import urllib.parse
|
| 5 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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 |
-
|
|
|
|
|
|
| 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()
|