face1 / app.py
arxivgpt kim
Update app.py
e9b7951 verified
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'''
<video width="640" height="360" controls>
<source src="{download_url}" type="video/mp4">
</video>
<br>
<button onclick="window.location.href='{download_url}'">λ‹€μš΄λ‘œλ“œ</button>
'''
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()