Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import time | |
API_HOME = 'https://api.fliki.ai/v1' | |
END_GENERATE = '/generate' | |
END_STATUS = '/generate/status' | |
API_KEY = 'ZZUIQ4OZASNRQ8B8WYHNW' | |
def set_header(): | |
header = { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer " + API_KEY, | |
} | |
return header | |
# ๋น๋์ค ๋๋ ์ค๋์ค ์ปจํ ์ธ ์์ฑํ๋ ํจ์ | |
def generate_contents(user_content, brand_name, aspect_ratio='landscape'): | |
url = API_HOME + END_GENERATE | |
VOICE_ID = '64ea14306a00ac991dcb8a3f' # Korean, Korea, Wolf Sea | |
# ๋ฉํฐ ๋ผ์ธ ํ ์คํธ๋ฅผ ๋ถ๋ฆฌํ์ฌ ๊ฐ ์ค์ ๋ณ๋์ ์ฌ์ผ๋ก ์ฒ๋ฆฌ | |
scenes = [{"content": line.strip(), "voiceId": VOICE_ID} for line in user_content.split('\n') if line.strip()] | |
payload = { | |
"format": "video", # video | audio | |
"scenes": scenes, | |
"settings": { | |
'aspectRatio': aspect_ratio, | |
'subtitle': { | |
'fontColor': 'yellow', | |
'backgroundColor': 'black', | |
'placement': 'bottom', | |
'display': 'phrase', | |
}, | |
}, | |
"backgroundMusicKeywords": "happy, lofi, beats" | |
} | |
headers = set_header() | |
response = requests.request("POST", url, json=payload, headers=headers).json() | |
if response['success']: | |
return response['data']['id'] | |
else: | |
return 'FAILED' | |
# ์ปจํ ์ธ ์์ฑ ์ํ๋ฅผ ํ์ธํ๋ ํจ์ | |
def generate_status(content_id): | |
url = API_HOME + END_STATUS | |
payload = {"id": content_id} | |
headers = set_header() | |
while True: | |
response = requests.request("POST", url, json=payload, headers=headers).json() | |
if response['success']: | |
status = response['data']['status'] | |
if status == 'queued' or status == 'processing': | |
time.sleep(10) # 10์ด ๋๊ธฐ | |
elif status == 'success': | |
return response['data']['file'] | |
else: | |
break | |
return 'FAILED' | |
# Gradio ์ธํฐํ์ด์ค ํจ์ | |
def gradio_generate_contents(user_content, brand_name, aspect_ratio): | |
return generate_contents(user_content, brand_name, aspect_ratio) | |
def gradio_generate_status(content_id): | |
download_url = generate_status(content_id) | |
if download_url != 'FAILED': | |
display_text = download_url[:10] + '...' if len(download_url) > 10 else download_url | |
html_link = f'<a href="{download_url}" target="_blank">{display_text}</a>' | |
html_video = f'<iframe src="{download_url}" width="640" height="360" frameborder="0" allowfullscreen></iframe>' | |
return html_link + '<br>' + html_video | |
else: | |
return "์ปจํ ์ธ ์์ฑ ์คํจ ๋๋ ID๊ฐ ์๋ชป๋์์ต๋๋ค." | |
# ์ปจํ ์ธ ์์ฑ ์ธํฐํ์ด์ค | |
iface_create = gr.Interface( | |
fn=gradio_generate_contents, | |
inputs=[ | |
gr.TextArea(label="์ปจํ ์ธ ํ ์คํธ"), | |
gr.Textbox(label="๋ธ๋๋๋ช "), | |
gr.Dropdown(label="ํ๋ฉด ๋น์จ", choices=['portrait', 'square', 'landscape'], value='landscape') | |
], | |
outputs=gr.Textbox(label="์ปจํ ์ธ ID") | |
) | |
# ์ํ ํ์ธ ๋ฐ ๋น๋์ค ๋ค์ด๋ก๋ ์ธํฐํ์ด์ค | |
iface_status = gr.Interface( | |
fn=gradio_generate_status, | |
inputs=gr.Textbox(label="์ปจํ ์ธ ID"), | |
outputs=gr.HTML(label="๋ค์ด๋ก๋ ๋งํฌ ๋ฐ ๋น๋์ค") | |
) | |
# ๋ ์ธํฐํ์ด์ค๋ฅผ ํญ์ผ๋ก ๊ตฌ์ฑ | |
iface_combined = gr.TabbedInterface([iface_create, iface_status], ["์ปจํ ์ธ ์์ฑ", "์ํ ํ์ธ ๋ฐ ๋ค์ด๋ก๋"]) | |
iface_combined.launch() | |