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import gradio as gr
|
2 |
+
import replicate
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
from io import BytesIO
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# ํ๊ฒฝ ๋ณ์์์ ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ
|
10 |
+
REPLICATE_API_TOKEN = os.getenv("RAPI_TOKEN")
|
11 |
+
FRIENDLI_TOKEN = os.getenv("FRIENDLI_TOKEN")
|
12 |
+
|
13 |
+
def generate_prompt_with_llm(topic):
|
14 |
+
"""์ฃผ์ ๋ฅผ ๋ฐ์์ LLM์ ์ฌ์ฉํด ์ด๋ฏธ์ง ํ๋กฌํํธ๋ฅผ ์์ฑ"""
|
15 |
+
url = "https://api.friendli.ai/dedicated/v1/chat/completions"
|
16 |
+
headers = {
|
17 |
+
"Authorization": f"Bearer {FRIENDLI_TOKEN}",
|
18 |
+
"Content-Type": "application/json"
|
19 |
+
}
|
20 |
+
|
21 |
+
# ํ๋กฌํํธ ์์ฑ์ ์ํ ์์คํ
๋ฉ์์ง
|
22 |
+
system_prompt = """๋น์ ์ ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํ๋กฌํํธ๋ฅผ ๋ง๋๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
23 |
+
์ฌ์ฉ์๊ฐ ์ฃผ์ ๋ฅผ ์ ๊ณตํ๋ฉด, ๊ทธ ์ฃผ์ ์ ๋ง๋ ์์ธํ๊ณ ์ฐฝ์์ ์ธ ์ด๋ฏธ์ง ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์.
|
24 |
+
ํ๋กฌํํธ๋ ๊ตฌ์ฒด์ ์ธ ์๊ฐ์ ์์, ์คํ์ผ, ๋ถ์๊ธฐ, ์์ ๋ฑ์ ํฌํจํด์ผ ํฉ๋๋ค.
|
25 |
+
์๋ต์ ํ๋กฌํํธ๋ง ๋ฐํํ๊ณ ๋ค๋ฅธ ์ค๋ช
์ ํฌํจํ์ง ๋ง์ธ์."""
|
26 |
+
|
27 |
+
payload = {
|
28 |
+
"model": "dep89a2fld32mcm",
|
29 |
+
"messages": [
|
30 |
+
{
|
31 |
+
"role": "system",
|
32 |
+
"content": system_prompt
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"role": "user",
|
36 |
+
"content": f"๋ค์ ์ฃผ์ ์ ๋ํ ์ด๋ฏธ์ง ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์: {topic}"
|
37 |
+
}
|
38 |
+
],
|
39 |
+
"max_tokens": 200,
|
40 |
+
"top_p": 0.8,
|
41 |
+
"stream": False
|
42 |
+
}
|
43 |
+
|
44 |
+
try:
|
45 |
+
response = requests.post(url, json=payload, headers=headers)
|
46 |
+
if response.status_code == 200:
|
47 |
+
result = response.json()
|
48 |
+
return result['choices'][0]['message']['content'].strip()
|
49 |
+
else:
|
50 |
+
return f"ํ๋กฌํํธ ์์ฑ ์คํจ: {response.status_code}"
|
51 |
+
except Exception as e:
|
52 |
+
return f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
53 |
+
|
54 |
+
def translate_to_english(text):
|
55 |
+
"""ํ๊ธ ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ (LLM ์ฌ์ฉ)"""
|
56 |
+
# ๊ฐ๋จํ ํ๊ธ ์ฒดํฌ
|
57 |
+
if not any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text):
|
58 |
+
return text # ํ๊ธ์ด ์์ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
|
59 |
+
|
60 |
+
url = "https://api.friendli.ai/dedicated/v1/chat/completions"
|
61 |
+
headers = {
|
62 |
+
"Authorization": f"Bearer {FRIENDLI_TOKEN}",
|
63 |
+
"Content-Type": "application/json"
|
64 |
+
}
|
65 |
+
|
66 |
+
payload = {
|
67 |
+
"model": "dep89a2fld32mcm",
|
68 |
+
"messages": [
|
69 |
+
{
|
70 |
+
"role": "system",
|
71 |
+
"content": "You are a translator. Translate the given Korean text to English. Only return the translation without any explanation."
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"role": "user",
|
75 |
+
"content": text
|
76 |
+
}
|
77 |
+
],
|
78 |
+
"max_tokens": 500,
|
79 |
+
"top_p": 0.8,
|
80 |
+
"stream": False
|
81 |
+
}
|
82 |
+
|
83 |
+
try:
|
84 |
+
response = requests.post(url, json=payload, headers=headers)
|
85 |
+
if response.status_code == 200:
|
86 |
+
result = response.json()
|
87 |
+
return result['choices'][0]['message']['content'].strip()
|
88 |
+
else:
|
89 |
+
return text # ๋ฒ์ญ ์คํจ์ ์๋ณธ ๋ฐํ
|
90 |
+
except Exception as e:
|
91 |
+
return text # ์ค๋ฅ ๋ฐ์์ ์๋ณธ ๋ฐํ
|
92 |
+
|
93 |
+
def generate_image(prompt, seed=10):
|
94 |
+
"""Replicate API๋ฅผ ์ฌ์ฉํด ์ด๋ฏธ์ง ์์ฑ"""
|
95 |
+
try:
|
96 |
+
# ํ๊ธ์ด ํฌํจ๋ ๊ฒฝ์ฐ ์์ด๋ก ๋ฒ์ญ
|
97 |
+
english_prompt = translate_to_english(prompt)
|
98 |
+
|
99 |
+
# Replicate ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
100 |
+
if not REPLICATE_API_TOKEN:
|
101 |
+
return None, "RAPI_TOKEN ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค."
|
102 |
+
|
103 |
+
client = replicate.Client(api_token=REPLICATE_API_TOKEN)
|
104 |
+
|
105 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
106 |
+
input_params = {
|
107 |
+
"seed": seed,
|
108 |
+
"prompt": english_prompt,
|
109 |
+
"speed_mode": "Extra Juiced ๐ (even more speed)",
|
110 |
+
"output_quality": 80
|
111 |
+
}
|
112 |
+
|
113 |
+
output = client.run(
|
114 |
+
"prunaai/hidream-l1-fast:17c237d753218fed0ed477cb553902b6b75735f48c128537ab829096ef3d3645",
|
115 |
+
input=input_params
|
116 |
+
)
|
117 |
+
|
118 |
+
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ ์ฝ๊ธฐ
|
119 |
+
if output:
|
120 |
+
# output์ด URL์ธ ๊ฒฝ์ฐ
|
121 |
+
if isinstance(output, str) and output.startswith('http'):
|
122 |
+
response = requests.get(output)
|
123 |
+
img = Image.open(BytesIO(response.content))
|
124 |
+
return img, f"์ฌ์ฉ๋ ์์ด ํ๋กฌํํธ: {english_prompt}"
|
125 |
+
# output์ด ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ์ธ ๊ฒฝ์ฐ
|
126 |
+
else:
|
127 |
+
img = Image.open(BytesIO(output.read()))
|
128 |
+
return img, f"์ฌ์ฉ๋ ์์ด ํ๋กฌํํธ: {english_prompt}"
|
129 |
+
else:
|
130 |
+
return None, "์ด๋ฏธ์ง ์์ฑ ์คํจ"
|
131 |
+
|
132 |
+
except Exception as e:
|
133 |
+
return None, f"์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
134 |
+
|
135 |
+
def process_topic_to_image(topic, manual_prompt, use_manual, seed):
|
136 |
+
"""์ฃผ์ ๋ฅผ ๋ฐ์์ ํ๋กฌํํธ ์์ฑ ํ ์ด๋ฏธ์ง ์์ฑ"""
|
137 |
+
if use_manual and manual_prompt.strip():
|
138 |
+
# ์๋ ํ๋กฌํํธ ์ฌ์ฉ
|
139 |
+
prompt = manual_prompt.strip()
|
140 |
+
else:
|
141 |
+
# LLM์ผ๋ก ํ๋กฌํํธ ์์ฑ
|
142 |
+
prompt = generate_prompt_with_llm(topic)
|
143 |
+
if "์ค๋ฅ" in prompt or "์คํจ" in prompt:
|
144 |
+
return None, prompt, prompt
|
145 |
+
|
146 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
147 |
+
image, status = generate_image(prompt, seed)
|
148 |
+
|
149 |
+
return image, prompt, status
|
150 |
+
|
151 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
152 |
+
with gr.Blocks(title="AI ์ด๋ฏธ์ง ์์ฑ๊ธฐ") as demo:
|
153 |
+
gr.Markdown("""
|
154 |
+
# ๐จ AI ์ด๋ฏธ์ง ์์ฑ๊ธฐ
|
155 |
+
|
156 |
+
์ฃผ์ ๋ฅผ ์
๋ ฅํ๋ฉด AI๊ฐ ํ๋กฌํํธ๋ฅผ ์์ฑํ๊ณ ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
|
157 |
+
ํ๋กฌํํธ๋ ์๋ ์์ฑ๋๊ฑฐ๋ ์ง์ ํธ์งํ ์ ์์ผ๋ฉฐ, ํ๊ธ ํ๋กฌํํธ๋ ์๋์ผ๋ก ์์ด๋ก ๋ฒ์ญ๋ฉ๋๋ค.
|
158 |
+
""")
|
159 |
+
|
160 |
+
with gr.Row():
|
161 |
+
with gr.Column(scale=1):
|
162 |
+
topic_input = gr.Textbox(
|
163 |
+
label="์ฃผ์ ์
๋ ฅ",
|
164 |
+
placeholder="์: ์ฐ์ฃผ๋ฅผ ์ฌํํ๋ ๊ณ ์์ด",
|
165 |
+
lines=2
|
166 |
+
)
|
167 |
+
|
168 |
+
generated_prompt = gr.Textbox(
|
169 |
+
label="์์ฑ๋ ํ๋กฌํํธ (ํธ์ง ๊ฐ๋ฅ)",
|
170 |
+
placeholder="์๋์ผ๋ก ์์ฑ๋ ํ๋กฌํํธ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.",
|
171 |
+
lines=3
|
172 |
+
)
|
173 |
+
|
174 |
+
use_manual_checkbox = gr.Checkbox(
|
175 |
+
label="์๋ ํ๋กฌํํธ ์ฌ์ฉ",
|
176 |
+
value=False
|
177 |
+
)
|
178 |
+
|
179 |
+
seed_input = gr.Slider(
|
180 |
+
minimum=1,
|
181 |
+
maximum=100,
|
182 |
+
value=10,
|
183 |
+
step=1,
|
184 |
+
label="์๋ ๊ฐ (๋์ผํ ์๋๋ ๋์ผํ ์ด๋ฏธ์ง ์์ฑ)"
|
185 |
+
)
|
186 |
+
|
187 |
+
generate_prompt_btn = gr.Button("ํ๋กฌํํธ ์์ฑ", variant="secondary")
|
188 |
+
generate_image_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ", variant="primary")
|
189 |
+
|
190 |
+
with gr.Column(scale=1):
|
191 |
+
output_image = gr.Image(
|
192 |
+
label="์์ฑ๋ ์ด๋ฏธ์ง",
|
193 |
+
type="pil"
|
194 |
+
)
|
195 |
+
|
196 |
+
status_text = gr.Textbox(
|
197 |
+
label="์ํ ๋ฉ์์ง",
|
198 |
+
lines=2,
|
199 |
+
interactive=False
|
200 |
+
)
|
201 |
+
|
202 |
+
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
203 |
+
def generate_prompt_only(topic):
|
204 |
+
if not topic.strip():
|
205 |
+
return "์ฃผ์ ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
206 |
+
prompt = generate_prompt_with_llm(topic)
|
207 |
+
return prompt
|
208 |
+
|
209 |
+
generate_prompt_btn.click(
|
210 |
+
fn=generate_prompt_only,
|
211 |
+
inputs=[topic_input],
|
212 |
+
outputs=[generated_prompt]
|
213 |
+
)
|
214 |
+
|
215 |
+
# ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
216 |
+
generate_image_btn.click(
|
217 |
+
fn=process_topic_to_image,
|
218 |
+
inputs=[topic_input, generated_prompt, use_manual_checkbox, seed_input],
|
219 |
+
outputs=[output_image, generated_prompt, status_text]
|
220 |
+
)
|
221 |
+
|
222 |
+
gr.Markdown("""
|
223 |
+
---
|
224 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
225 |
+
1. **์ฃผ์ ์
๋ ฅ**: ์ํ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ๋ฅผ ์
๋ ฅํ์ธ์.
|
226 |
+
2. **ํ๋กฌํํธ ์์ฑ**: "ํ๋กฌํํธ ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด AI๊ฐ ์๋์ผ๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํฉ๋๋ค.
|
227 |
+
3. **ํ๋กฌํํธ ํธ์ง**: ์์ฑ๋ ํ๋กฌํํธ๋ฅผ ์ํ๋ ๋๋ก ์์ ํ ์ ์์ต๋๋ค.
|
228 |
+
4. **์ด๋ฏธ์ง ์์ฑ**: "์ด๋ฏธ์ง ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.
|
229 |
+
|
230 |
+
๐ก **ํ**:
|
231 |
+
- ํ๊ธ ํ๋กฌํํธ๋ ์๋์ผ๋ก ์์ด๋ก ๋ฒ์ญ๋์ด ์ ์ก๋ฉ๋๋ค.
|
232 |
+
- ์๋ ๊ฐ์ ๊ณ ์ ํ๋ฉด ๋์ผํ ํ๋กฌํํธ๋ก ๋์ผํ ์ด๋ฏธ์ง๋ฅผ ์ฌ์์ฑํ ์ ์์ต๋๋ค.
|
233 |
+
- "์๋ ํ๋กฌํํธ ์ฌ์ฉ"์ ์ฒดํฌํ๋ฉด ์ฃผ์ ๊ธฐ๋ฐ ์๋ ์์ฑ์ ๊ฑด๋๋ฐ๊ณ ์ง์ ์
๋ ฅํ ํ๋กฌํํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
|
234 |
+
""")
|
235 |
+
|
236 |
+
# ์ฑ ์คํ
|
237 |
+
if __name__ == "__main__":
|
238 |
+
# ํ๊ฒฝ ๋ณ์ ํ์ธ
|
239 |
+
if not REPLICATE_API_TOKEN:
|
240 |
+
print("๊ฒฝ๊ณ : RAPI_TOKEN ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
241 |
+
if not FRIENDLI_TOKEN:
|
242 |
+
print("๊ฒฝ๊ณ : FRIENDLI_TOKEN ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
243 |
+
|
244 |
+
demo.launch()
|