Spaces:
Runtime error
Runtime error
File size: 5,899 Bytes
47ab4d9 df881b8 47ab4d9 df881b8 47ab4d9 df881b8 47ab4d9 df881b8 47ab4d9 df881b8 47ab4d9 284a6d8 244d581 47ab4d9 244d581 284a6d8 244d581 284a6d8 244d581 284a6d8 47ab4d9 36e0165 10670d6 47ab4d9 36e0165 47ab4d9 10670d6 47ab4d9 8b7f166 47ab4d9 10670d6 47ab4d9 36e0165 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
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()
|