Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import time | |
API_HOME = 'https://api.fliki.ai/v1' | |
END_LANGUAGES = '/languages' | |
END_DIALECTS = '/dialects' | |
END_VOICES = '/voices' | |
END_GENERATE = '/generate' | |
END_STATUS = '/generate/status' | |
API_KEY = 'ZZUIQ4OZASNRQ8B8WYHNW' | |
brand = 'ํ์ธ์ ํ ๋ ธํธ๋ถ' | |
def set_header(): | |
header = { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer " + API_KEY, | |
} | |
return header | |
# ๊ธฐ์กด์ ํจ์๋ค... | |
def set_header(): | |
header = { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer " + API_KEY, | |
} | |
return header | |
# ์ธ์ด ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์. | |
def get_language_id(): | |
url = API_HOME + END_LANGUAGES | |
headers = set_header() | |
response = requests.request("GET", url, headers=headers).json() | |
print(f"์ธ์ด ๋ฆฌ์คํธ: {response}") | |
# Korean: 61b8b2fa4268666c126babf1 | |
return response | |
# ์ธ์ด์ง์ญ ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์. | |
def get_dialects(): | |
url = API_HOME + END_DIALECTS | |
headers = set_header() | |
response = requests.request("GET", url, headers=headers).json() | |
print(f"์ธ์ด์ง์ญ ๋ฆฌ์คํธ: {response}") | |
# Korea: 61b8b30f4268666c126bac8d | |
return response | |
# ์ง์ญ์ธ์ด ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์. | |
def get_voices(language_id, dialect_id): | |
url = API_HOME + END_VOICES | |
headers = set_header() | |
payload = { | |
'languageId': language_id, | |
'dialectId': dialect_id | |
} | |
response = requests.request("POST", url, json=payload, headers=headers).json() | |
print(f"voice: {response}") | |
return response | |
# ๋น๋์ค ๋๋ ์ค๋์ค ์ปจํ ์ธ ์์ฑํ๋ ํจ์. | |
def generate_contents(user_content, brand_name): | |
global brand | |
brand = brand_name # ๋ธ๋๋๋ช ์ค์ | |
url = API_HOME + END_GENERATE | |
VOICE_ID = '65934ac2bc02ab1c006755fa' # Korean, Korea, Wolf Sea | |
# ๊ฐ ๋ฌธ์ฅ์ ๋ณ๋์ scene์ผ๋ก ์ฒ๋ฆฌ | |
scenes = [{"content": line, "voiceId": VOICE_ID} for line in user_content.split('\n') if line.strip()] | |
payload = { | |
"format": "video", # video | audio | |
"scenes": scenes, | |
"settings": { | |
# ... ์ค์ ๋ค ... | |
}, | |
"backgroundMusicKeywords": "happy, lofi, beats" | |
} | |
# ... ๋๋จธ์ง ์ฝ๋ ... | |
headers = set_header() | |
response = requests.request("POST", url, json=payload, headers=headers).json() | |
# HTTP ์ํ์ฝ๋ ํ์ธ | |
if response['success'] == True: | |
# ์๋ต๊ฐ ์์: {'success': True, 'data': {'id': '6593aa11d153762d8aa022e9'}} | |
print(f"์ ์: {response['data']['id']}") | |
return response['data']['id'] | |
else: | |
print(f"request ์คํจ: {response}") | |
return 'FAILED' | |
# /generate API ํธ์ถ ์ฑ๊ณต ํ ๋ฐ์ id๋ก ์ปจํ ์ธ ์์ฑ ์ํ๋ฅผ ํ์ธํ๋ ํจ์. | |
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']: | |
if response['data']['status'] == 'queued' or response['data']['status'] == 'processing': | |
print('์ปจํ ์ธ ์์ฑ ์ค... ์ ์ ํ ๋ค์ ์๋ํ์ธ์.') | |
time.sleep(10) # 10์ด ๋๊ธฐ | |
elif response['data']['status'] == 'success': | |
print('์ปจํ ์ธ ์์ฑ ์๋ฃ!') | |
return response['data']['file'] | |
else: | |
print(f"request ์คํจ: {response}") | |
break | |
return 'FAILED' | |
def gradio_generate_status(content_id): | |
download_url = generate_status(content_id) | |
if download_url != 'FAILED': | |
return download_url, requests.get(download_url).content | |
else: | |
return "์ปจํ ์ธ ์์ฑ ์คํจ ๋๋ ID๊ฐ ์๋ชป๋์์ต๋๋ค.", None | |
status_iface = gr.Interface( | |
fn=gradio_generate_status, | |
inputs=gr.Textbox(label="์ปจํ ์ธ ID"), | |
outputs=[ | |
gr.Textbox(label="๋ค์ด๋ก๋ URL"), | |
gr.File(label="๋ค์ด๋ก๋ ๋ฒํผ") | |
] | |
) | |
status_iface.launch() | |
def download_content(url): | |
data = requests.get(url, timeout=30) | |
save_path = "downloaded_example.mp4" | |
with open(save_path, 'wb') as file: | |
for chunk in data.iter_content(chunk_size=128): | |
file.write(chunk) | |
print(f"MP4 ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ์ ์ฅ๋์์ต๋๋ค. ๊ฒฝ๋ก: {save_path}") | |
return 'success' | |
# # ์คํ. | |
# get_language_id() # Korean: 61b8b2fa4268666c126babf1 | |
# get_dialects() # Korea: 61b8b30f4268666c126bac8d | |
# get_voices('61b8b2fa4268666c126babf1', '61b8b30f4268666c126bac8d') | |
try: | |
id = generate_contents() | |
if id != 'FAILED': | |
status = 'PENDING' | |
while status == 'PENDING': | |
time.sleep(9) # 9์ด ๊ธฐ๋ค๋ฆผ | |
print('9์ด ๋๊ธฐ ํ ์ํํ์ธ ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.') | |
status = generate_status(id) | |
if status != 'PENDING': break | |
print(f"์ปจํ ์ธ ์์ฑ ์ฑ๊ณต: {status}") | |
else: | |
print('์์ฑ ์คํจ..') | |
except Exception as e: | |
print(f"์ค๋ฅ ๋ฐ์: {e}") | |
def gradio_generate_contents(user_content, brand_name): | |
return generate_contents(user_content, brand_name) | |
def gradio_generate_status(content_id): | |
return generate_status(content_id) | |
# ... ์ด์ ์ฝ๋ ... | |
# Gradio ์ธํฐํ์ด์ค ์์ | |
iface = gr.Interface( | |
fn=gradio_generate_contents, # ์ปจํ ์ธ ์์ฑ ํจ์ | |
inputs=[gr.TextArea(label="์ปจํ ์ธ ํ ์คํธ"), gr.Textbox(label="๋ธ๋๋๋ช ")], # ๋ฉํฐ๋ผ์ธ ํ ์คํธ ์ ๋ ฅ ๋ฐ ๋ธ๋๋๋ช ์ ๋ ฅ | |
outputs=gr.Textbox(label="์ปจํ ์ธ ID") | |
) | |
status_iface = gr.Interface( | |
fn=gradio_generate_status, # ์ปจํ ์ธ ์ํ ํ์ธ ํจ์ | |
inputs=gr.Textbox(label="์ปจํ ์ธ ID"), | |
outputs=gr.Textbox(label="์ปจํ ์ธ ์ํ ๋๋ URL") | |
) | |
# ์ธํฐํ์ด์ค ์คํ | |
iface.launch() | |
status_iface.launch() | |