import gradio as gr
import requests
import time
import os
import tempfile
import shutil
# 환경 변수에서 API 키와 API 엔드포인트 주소를 읽어옵니다.
API_KEY = os.getenv("API_KEY")
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_data, audio_data):
files = {
'video': ('video.mp4', video_data, 'video/mp4'),
'audio': ('audio.mp3', audio_data, 'audio/mpeg'),
}
payload = {
"synergize": True,
"model": os.getenv("MODEL_VERSION")
}
headers = {"x-api-key": API_KEY}
try:
response = requests.post(api_url, files=files, data=payload, headers=headers)
print(f"Response: {response.status_code}, {response.text}") # 응답 상태 코드와 내용 로깅
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_code = f'''
'''
return job_id, html_code, message
else:
return job_id, "비디오 링크를 불러올 수 없습니다.", message
else:
print(f"API 호출 실패: {response.status_code}, {response.text}")
return "API 호출 실패", None, None
except Exception as e:
print(f"API 호출 중 예외 발생: {e}")
return "API 호출 중 오류 발생", None, None
iface = gr.Interface(
fn=create_video,
inputs=[
gr.File(label="비디오 파일 업로드", type="binary"),
gr.File(label="오디오 파일 업로드", type="binary")
],
outputs=[
gr.Textbox(label="작업 ID"),
gr.HTML(label="비디오"),
gr.Textbox(label="메시지")
],
title="혜자 페이스",
description="mp4 비디오 파일과 오디오 파일을 업로드하여 비디오를 생성합니다."
)
iface.launch()