seawolf2357 commited on
Commit
47ab4d9
ยท
1 Parent(s): 3e9646a

Upload app (11).py

Browse files
Files changed (1) hide show
  1. app (11).py +180 -0
app (11).py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import time
4
+
5
+ API_HOME = 'https://api.fliki.ai/v1'
6
+ END_LANGUAGES = '/languages'
7
+ END_DIALECTS = '/dialects'
8
+ END_VOICES = '/voices'
9
+ END_GENERATE = '/generate'
10
+ END_STATUS = '/generate/status'
11
+ API_KEY = 'ZZUIQ4OZASNRQ8B8WYHNW'
12
+ brand = 'ํŒŒ์ธ์• ํ”Œ ๋…ธํŠธ๋ถ'
13
+
14
+ def set_header():
15
+ header = {
16
+ "Content-Type": "application/json",
17
+ "Authorization": "Bearer " + API_KEY,
18
+ }
19
+ return header
20
+
21
+ # ๊ธฐ์กด์˜ ํ•จ์ˆ˜๋“ค...
22
+
23
+
24
+ def set_header():
25
+ header = {
26
+ "Content-Type": "application/json",
27
+ "Authorization": "Bearer " + API_KEY,
28
+ }
29
+ return header
30
+
31
+ # ์–ธ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜.
32
+ def get_language_id():
33
+ url = API_HOME + END_LANGUAGES
34
+ headers = set_header()
35
+ response = requests.request("GET", url, headers=headers).json()
36
+ print(f"์–ธ์–ด ๋ฆฌ์ŠคํŠธ: {response}")
37
+ # Korean: 61b8b2fa4268666c126babf1
38
+ return response
39
+
40
+
41
+ # ์–ธ์–ด์ง€์—ญ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜.
42
+ def get_dialects():
43
+ url = API_HOME + END_DIALECTS
44
+ headers = set_header()
45
+ response = requests.request("GET", url, headers=headers).json()
46
+ print(f"์–ธ์–ด์ง€์—ญ ๋ฆฌ์ŠคํŠธ: {response}")
47
+ # Korea: 61b8b30f4268666c126bac8d
48
+ return response
49
+
50
+
51
+ # ์ง€์—ญ์–ธ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜.
52
+ def get_voices(language_id, dialect_id):
53
+ url = API_HOME + END_VOICES
54
+ headers = set_header()
55
+ payload = {
56
+ 'languageId': language_id,
57
+ 'dialectId': dialect_id
58
+ }
59
+ response = requests.request("POST", url, json=payload, headers=headers).json()
60
+ print(f"voice: {response}")
61
+ return response
62
+
63
+
64
+ # ๋น„๋””์˜ค ๋˜๋Š” ์˜ค๋””์˜ค ์ปจํ…์ธ  ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
65
+ def generate_contents(user_content, brand_name):
66
+ global brand
67
+ brand = brand_name # ๋ธŒ๋žœ๋“œ๋ช… ์„ค์ •
68
+ url = API_HOME + END_GENERATE
69
+ VOICE_ID = '65934ac2bc02ab1c006755fa' # Korean, Korea, Wolf Sea
70
+
71
+ payload = {
72
+ "format": "video", # video | audio
73
+ "scenes": [
74
+ {
75
+ "content": user_content, # ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ
76
+ "voiceId": VOICE_ID
77
+ },
78
+ # ... ๋‹ค๋ฅธ ์”ฌ๋“ค์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์—ฌ๊ธฐ์— ์ถ”๊ฐ€ ...
79
+ ],
80
+ "settings": {
81
+ 'aspectRatio': 'portrait', # 'portrait', 'square', 'horizontal'
82
+ 'subtitle': {
83
+ 'fontColor': 'yellow',
84
+ 'backgroundColor': 'black',
85
+ 'placement': 'bottom',
86
+ 'display': 'phrase',
87
+ },
88
+ },
89
+ "backgroundMusicKeywords": "happy, lofi, beats"
90
+ }
91
+
92
+ headers = set_header()
93
+ response = requests.request("POST", url, json=payload, headers=headers).json()
94
+
95
+ # HTTP ์ƒํƒœ์ฝ”๋“œ ํ™•์ธ
96
+ if response['success'] == True:
97
+ # ์‘๋‹ต๊ฐ’ ์˜ˆ์‹œ: {'success': True, 'data': {'id': '6593aa11d153762d8aa022e9'}}
98
+ print(f"์ •์ƒ: {response['data']['id']}")
99
+ return response['data']['id']
100
+ else:
101
+ print(f"request ์‹คํŒจ: {response}")
102
+ return 'FAILED'
103
+
104
+
105
+ # /generate API ํ˜ธ์ถœ ์„ฑ๊ณต ํ›„ ๋ฐ›์€ id๋กœ ์ปจํ…์ธ  ์ƒ์„ฑ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜.
106
+ def generate_status(content_id):
107
+ url = API_HOME + END_STATUS
108
+ payload = {"id": content_id}
109
+ headers = set_header()
110
+ response = requests.request("POST", url, json=payload, headers=headers).json()
111
+
112
+ # HTTP ์ƒํƒœ์ฝ”๋“œ ํ™•์ธ
113
+ if response['success'] == True:
114
+ print(f"์ •์ƒ: {response}")
115
+ if response['data']['status'] == 'queued':
116
+ print('์•„์ง ์ปจํ…์ธ  URL์ด ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.')
117
+ return 'PENDING'
118
+ if response['data']['status'] == 'processing':
119
+ print('์ปจํ…์ธ  URL์ด ์•„์ง ์ƒ์„ฑ ์ค‘ ์ž…๋‹ˆ๋‹ค.')
120
+ return 'PENDING'
121
+ elif response['data']['status'] == 'success':
122
+ return response['data']['file']
123
+
124
+ else:
125
+ print(f"request ์‹คํŒจ")
126
+
127
+
128
+ def download_content(url):
129
+ data = requests.get(url, timeout=30)
130
+ save_path = "downloaded_example.mp4"
131
+ with open(save_path, 'wb') as file:
132
+ for chunk in data.iter_content(chunk_size=128):
133
+ file.write(chunk)
134
+ print(f"MP4 ํŒŒ์ผ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ๋กœ: {save_path}")
135
+ return 'success'
136
+
137
+
138
+ # # ์‹คํ–‰.
139
+
140
+ # get_language_id() # Korean: 61b8b2fa4268666c126babf1
141
+ # get_dialects() # Korea: 61b8b30f4268666c126bac8d
142
+ # get_voices('61b8b2fa4268666c126babf1', '61b8b30f4268666c126bac8d')
143
+
144
+ try:
145
+ id = generate_contents()
146
+ if id != 'FAILED':
147
+ status = 'PENDING'
148
+ while status == 'PENDING':
149
+ time.sleep(9) # 9์ดˆ ๊ธฐ๋‹ค๋ฆผ
150
+ print('9์ดˆ ๋Œ€๊ธฐ ํ›„ ์ƒํƒœํ™•์ธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.')
151
+ status = generate_status(id)
152
+ if status != 'PENDING': break
153
+
154
+ print(f"์ปจํ…์ธ  ์ƒ์„ฑ ์„ฑ๊ณต: {status}")
155
+ else:
156
+ print('์ƒ์„ฑ ์‹คํŒจ..')
157
+ except Exception as e:
158
+ print(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
159
+
160
+ def gradio_generate_contents(user_content, brand_name):
161
+ return generate_contents(user_content, brand_name)
162
+
163
+ def gradio_generate_status(content_id):
164
+ return generate_status(content_id)
165
+
166
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ˆ˜์ •
167
+ iface = gr.Interface(
168
+ fn=gradio_generate_contents, # ์ปจํ…์ธ  ์ƒ์„ฑ ํ•จ์ˆ˜
169
+ inputs=[gr.Textbox(label="์ปจํ…์ธ  ํ…์ŠคํŠธ"), gr.Textbox(label="๋ธŒ๋žœ๋“œ๋ช…")],
170
+ outputs=gr.Textbox(label="์ปจํ…์ธ  ID")
171
+ )
172
+
173
+ status_iface = gr.Interface(
174
+ fn=gradio_generate_status, # ์ปจํ…์ธ  ์ƒํƒœ ํ™•์ธ ํ•จ์ˆ˜
175
+ inputs=gr.Textbox(label="์ปจํ…์ธ  ID"),
176
+ outputs=gr.Textbox(label="์ปจํ…์ธ  ์ƒํƒœ ๋˜๋Š” URL")
177
+ )
178
+
179
+ iface.launch()
180
+ status_iface.launch()