Spaces:
Build error
Build 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 | |
payload = { | |
"format": "video", # video | audio | |
"scenes": [ | |
{ | |
"content": user_content, # μ¬μ©μκ° μ λ ₯ν ν μ€νΈ | |
"voiceId": VOICE_ID | |
}, | |
# ... λ€λ₯Έ μ¬λ€μ΄ νμνλ€λ©΄ μ¬κΈ°μ μΆκ° ... | |
], | |
"settings": { | |
'aspectRatio': 'portrait', # 'portrait', 'square', 'horizontal' | |
'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() | |
# 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() | |
response = requests.request("POST", url, json=payload, headers=headers).json() | |
# HTTP μνμ½λ νμΈ | |
if response['success'] == True: | |
print(f"μ μ: {response}") | |
if response['data']['status'] == 'queued': | |
print('μμ§ μ»¨ν μΈ URLμ΄ μμ±λμ§ μμμ΅λλ€.') | |
return 'PENDING' | |
if response['data']['status'] == 'processing': | |
print('컨ν μΈ URLμ΄ μμ§ μμ± μ€ μ λλ€.') | |
return 'PENDING' | |
elif response['data']['status'] == 'success': | |
return response['data']['file'] | |
else: | |
print(f"request μ€ν¨") | |
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}") | |
# Gradio μΈν°νμ΄μ€λ₯Ό μν ν¨μ | |
def gradio_generate_contents(user_content, brand_name): | |
return generate_contents(user_content, brand_name) # λ μ λ ₯ λͺ¨λ μ λ¬ | |
# Gradio μΈν°νμ΄μ€ μμ | |
iface = gr.Interface( | |
fn=gradio_generate_contents, # 컨ν μΈ μμ± ν¨μ | |
inputs=[gr.inputs.Textbox(label="컨ν μΈ ν μ€νΈ"), gr.inputs.Textbox(label="λΈλλλͺ ")], | |
outputs=gr.outputs.Textbox(label="컨ν μΈ ID") | |
) | |
status_iface = gr.Interface( | |
fn=gradio_generate_status, # 컨ν μΈ μν νμΈ ν¨μ | |
inputs=gr.inputs.Textbox(label="컨ν μΈ ID"), | |
outputs=gr.outputs.Textbox(label="컨ν μΈ μν λλ URL") | |
) | |
iface.launch() | |
status_iface.launch() | |