Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import time | |
import os | |
API_HOME = os.environ['url'] | |
END_GENERATE = '/generate' | |
END_STATUS = '/generate/status' | |
API_KEY = os.environ['api'] | |
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): | |
content_id = generate_contents(user_content, brand_name, aspect_ratio) | |
markdown_text = "λΉλμ€ μ€ν¬λ¦½νΈ μλ μμ± ν΄λ¦: [μ¬κΈ°λ₯Ό ν΄λ¦νμΈμ](https://seawolf2357-hyejascript.hf.space)" | |
return content_id, markdown_text | |
def gradio_generate_status(content_id): | |
download_url = generate_status(content_id) | |
markdown_text = "μλ μμ±λ μμμ λν μμ λ° νΈμ§ μμ² ν΄λ¦: [μ¬κΈ°λ₯Ό ν΄λ¦νμΈμ](https://forms.gle/u9hPDFbntLEp8xH6A)" | |
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, markdown_text | |
else: | |
return "컨ν μΈ μμ± μ€ν¨ λλ IDκ° μλͺ»λμμ΅λλ€.", markdown_text | |
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"), | |
gr.Markdown() # λ§ν¬λ€μ΄ μΆλ ₯ μΆκ° | |
], | |
examples=[ | |
[ | |
"Nature Republic Super Aqua Max μ§μ± νΌλΆμ© νλ μ μν° ν¬λ¦Ό\nμλ¦λ€μ΄ μ¬μ±μκ² μ§μ νμνκ²μ 무μμΌκΉμ? μ§μ ν μλ¦λ€μμ 건κ°νκ³ λ§μ νΌλΆμ μμ΅λλ€.", | |
"λ€μ΄μ²λ¦¬νΌλΈλ¦", | |
"landscape" | |
], | |
[ | |
"μΈμμ μμ΄ νλ³΅μ΄ λ¬΄μμΌκΉμ?\nμΌμΈμμ λ°μ΄λκ³ μλ λ©μ§ κ°λ€μ 보면 κΈ°λΆμ΄ μ’μμ§λλ€.\nλ΄κ³μμ μ‘Έκ³ μλ κ³ μμ΄λ€μ΄ μ¬λμ€λ½μ£ ?", | |
"ν볡", | |
"landscape" | |
] | |
] | |
) | |
iface_status = gr.Interface( | |
fn=gradio_generate_status, | |
inputs=gr.Textbox(label="컨ν μΈ ID"), | |
outputs=[ | |
gr.HTML(label="λ€μ΄λ‘λ λ§ν¬ λ° λΉλμ€"), | |
gr.Markdown("μλ μμ±λ μμμ λν μμ λ° νΈμ§ μμ² ν΄λ¦: [μ¬κΈ°λ₯Ό ν΄λ¦νμΈμ](https://forms.gle/u9hPDFbntLEp8xH6A)") | |
] | |
) | |
iface_combined = gr.TabbedInterface([iface_create, iface_status], ["컨ν μΈ μμ±", "μν νμΈ λ° λ€μ΄λ‘λ"]) | |
iface_combined.launch(auth=("arxiv", "gpt")) | |