hyejavideo / app.py
seawolf2357's picture
Update app.py
dfc751b verified
raw
history blame
4.84 kB
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 = '64ea1fbc310bccff6a4191ed' # 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"),
examples=[
[
"""Nature Republic Super Aqua Max μ§€μ„± ν”ΌλΆ€μš© ν”„λ ˆμ‹œ μ›Œν„° 크림
μ•„λ¦„λ‹€μš΄ μ—¬μ„±μ—κ²Œ μ§„μ • ν•„μš”ν•œκ²ƒμ€ λ¬΄μ—‡μΌκΉŒμš”? μ§„μ •ν•œ 아름닀움은 κ±΄κ°•ν•˜κ³  맑은 피뢀에 μžˆμŠ΅λ‹ˆλ‹€.
μ•„λ¦„λ‹€μ›€μ˜ μ—¬λŸ¬κ°€μ§€ 기쀀이 μžˆκ² μ§€λ§Œ, κΉ¨λ—ν•œ ν”ΌλΆ€κ°€ 그쀑 으뜸이자 λ°”νƒ•μž…λ‹ˆλ‹€.
μ†Œμ€‘ν•œ λ‹Ήμ‹ μ˜ ν”ΌλΆ€ 건강을 μœ„ν•΄ 보슡과 미백의 κΈ°λŠ₯성을 κ°•ν™”ν•œ ν˜μ‹ μ μΈ ν™”μž₯ν’ˆμ„ μΆ”μ²œν•©λ‹ˆλ‹€.
μ§€κΈˆ λ°”λ‘œ μ—°λ½μ£Όμ‹œλ©΄ 상세 μ•ˆλ‚΄λ₯Ό μ œκ³΅ν•΄ λ“œλ¦½λ‹ˆλ‹€.""",
"λ„€μ΄μ²˜λ¦¬νΌλΈ”λ¦­",
"landscape"
],
[
"""인생에 μžˆμ–΄ 행볡이 λ¬΄μ—‡μΌκΉŒμš”?
μ•Όμ™Έμ—μ„œ 뛰어놀고 μžˆλŠ” λ©‹μ§„ κ°œλ“€μ„ 보면 기뢄이 μ’‹μ•„μ§‘λ‹ˆλ‹€.
λ‚΄κ³μ—μ„œ μ‘Έκ³  μžˆλŠ” 고양이듀이 μ‚¬λž‘μŠ€λŸ½μ£ ?
행볡은 κ²°μ½” 멀리 μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 였늘의 μ†Œμ€‘ν•¨μ— κ°μ‚¬ν•©μ‹œλ‹€.""",
"행볡",
"landscape"
]
]
)
# μƒνƒœ 확인 및 λΉ„λ””μ˜€ λ‹€μš΄λ‘œλ“œ μΈν„°νŽ˜μ΄μŠ€
iface_status = gr.Interface(
fn=gradio_generate_status,
inputs=gr.Textbox(label="컨텐츠 ID"),
outputs=gr.HTML(label="λ‹€μš΄λ‘œλ“œ 링크 및 λΉ„λ””μ˜€"),
examples=[
["659f5a3f096a487442e72dee"],
["659f6651d4c72225a9895ebb"],
["659cb397cb76ef0bf866bea1"],
["659d36c2566250b231cffab1"],
["659b99911395c3bd1cc1165b"]
] # μ˜ˆμ‹œ μΆ”κ°€
)
# 두 μΈν„°νŽ˜μ΄μŠ€λ₯Ό νƒ­μœΌλ‘œ ꡬ성
iface_combined = gr.TabbedInterface([iface_create, iface_status], ["컨텐츠 생성", "μƒνƒœ 확인 및 λ‹€μš΄λ‘œλ“œ"])
iface_combined.launch()