Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
#
|
| 8 |
-
|
| 9 |
-
AA_HOST = "youtube-transcriptor.p.rapidapi.com"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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 환경 변수가 설정되지 않았습니다.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|