File size: 5,584 Bytes
aa95dd5
bc18a34
0af8481
993c6c8
0af8481
 
 
 
 
 
 
 
 
 
 
 
 
5206fbf
0af8481
 
 
 
 
 
 
 
 
5206fbf
0af8481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
baf199d
 
 
0af8481
 
aab1d42
0af8481
 
 
 
aab1d42
 
0af8481
baf199d
 
0af8481
 
 
 
 
 
 
 
 
 
 
baf199d
0af8481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd532eb
0af8481
baf199d
 
0af8481
baf199d
3c02b31
0af8481
baf199d
0af8481
 
 
baf199d
0af8481
 
 
 
3c02b31
bc18a34
3c02b31
0af8481
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
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()