File size: 3,516 Bytes
47ab4d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a686c9
 
47ab4d9
5a686c9
47ab4d9
5a686c9
 
df881b8
47ab4d9
 
df881b8
47ab4d9
5a686c9
 
 
 
 
 
 
47ab4d9
 
 
 
 
 
 
433ca1f
47ab4d9
 
 
 
5a686c9
47ab4d9
 
 
 
 
284a6d8
 
 
 
433ca1f
 
284a6d8
433ca1f
284a6d8
 
 
 
 
433ca1f
5a686c9
 
 
284a6d8
 
 
1c97435
e2c5877
73ef68b
25e5468
47ab4d9
d49d212
284a6d8
5a686c9
 
 
 
 
 
 
 
 
 
 
 
 
244d581
284a6d8
25e5468
284a6d8
 
5a686c9
 
 
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
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 = '64ea14306a00ac991dcb8a3f'  # 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")
)

# ์ƒํƒœ ํ™•์ธ ๋ฐ ๋น„๋””์˜ค ๋‹ค์šด๋กœ๋“œ ์ธํ„ฐํŽ˜์ด์Šค
iface_status = gr.Interface(
    fn=gradio_generate_status,
    inputs=gr.Textbox(label="์ปจํ…์ธ  ID"),
    outputs=gr.HTML(label="๋‹ค์šด๋กœ๋“œ ๋งํฌ ๋ฐ ๋น„๋””์˜ค")
)

# ๋‘ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํƒญ์œผ๋กœ ๊ตฌ์„ฑ
iface_combined = gr.TabbedInterface([iface_create, iface_status], ["์ปจํ…์ธ  ์ƒ์„ฑ", "์ƒํƒœ ํ™•์ธ ๋ฐ ๋‹ค์šด๋กœ๋“œ"])
iface_combined.launch()