File size: 2,862 Bytes
bcc6031 1d106f7 90e7004 8062da3 e9b7951 90e7004 4237e03 b32565c 7677dbe 06131b3 15f4f22 bcc6031 e9b7951 15f4f22 e9b7951 15f4f22 06131b3 e9b7951 06131b3 adf1016 06131b3 15f4f22 bcc6031 abfd02c e9b7951 abfd02c e9b7951 90e7004 bcc6031 06131b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 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() |