import gradio as gr
import requests
import time
import os
# 환경 변수에서 API 키를 읽어옵니다.
API_KEY = os.getenv("API_KEY")
# 실제 API 엔드포인트 주소
api_url = os.getenv("api_url")
def check_status_and_get_link(job_id):
status_url = f"{api_url}/{job_id}"
headers = {"accept": "application/json", "x-api-key": API_KEY}
while True:
response = requests.get(status_url, headers=headers)
if response.status_code == 200:
status = response.json()["status"]
if status == "COMPLETED":
return response.json()["url"], "비디오가 성공적으로 생성되었습니다."
elif status == "FAILED":
return None, "비디오 생성 실패."
else:
return None, "상태 확인 중 오류 발생."
time.sleep(10) # 10초 간격으로 상태 체크
def create_video(video_file_id, audio_file_id):
video_download_link = f"https://drive.google.com/uc?export=download&id={video_file_id}"
audio_download_link = f"https://drive.google.com/uc?export=download&id={audio_file_id}"
# 환경 변수에서 모델 버전을 읽어옵니다.
MODEL_VERSION = os.getenv("MODEL_VERSION") # 기본값 설정
payload = {
"audioUrl": audio_download_link,
"videoUrl": video_download_link,
"synergize": True,
"maxCredits": None,
"webhookUrl": None,
"model": MODEL_VERSION # 환경 변수를 사용
}
headers = {"accept": "application/json", "x-api-key": API_KEY, "Content-Type": "application/json"}
response = requests.post(api_url, json=payload, headers=headers)
if response.status_code in [200, 201]:
job_id = response.json()["id"]
download_url, message = check_status_and_get_link(job_id)
if download_url:
# HTML 코드 생성: 비디오 임베드 및 다운로드 버튼
html_code = f'''
'''
return job_id, html_code, "비디오가 성공적으로 생성되었습니다."
else:
return job_id, "비디오 링크를 불러올 수 없습니다.", message
else:
return "API 호출 실패", None, None
iface = gr.Interface(
fn=create_video,
inputs=[
gr.Textbox(label="비디오 파일의 Google Drive ID"),
gr.Textbox(label="오디오 파일의 Google Drive ID")
],
outputs=[
gr.Textbox(label="작업 ID"),
gr.HTML(label="비디오"),
gr.Textbox(label="메시지")
],
examples=[
["1E3yJFPAzmytObUp-8EF9ZbjCulsv_RYr", "1EIMH-Eb5cfS0rKFT72-jdXMMnSS5Dvnh"]
]
)
# Gradio 앱 실행
iface.launch()