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()