hyejavideo / app.py
seawolf2357's picture
Update app.py
baf199d
raw
history blame
5.58 kB
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()