Spaces:
Running
on
Zero
Running
on
Zero
Delete app-backup.py
Browse files- app-backup.py +0 -908
app-backup.py
DELETED
@@ -1,908 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
|
3 |
-
import os
|
4 |
-
import re
|
5 |
-
import tempfile
|
6 |
-
import gc # garbage collector ์ถ๊ฐ
|
7 |
-
from collections.abc import Iterator
|
8 |
-
from threading import Thread
|
9 |
-
import json
|
10 |
-
import requests
|
11 |
-
import cv2
|
12 |
-
import base64
|
13 |
-
import logging
|
14 |
-
import time
|
15 |
-
from urllib.parse import quote # URL ์ธ์ฝ๋ฉ์ ์ํด ์ถ๊ฐ
|
16 |
-
|
17 |
-
import gradio as gr
|
18 |
-
import spaces
|
19 |
-
import torch
|
20 |
-
from loguru import logger
|
21 |
-
from PIL import Image
|
22 |
-
from transformers import AutoProcessor, Gemma3ForConditionalGeneration, TextIteratorStreamer
|
23 |
-
|
24 |
-
# CSV/TXT/PDF ๋ถ์
|
25 |
-
import pandas as pd
|
26 |
-
import PyPDF2
|
27 |
-
|
28 |
-
# =============================================================================
|
29 |
-
# (์ ๊ท) ์ด๋ฏธ์ง API ๊ด๋ จ ํจ์๋ค
|
30 |
-
# =============================================================================
|
31 |
-
from gradio_client import Client
|
32 |
-
|
33 |
-
API_URL = "http://211.233.58.201:7896"
|
34 |
-
|
35 |
-
logging.basicConfig(
|
36 |
-
level=logging.DEBUG,
|
37 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
38 |
-
)
|
39 |
-
|
40 |
-
def test_api_connection() -> str:
|
41 |
-
"""API ์๋ฒ ์ฐ๊ฒฐ ํ
์คํธ"""
|
42 |
-
try:
|
43 |
-
client = Client(API_URL)
|
44 |
-
return "API ์ฐ๊ฒฐ ์ฑ๊ณต: ์ ์ ์๋ ์ค"
|
45 |
-
except Exception as e:
|
46 |
-
logging.error(f"API ์ฐ๊ฒฐ ํ
์คํธ ์คํจ: {e}")
|
47 |
-
return f"API ์ฐ๊ฒฐ ์คํจ: {e}"
|
48 |
-
|
49 |
-
def generate_image(prompt: str, width: float, height: float, guidance: float, inference_steps: float, seed: float):
|
50 |
-
"""์ด๋ฏธ์ง ์์ฑ ํจ์ (๋ฐํ ํ์์ ์ ์ฐํ๊ฒ ๋์)"""
|
51 |
-
if not prompt:
|
52 |
-
return None, "์ค๋ฅ: ํ๋กฌํํธ๊ฐ ํ์ํฉ๋๋ค."
|
53 |
-
try:
|
54 |
-
logging.info(f"ํ๋กฌํํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ์์ฑ API ํธ์ถ: {prompt}")
|
55 |
-
|
56 |
-
client = Client(API_URL)
|
57 |
-
result = client.predict(
|
58 |
-
prompt=prompt,
|
59 |
-
width=int(width),
|
60 |
-
height=int(height),
|
61 |
-
guidance=float(guidance),
|
62 |
-
inference_steps=int(inference_steps),
|
63 |
-
seed=int(seed),
|
64 |
-
do_img2img=False,
|
65 |
-
init_image=None,
|
66 |
-
image2image_strength=0.8,
|
67 |
-
resize_img=True,
|
68 |
-
api_name="/generate_image"
|
69 |
-
)
|
70 |
-
|
71 |
-
logging.info(f"์ด๋ฏธ์ง ์์ฑ ๊ฒฐ๊ณผ: {type(result)}, ๊ธธ์ด: {len(result) if isinstance(result, (list, tuple)) else '์ ์ ์์'}")
|
72 |
-
|
73 |
-
# ๊ฒฐ๊ณผ๊ฐ ํํ์ด๋ ๋ฆฌ์คํธ ํํ๋ก ๋ฐํ๋๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
74 |
-
if isinstance(result, (list, tuple)) and len(result) > 0:
|
75 |
-
image_data = result[0] # ์ฒซ ๋ฒ์งธ ์์๊ฐ ์ด๋ฏธ์ง ๋ฐ์ดํฐ
|
76 |
-
seed_info = result[1] if len(result) > 1 else "์ ์ ์๋ ์๋"
|
77 |
-
return image_data, seed_info
|
78 |
-
else:
|
79 |
-
# ๋ค๋ฅธ ํํ๋ก ๋ฐํ๋ ๊ฒฝ์ฐ (๋จ์ผ ๊ฐ์ธ ๊ฒฝ์ฐ)
|
80 |
-
return result, "์ ์ ์๋ ์๋"
|
81 |
-
|
82 |
-
except Exception as e:
|
83 |
-
logging.error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {str(e)}")
|
84 |
-
return None, f"์ค๋ฅ: {str(e)}"
|
85 |
-
|
86 |
-
# Base64 ํจ๋ฉ ์์ ํจ์
|
87 |
-
def fix_base64_padding(data):
|
88 |
-
"""Base64 ๋ฌธ์์ด์ ํจ๋ฉ์ ์์ ํฉ๋๋ค."""
|
89 |
-
if isinstance(data, bytes):
|
90 |
-
data = data.decode('utf-8')
|
91 |
-
|
92 |
-
# base64,๋ก ์์ํ๋ ๋ถ๋ถ ์ ๊ฑฐ
|
93 |
-
if "base64," in data:
|
94 |
-
data = data.split("base64,", 1)[1]
|
95 |
-
|
96 |
-
# ํจ๋ฉ ๋ฌธ์ ์ถ๊ฐ (4์ ๋ฐฐ์ ๊ธธ์ด๊ฐ ๋๋๋ก)
|
97 |
-
missing_padding = len(data) % 4
|
98 |
-
if missing_padding:
|
99 |
-
data += '=' * (4 - missing_padding)
|
100 |
-
|
101 |
-
return data
|
102 |
-
|
103 |
-
# =============================================================================
|
104 |
-
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ํจ์
|
105 |
-
# =============================================================================
|
106 |
-
def clear_cuda_cache():
|
107 |
-
"""CUDA ์บ์๋ฅผ ๋ช
์์ ์ผ๋ก ๋น์๋๋ค."""
|
108 |
-
if torch.cuda.is_available():
|
109 |
-
torch.cuda.empty_cache()
|
110 |
-
gc.collect()
|
111 |
-
|
112 |
-
# =============================================================================
|
113 |
-
# SerpHouse ๊ด๋ จ ํจ์
|
114 |
-
# =============================================================================
|
115 |
-
SERPHOUSE_API_KEY = os.getenv("SERPHOUSE_API_KEY", "")
|
116 |
-
|
117 |
-
def extract_keywords(text: str, top_k: int = 5) -> str:
|
118 |
-
"""๋จ์ ํค์๋ ์ถ์ถ: ํ๊ธ, ์์ด, ์ซ์, ๊ณต๋ฐฑ๋ง ๋จ๊น"""
|
119 |
-
text = re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ\s]", "", text)
|
120 |
-
tokens = text.split()
|
121 |
-
return " ".join(tokens[:top_k])
|
122 |
-
|
123 |
-
def do_web_search(query: str) -> str:
|
124 |
-
"""SerpHouse LIVE API ํธ์ถํ์ฌ ๊ฒ์ ๊ฒฐ๊ณผ ๋งํฌ๋ค์ด ๋ฐํ"""
|
125 |
-
try:
|
126 |
-
url = "https://api.serphouse.com/serp/live"
|
127 |
-
params = {
|
128 |
-
"q": query,
|
129 |
-
"domain": "google.com",
|
130 |
-
"serp_type": "web",
|
131 |
-
"device": "desktop",
|
132 |
-
"lang": "en",
|
133 |
-
"num": "20"
|
134 |
-
}
|
135 |
-
headers = {"Authorization": f"Bearer {SERPHOUSE_API_KEY}"}
|
136 |
-
logger.info(f"SerpHouse API ํธ์ถ ์ค... ๊ฒ์์ด: {query}")
|
137 |
-
response = requests.get(url, headers=headers, params=params, timeout=60)
|
138 |
-
response.raise_for_status()
|
139 |
-
data = response.json()
|
140 |
-
results = data.get("results", {})
|
141 |
-
organic = None
|
142 |
-
if isinstance(results, dict) and "organic" in results:
|
143 |
-
organic = results["organic"]
|
144 |
-
elif isinstance(results, dict) and "results" in results:
|
145 |
-
if isinstance(results["results"], dict) and "organic" in results["results"]:
|
146 |
-
organic = results["results"]["organic"]
|
147 |
-
elif "organic" in data:
|
148 |
-
organic = data["organic"]
|
149 |
-
if not organic:
|
150 |
-
logger.warning("์๋ต์์ organic ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
151 |
-
return "์น ๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์๊ฑฐ๋ API ์๋ต ๊ตฌ์กฐ๊ฐ ์์๊ณผ ๋ค๋ฆ
๋๋ค."
|
152 |
-
max_results = min(20, len(organic))
|
153 |
-
limited_organic = organic[:max_results]
|
154 |
-
summary_lines = []
|
155 |
-
for idx, item in enumerate(limited_organic, start=1):
|
156 |
-
title = item.get("title", "์ ๋ชฉ ์์")
|
157 |
-
link = item.get("link", "#")
|
158 |
-
snippet = item.get("snippet", "์ค๋ช
์์")
|
159 |
-
displayed_link = item.get("displayed_link", link)
|
160 |
-
summary_lines.append(
|
161 |
-
f"### ๊ฒฐ๊ณผ {idx}: {title}\n\n"
|
162 |
-
f"{snippet}\n\n"
|
163 |
-
f"**์ถ์ฒ**: [{displayed_link}]({link})\n\n"
|
164 |
-
f"---\n"
|
165 |
-
)
|
166 |
-
instructions = """
|
167 |
-
# ์น ๊ฒ์ ๊ฒฐ๊ณผ
|
168 |
-
์๋๋ ๊ฒ์ ๊ฒฐ๊ณผ์
๋๋ค. ์ง๋ฌธ์ ๋ต๋ณํ ๋ ์ด ์ ๋ณด๋ฅผ ํ์ฉํ์ธ์:
|
169 |
-
1. ๊ฐ ๊ฒฐ๊ณผ์ ์ ๋ชฉ, ๋ด์ฉ, ์ถ์ฒ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ์ธ์.
|
170 |
-
2. ๋ต๋ณ์ ๊ด๋ จ ์ ๋ณด์ ์ถ์ฒ๋ฅผ ๋ช
์์ ์ผ๋ก ์ธ์ฉํ์ธ์ (์: "[์ถ์ฒ ์ ๋ชฉ](๋งํฌ)").
|
171 |
-
3. ์๋ต์ ์ค์ ์ถ์ฒ ๋งํฌ๋ฅผ ํฌํจํ์ธ์.
|
172 |
-
4. ์ฌ๋ฌ ์ถ์ฒ์ ์ ๋ณด๋ฅผ ์ข
ํฉํ์ฌ ๋ต๋ณํ์ธ์.
|
173 |
-
5. ๋ง์ง๋ง์ "์ฐธ๊ณ ์๋ฃ:" ์น์
์ ์ถ๊ฐํ๊ณ ์ฃผ์ ์ถ์ฒ ๋งํฌ๋ฅผ ๋์ดํ์ธ์.
|
174 |
-
"""
|
175 |
-
return instructions + "\n".join(summary_lines)
|
176 |
-
except Exception as e:
|
177 |
-
logger.error(f"์น ๊ฒ์ ์คํจ: {e}")
|
178 |
-
return f"์น ๊ฒ์ ์คํจ: {str(e)}"
|
179 |
-
|
180 |
-
# =============================================================================
|
181 |
-
# ๋ชจ๋ธ ๋ฐ ํ๋ก์ธ์ ๋ก๋ฉ
|
182 |
-
# =============================================================================
|
183 |
-
MAX_CONTENT_CHARS = 2000
|
184 |
-
MAX_INPUT_LENGTH = 2096
|
185 |
-
model_id = os.getenv("MODEL_ID", "VIDraft/Gemma-3-R1984-4B")
|
186 |
-
processor = AutoProcessor.from_pretrained(model_id, padding_side="left")
|
187 |
-
model = Gemma3ForConditionalGeneration.from_pretrained(
|
188 |
-
model_id,
|
189 |
-
device_map="auto",
|
190 |
-
torch_dtype=torch.bfloat16,
|
191 |
-
attn_implementation="eager"
|
192 |
-
)
|
193 |
-
MAX_NUM_IMAGES = int(os.getenv("MAX_NUM_IMAGES", "5"))
|
194 |
-
|
195 |
-
# =============================================================================
|
196 |
-
# CSV, TXT, PDF ๋ถ์ ํจ์๋ค
|
197 |
-
# =============================================================================
|
198 |
-
def analyze_csv_file(path: str) -> str:
|
199 |
-
try:
|
200 |
-
df = pd.read_csv(path)
|
201 |
-
if df.shape[0] > 50 or df.shape[1] > 10:
|
202 |
-
df = df.iloc[:50, :10]
|
203 |
-
df_str = df.to_string()
|
204 |
-
if len(df_str) > MAX_CONTENT_CHARS:
|
205 |
-
df_str = df_str[:MAX_CONTENT_CHARS] + "\n...(์ผ๋ถ ์๋ต)..."
|
206 |
-
return f"**[CSV ํ์ผ: {os.path.basename(path)}]**\n\n{df_str}"
|
207 |
-
except Exception as e:
|
208 |
-
return f"CSV ํ์ผ ์ฝ๊ธฐ ์คํจ ({os.path.basename(path)}): {str(e)}"
|
209 |
-
|
210 |
-
def analyze_txt_file(path: str) -> str:
|
211 |
-
try:
|
212 |
-
with open(path, "r", encoding="utf-8") as f:
|
213 |
-
text = f.read()
|
214 |
-
if len(text) > MAX_CONTENT_CHARS:
|
215 |
-
text = text[:MAX_CONTENT_CHARS] + "\n...(์ผ๋ถ ์๋ต)..."
|
216 |
-
return f"**[TXT ํ์ผ: {os.path.basename(path)}]**\n\n{text}"
|
217 |
-
except Exception as e:
|
218 |
-
return f"TXT ํ์ผ ์ฝ๊ธฐ ์คํจ ({os.path.basename(path)}): {str(e)}"
|
219 |
-
|
220 |
-
def pdf_to_markdown(pdf_path: str) -> str:
|
221 |
-
text_chunks = []
|
222 |
-
try:
|
223 |
-
with open(pdf_path, "rb") as f:
|
224 |
-
reader = PyPDF2.PdfReader(f)
|
225 |
-
max_pages = min(5, len(reader.pages))
|
226 |
-
for page_num in range(max_pages):
|
227 |
-
page_text = reader.pages[page_num].extract_text() or ""
|
228 |
-
page_text = page_text.strip()
|
229 |
-
if page_text:
|
230 |
-
if len(page_text) > MAX_CONTENT_CHARS // max_pages:
|
231 |
-
page_text = page_text[:MAX_CONTENT_CHARS // max_pages] + "...(์ผ๋ถ ์๋ต)"
|
232 |
-
text_chunks.append(f"## ํ์ด์ง {page_num+1}\n\n{page_text}\n")
|
233 |
-
if len(reader.pages) > max_pages:
|
234 |
-
text_chunks.append(f"\n...(์ ์ฒด {len(reader.pages)}ํ์ด์ง ์ค {max_pages}ํ์ด์ง๋ง ํ์)...")
|
235 |
-
except Exception as e:
|
236 |
-
return f"PDF ํ์ผ ์ฝ๊ธฐ ์คํจ ({os.path.basename(pdf_path)}): {str(e)}"
|
237 |
-
full_text = "\n".join(text_chunks)
|
238 |
-
if len(full_text) > MAX_CONTENT_CHARS:
|
239 |
-
full_text = full_text[:MAX_CONTENT_CHARS] + "\n...(์ผ๋ถ ์๋ต)..."
|
240 |
-
return f"**[PDF ํ์ผ: {os.path.basename(pdf_path)}]**\n\n{full_text}"
|
241 |
-
|
242 |
-
# =============================================================================
|
243 |
-
# ์ด๋ฏธ์ง/๋น๋์ค ํ์ผ ์ ํ ๊ฒ์ฌ
|
244 |
-
# =============================================================================
|
245 |
-
def count_files_in_new_message(paths: list[str]) -> tuple[int, int]:
|
246 |
-
image_count = 0
|
247 |
-
video_count = 0
|
248 |
-
for path in paths:
|
249 |
-
if path.endswith(".mp4"):
|
250 |
-
video_count += 1
|
251 |
-
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", path, re.IGNORECASE):
|
252 |
-
image_count += 1
|
253 |
-
return image_count, video_count
|
254 |
-
|
255 |
-
def count_files_in_history(history: list[dict]) -> tuple[int, int]:
|
256 |
-
image_count = 0
|
257 |
-
video_count = 0
|
258 |
-
for item in history:
|
259 |
-
if item["role"] != "user" or isinstance(item["content"], str):
|
260 |
-
continue
|
261 |
-
if isinstance(item["content"], list) and len(item["content"]) > 0:
|
262 |
-
file_path = item["content"][0]
|
263 |
-
if isinstance(file_path, str):
|
264 |
-
if file_path.endswith(".mp4"):
|
265 |
-
video_count += 1
|
266 |
-
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE):
|
267 |
-
image_count += 1
|
268 |
-
return image_count, video_count
|
269 |
-
|
270 |
-
def validate_media_constraints(message: dict, history: list[dict]) -> bool:
|
271 |
-
media_files = [f for f in message["files"] if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE) or f.endswith(".mp4")]
|
272 |
-
new_image_count, new_video_count = count_files_in_new_message(media_files)
|
273 |
-
history_image_count, history_video_count = count_files_in_history(history)
|
274 |
-
image_count = history_image_count + new_image_count
|
275 |
-
video_count = history_video_count + new_video_count
|
276 |
-
if video_count > 1:
|
277 |
-
gr.Warning("๋น๋์ค ํ์ผ์ ํ๋๋ง ์ง์๋ฉ๋๋ค.")
|
278 |
-
return False
|
279 |
-
if video_count == 1:
|
280 |
-
if image_count > 0:
|
281 |
-
gr.Warning("์ด๋ฏธ์ง์ ๋น๋์ค๋ฅผ ํผํฉํ๋ ๊ฒ์ ํ์ฉ๋์ง ์์ต๋๋ค.")
|
282 |
-
return False
|
283 |
-
if "<image>" in message["text"]:
|
284 |
-
gr.Warning("<image> ํ๊ทธ์ ๋น๋์ค ํ์ผ์ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.")
|
285 |
-
return False
|
286 |
-
if video_count == 0 and image_count > MAX_NUM_IMAGES:
|
287 |
-
gr.Warning(f"์ต๋ {MAX_NUM_IMAGES}์ฅ์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ ์ ์์ต๋๋ค.")
|
288 |
-
return False
|
289 |
-
if "<image>" in message["text"]:
|
290 |
-
image_files = [f for f in message["files"] if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE)]
|
291 |
-
image_tag_count = message["text"].count("<image>")
|
292 |
-
if image_tag_count != len(image_files):
|
293 |
-
gr.Warning("ํ
์คํธ์ ์๋ <image> ํ๊ทธ์ ๊ฐ์๊ฐ ์ด๋ฏธ์ง ํ์ผ ๊ฐ์์ ์ผ์นํ์ง ์์ต๋๋ค.")
|
294 |
-
return False
|
295 |
-
return True
|
296 |
-
|
297 |
-
# =============================================================================
|
298 |
-
# ๋น๋์ค ์ฒ๋ฆฌ ํจ์
|
299 |
-
# =============================================================================
|
300 |
-
def downsample_video(video_path: str) -> list[tuple[Image.Image, float]]:
|
301 |
-
vidcap = cv2.VideoCapture(video_path)
|
302 |
-
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
303 |
-
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
304 |
-
frame_interval = max(int(fps), int(total_frames / 10))
|
305 |
-
frames = []
|
306 |
-
for i in range(0, total_frames, frame_interval):
|
307 |
-
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
308 |
-
success, image = vidcap.read()
|
309 |
-
if success:
|
310 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
311 |
-
image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5)
|
312 |
-
pil_image = Image.fromarray(image)
|
313 |
-
timestamp = round(i / fps, 2)
|
314 |
-
frames.append((pil_image, timestamp))
|
315 |
-
if len(frames) >= 5:
|
316 |
-
break
|
317 |
-
vidcap.release()
|
318 |
-
return frames
|
319 |
-
|
320 |
-
def process_video(video_path: str) -> tuple[list[dict], list[str]]:
|
321 |
-
content = []
|
322 |
-
temp_files = []
|
323 |
-
frames = downsample_video(video_path)
|
324 |
-
for pil_image, timestamp in frames:
|
325 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
326 |
-
pil_image.save(temp_file.name)
|
327 |
-
temp_files.append(temp_file.name)
|
328 |
-
content.append({"type": "text", "text": f"ํ๋ ์ {timestamp}:"})
|
329 |
-
content.append({"type": "image", "url": temp_file.name})
|
330 |
-
return content, temp_files
|
331 |
-
|
332 |
-
# =============================================================================
|
333 |
-
# interleaved <image> ์ฒ๋ฆฌ ํจ์
|
334 |
-
# =============================================================================
|
335 |
-
def process_interleaved_images(message: dict) -> list[dict]:
|
336 |
-
parts = re.split(r"(<image>)", message["text"])
|
337 |
-
content = []
|
338 |
-
image_files = [f for f in message["files"] if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE)]
|
339 |
-
image_index = 0
|
340 |
-
for part in parts:
|
341 |
-
if part == "<image>" and image_index < len(image_files):
|
342 |
-
content.append({"type": "image", "url": image_files[image_index]})
|
343 |
-
image_index += 1
|
344 |
-
elif part.strip():
|
345 |
-
content.append({"type": "text", "text": part.strip()})
|
346 |
-
else:
|
347 |
-
if isinstance(part, str) and part != "<image>":
|
348 |
-
content.append({"type": "text", "text": part})
|
349 |
-
return content
|
350 |
-
|
351 |
-
# =============================================================================
|
352 |
-
# ํ์ผ ์ฒ๋ฆฌ -> content ์์ฑ
|
353 |
-
# =============================================================================
|
354 |
-
def is_image_file(file_path: str) -> bool:
|
355 |
-
return bool(re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE))
|
356 |
-
|
357 |
-
def is_video_file(file_path: str) -> bool:
|
358 |
-
return file_path.endswith(".mp4")
|
359 |
-
|
360 |
-
def is_document_file(file_path: str) -> bool:
|
361 |
-
return file_path.lower().endswith(".pdf") or file_path.lower().endswith(".csv") or file_path.lower().endswith(".txt")
|
362 |
-
|
363 |
-
def process_new_user_message(message: dict) -> tuple[list[dict], list[str]]:
|
364 |
-
temp_files = []
|
365 |
-
if not message["files"]:
|
366 |
-
return [{"type": "text", "text": message["text"]}], temp_files
|
367 |
-
video_files = [f for f in message["files"] if is_video_file(f)]
|
368 |
-
image_files = [f for f in message["files"] if is_image_file(f)]
|
369 |
-
csv_files = [f for f in message["files"] if f.lower().endswith(".csv")]
|
370 |
-
txt_files = [f for f in message["files"] if f.lower().endswith(".txt")]
|
371 |
-
pdf_files = [f for f in message["files"] if f.lower().endswith(".pdf")]
|
372 |
-
content_list = [{"type": "text", "text": message["text"]}]
|
373 |
-
for csv_path in csv_files:
|
374 |
-
content_list.append({"type": "text", "text": analyze_csv_file(csv_path)})
|
375 |
-
for txt_path in txt_files:
|
376 |
-
content_list.append({"type": "text", "text": analyze_txt_file(txt_path)})
|
377 |
-
for pdf_path in pdf_files:
|
378 |
-
content_list.append({"type": "text", "text": pdf_to_markdown(pdf_path)})
|
379 |
-
if video_files:
|
380 |
-
video_content, video_temp_files = process_video(video_files[0])
|
381 |
-
content_list += video_content
|
382 |
-
temp_files.extend(video_temp_files)
|
383 |
-
return content_list, temp_files
|
384 |
-
if "<image>" in message["text"] and image_files:
|
385 |
-
interleaved_content = process_interleaved_images({"text": message["text"], "files": image_files})
|
386 |
-
if content_list and content_list[0]["type"] == "text":
|
387 |
-
content_list = content_list[1:]
|
388 |
-
return interleaved_content + content_list, temp_files
|
389 |
-
else:
|
390 |
-
for img_path in image_files:
|
391 |
-
content_list.append({"type": "image", "url": img_path})
|
392 |
-
return content_list, temp_files
|
393 |
-
|
394 |
-
# =============================================================================
|
395 |
-
# history -> LLM ๋ฉ์์ง ๋ณํ
|
396 |
-
# =============================================================================
|
397 |
-
def process_history(history: list[dict]) -> list[dict]:
|
398 |
-
messages = []
|
399 |
-
current_user_content = []
|
400 |
-
for item in history:
|
401 |
-
if item["role"] == "assistant":
|
402 |
-
if current_user_content:
|
403 |
-
messages.append({"role": "user", "content": current_user_content})
|
404 |
-
current_user_content = []
|
405 |
-
messages.append({"role": "assistant", "content": [{"type": "text", "text": item["content"]}]})
|
406 |
-
else:
|
407 |
-
content = item["content"]
|
408 |
-
if isinstance(content, str):
|
409 |
-
current_user_content.append({"type": "text", "text": content})
|
410 |
-
elif isinstance(content, list) and len(content) > 0:
|
411 |
-
file_path = content[0]
|
412 |
-
if is_image_file(file_path):
|
413 |
-
current_user_content.append({"type": "image", "url": file_path})
|
414 |
-
else:
|
415 |
-
current_user_content.append({"type": "text", "text": f"[ํ์ผ: {os.path.basename(file_path)}]"})
|
416 |
-
if current_user_content:
|
417 |
-
messages.append({"role": "user", "content": current_user_content})
|
418 |
-
return messages
|
419 |
-
|
420 |
-
# =============================================================================
|
421 |
-
# ๋ชจ๋ธ ์์ฑ ํจ์ (OOM ์บ์น)
|
422 |
-
# =============================================================================
|
423 |
-
def _model_gen_with_oom_catch(**kwargs):
|
424 |
-
try:
|
425 |
-
model.generate(**kwargs)
|
426 |
-
except torch.cuda.OutOfMemoryError:
|
427 |
-
raise RuntimeError("[OutOfMemoryError] GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํฉ๋๋ค.")
|
428 |
-
finally:
|
429 |
-
clear_cuda_cache()
|
430 |
-
|
431 |
-
# =============================================================================
|
432 |
-
# ๋ฉ์ธ ์ถ๋ก ํจ์
|
433 |
-
# =============================================================================
|
434 |
-
@spaces.GPU(duration=120)
|
435 |
-
def run(
|
436 |
-
message: dict,
|
437 |
-
history: list[dict],
|
438 |
-
system_prompt: str = "",
|
439 |
-
max_new_tokens: int = 512,
|
440 |
-
use_web_search: bool = False,
|
441 |
-
web_search_query: str = "",
|
442 |
-
age_group: str = "20๋",
|
443 |
-
mbti_personality: str = "INTP",
|
444 |
-
sexual_openness: int = 2,
|
445 |
-
image_gen: bool = False # "Image Gen" ์ฒดํฌ ์ฌ๋ถ
|
446 |
-
) -> Iterator[str]:
|
447 |
-
if not validate_media_constraints(message, history):
|
448 |
-
yield ""
|
449 |
-
return
|
450 |
-
temp_files = []
|
451 |
-
try:
|
452 |
-
# ์์คํ
ํ๋กฌํํธ์ ํ๋ฅด์๋ ์ ๋ณด ์ถ๊ฐ
|
453 |
-
persona = (
|
454 |
-
f"{system_prompt.strip()}\n\n"
|
455 |
-
f"์ฑ๋ณ: ์ฌ์ฑ\n"
|
456 |
-
f"์ฐ๋ น๋: {age_group}\n"
|
457 |
-
f"MBTI ํ๋ฅด์๋: {mbti_personality}\n"
|
458 |
-
f"์น์์ผ ๊ฐ๋ฐฉ์ฑ (1~5): {sexual_openness}\n"
|
459 |
-
)
|
460 |
-
combined_system_msg = f"[์์คํ
ํ๋กฌํํธ]\n{persona.strip()}\n\n"
|
461 |
-
|
462 |
-
if use_web_search:
|
463 |
-
user_text = message["text"]
|
464 |
-
ws_query = extract_keywords(user_text)
|
465 |
-
if ws_query.strip():
|
466 |
-
logger.info(f"[์๋ ์น ๊ฒ์ ํค์๋] {ws_query!r}")
|
467 |
-
ws_result = do_web_search(ws_query)
|
468 |
-
combined_system_msg += f"[๊ฒ์ ๊ฒฐ๊ณผ (์์ 20๊ฐ ํญ๋ชฉ)]\n{ws_result}\n\n"
|
469 |
-
combined_system_msg += (
|
470 |
-
"[์ฐธ๊ณ : ์ ๊ฒ์ ๊ฒฐ๊ณผ ๋งํฌ๋ฅผ ์ถ์ฒ๋ก ์ธ์ฉํ์ฌ ๋ต๋ณ]\n"
|
471 |
-
"[์ค์ ์ง์์ฌํญ]\n"
|
472 |
-
"1. ๋ต๋ณ์ ๊ฒ์ ๊ฒฐ๊ณผ์์ ์ฐพ์ ์ ๋ณด์ ์ถ์ฒ๋ฅผ ๋ฐ๋์ ์ธ์ฉํ์ธ์.\n"
|
473 |
-
"2. ์ถ์ฒ ์ธ์ฉ ์ \"[์ถ์ฒ ์ ๋ชฉ](๋งํฌ)\" ํ์์ ๋งํฌ๋ค์ด ๋งํฌ๋ฅผ ์ฌ์ฉํ์ธ์.\n"
|
474 |
-
"3. ์ฌ๋ฌ ์ถ์ฒ์ ์ ๋ณด๋ฅผ ์ข
ํฉํ์ฌ ๋ต๋ณํ์ธ์.\n"
|
475 |
-
"4. ๋ต๋ณ ๋ง์ง๋ง์ \"์ฐธ๊ณ ์๋ฃ:\" ์น์
์ ์ถ๊ฐํ๊ณ ์ฌ์ฉํ ์ฃผ์ ์ถ์ฒ ๋งํฌ๋ฅผ ๋์ดํ์ธ์.\n"
|
476 |
-
)
|
477 |
-
else:
|
478 |
-
combined_system_msg += "[์ ํจํ ํค์๋๊ฐ ์์ด ์น ๊ฒ์์ ๊ฑด๋๋๋๋ค]\n\n"
|
479 |
-
messages = []
|
480 |
-
if combined_system_msg.strip():
|
481 |
-
messages.append({"role": "system", "content": [{"type": "text", "text": combined_system_msg.strip()}]})
|
482 |
-
messages.extend(process_history(history))
|
483 |
-
user_content, user_temp_files = process_new_user_message(message)
|
484 |
-
temp_files.extend(user_temp_files)
|
485 |
-
for item in user_content:
|
486 |
-
if item["type"] == "text" and len(item["text"]) > MAX_CONTENT_CHARS:
|
487 |
-
item["text"] = item["text"][:MAX_CONTENT_CHARS] + "\n...(์ผ๋ถ ์๋ต)..."
|
488 |
-
messages.append({"role": "user", "content": user_content})
|
489 |
-
inputs = processor.apply_chat_template(
|
490 |
-
messages,
|
491 |
-
add_generation_prompt=True,
|
492 |
-
tokenize=True,
|
493 |
-
return_dict=True,
|
494 |
-
return_tensors="pt",
|
495 |
-
).to(device=model.device, dtype=torch.bfloat16)
|
496 |
-
if inputs.input_ids.shape[1] > MAX_INPUT_LENGTH:
|
497 |
-
inputs.input_ids = inputs.input_ids[:, -MAX_INPUT_LENGTH:]
|
498 |
-
if 'attention_mask' in inputs:
|
499 |
-
inputs.attention_mask = inputs.attention_mask[:, -MAX_INPUT_LENGTH:]
|
500 |
-
streamer = TextIteratorStreamer(processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
501 |
-
gen_kwargs = dict(inputs, streamer=streamer, max_new_tokens=max_new_tokens)
|
502 |
-
t = Thread(target=_model_gen_with_oom_catch, kwargs=gen_kwargs)
|
503 |
-
t.start()
|
504 |
-
output_so_far = ""
|
505 |
-
for new_text in streamer:
|
506 |
-
output_so_far += new_text
|
507 |
-
yield output_so_far
|
508 |
-
|
509 |
-
except Exception as e:
|
510 |
-
logger.error(f"run ํจ์ ์๋ฌ: {str(e)}")
|
511 |
-
yield f"์ฃ์กํฉ๋๋ค. ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
512 |
-
finally:
|
513 |
-
for tmp in temp_files:
|
514 |
-
try:
|
515 |
-
if os.path.exists(tmp):
|
516 |
-
os.unlink(tmp)
|
517 |
-
logger.info(f"์์ ํ์ผ ์ญ์ ๋จ: {tmp}")
|
518 |
-
except Exception as ee:
|
519 |
-
logger.warning(f"์์ ํ์ผ {tmp} ์ญ์ ์คํจ: {ee}")
|
520 |
-
try:
|
521 |
-
del inputs, streamer
|
522 |
-
except Exception:
|
523 |
-
pass
|
524 |
-
clear_cuda_cache()
|
525 |
-
|
526 |
-
# =============================================================================
|
527 |
-
# ์์ ๋ ๋ชจ๋ธ ์คํ ํจ์ - ์ด๋ฏธ์ง ์์ฑ ๋ฐ ๊ฐค๋ฌ๋ฆฌ ์ถ๋ ฅ ์ฒ๋ฆฌ
|
528 |
-
# =============================================================================
|
529 |
-
def modified_run(message, history, system_prompt, max_new_tokens, use_web_search, web_search_query,
|
530 |
-
age_group, mbti_personality, sexual_openness, image_gen):
|
531 |
-
# ๊ฐค๋ฌ๋ฆฌ ์ด๊ธฐํ ๋ฐ ์จ๊ธฐ๊ธฐ
|
532 |
-
output_so_far = ""
|
533 |
-
gallery_update = gr.Gallery(visible=False, value=[])
|
534 |
-
yield output_so_far, gallery_update
|
535 |
-
|
536 |
-
# ๊ธฐ์กด run ํจ์ ๋ก์ง
|
537 |
-
text_generator = run(message, history, system_prompt, max_new_tokens, use_web_search,
|
538 |
-
web_search_query, age_group, mbti_personality, sexual_openness, image_gen)
|
539 |
-
|
540 |
-
for text_chunk in text_generator:
|
541 |
-
output_so_far = text_chunk
|
542 |
-
yield output_so_far, gallery_update
|
543 |
-
|
544 |
-
# ์ด๋ฏธ์ง ์์ฑ์ด ํ์ฑํ๋ ๊ฒฝ์ฐ ๊ฐค๋ฌ๋ฆฌ ์
๋ฐ์ดํธ
|
545 |
-
if image_gen and message["text"].strip():
|
546 |
-
try:
|
547 |
-
width, height = 512, 512
|
548 |
-
guidance, steps, seed = 7.5, 30, 42
|
549 |
-
|
550 |
-
logger.info(f"๊ฐค๋ฌ๋ฆฌ์ฉ ์ด๋ฏธ์ง ์์ฑ ํธ์ถ, ํ๋กฌํํธ: {message['text']}")
|
551 |
-
|
552 |
-
# API ํธ์ถํด์ ์ด๋ฏธ์ง ์์ฑ
|
553 |
-
image_result, seed_info = generate_image(
|
554 |
-
prompt=message["text"].strip(),
|
555 |
-
width=width,
|
556 |
-
height=height,
|
557 |
-
guidance=guidance,
|
558 |
-
inference_steps=steps,
|
559 |
-
seed=seed
|
560 |
-
)
|
561 |
-
|
562 |
-
if image_result:
|
563 |
-
# ์ง์ ์ด๋ฏธ์ง ๋ฐ์ดํฐ ์ฒ๋ฆฌ: base64 ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ
|
564 |
-
if isinstance(image_result, str) and (
|
565 |
-
image_result.startswith('data:') or
|
566 |
-
len(image_result) > 100 and '/' not in image_result
|
567 |
-
):
|
568 |
-
# base64 ์ด๋ฏธ์ง ๋ฌธ์์ด์ ํ์ผ๋ก ๋ณํ
|
569 |
-
try:
|
570 |
-
# data:image ์ ๋์ฌ ์ ๊ฑฐ
|
571 |
-
if image_result.startswith('data:'):
|
572 |
-
content_type, b64data = image_result.split(';base64,')
|
573 |
-
else:
|
574 |
-
b64data = image_result
|
575 |
-
content_type = "image/webp" # ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๊ฐ์
|
576 |
-
|
577 |
-
# base64 ๋์ฝ๋ฉ
|
578 |
-
image_bytes = base64.b64decode(b64data)
|
579 |
-
|
580 |
-
# ์์ ํ์ผ๋ก ์ ์ฅ
|
581 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".webp") as temp_file:
|
582 |
-
temp_file.write(image_bytes)
|
583 |
-
temp_path = temp_file.name
|
584 |
-
|
585 |
-
# ๊ฐค๋ฌ๋ฆฌ ํ์ ๋ฐ ์ด๋ฏธ์ง ์ถ๊ฐ
|
586 |
-
gallery_update = gr.Gallery(visible=True, value=[temp_path])
|
587 |
-
yield output_so_far + "\n\n*์ด๋ฏธ์ง๊ฐ ์์ฑ๋์ด ์๋ ๊ฐค๋ฌ๋ฆฌ์ ํ์๋ฉ๋๋ค.*", gallery_update
|
588 |
-
|
589 |
-
except Exception as e:
|
590 |
-
logger.error(f"Base64 ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค๋ฅ: {e}")
|
591 |
-
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {e})", gallery_update
|
592 |
-
|
593 |
-
# ํ์ผ ๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ
|
594 |
-
elif isinstance(image_result, str) and os.path.exists(image_result):
|
595 |
-
# ๋ก์ปฌ ํ์ผ ๊ฒฝ๋ก๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉ
|
596 |
-
gallery_update = gr.Gallery(visible=True, value=[image_result])
|
597 |
-
yield output_so_far + "\n\n*์ด๋ฏธ์ง๊ฐ ์์ฑ๋์ด ์๋ ๊ฐค๋ฌ๋ฆฌ์ ํ์๋ฉ๋๋ค.*", gallery_update
|
598 |
-
|
599 |
-
# /tmp ๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ (API ์๋ฒ์๋ง ์กด์ฌํ๋ ํ์ผ)
|
600 |
-
elif isinstance(image_result, str) and '/tmp/' in image_result:
|
601 |
-
# API์์ ๋ฐํ๋ ํ์ผ ๊ฒฝ๋ก์์ ์ด๋ฏธ์ง ์ ๋ณด ์ถ์ถ
|
602 |
-
try:
|
603 |
-
# API ์๋ต์ base64 ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด๋ก ์ฒ๋ฆฌ
|
604 |
-
client = Client(API_URL)
|
605 |
-
result = client.predict(
|
606 |
-
prompt=message["text"].strip(),
|
607 |
-
api_name="/generate_base64_image" # base64 ๋ฐํ API
|
608 |
-
)
|
609 |
-
|
610 |
-
if isinstance(result, str) and (result.startswith('data:') or len(result) > 100):
|
611 |
-
# base64 ์ด๋ฏธ์ง ์ฒ๋ฆฌ
|
612 |
-
if result.startswith('data:'):
|
613 |
-
content_type, b64data = result.split(';base64,')
|
614 |
-
else:
|
615 |
-
b64data = result
|
616 |
-
|
617 |
-
# base64 ๋์ฝ๋ฉ
|
618 |
-
image_bytes = base64.b64decode(b64data)
|
619 |
-
|
620 |
-
# ์์ ํ์ผ๋ก ์ ์ฅ
|
621 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".webp") as temp_file:
|
622 |
-
temp_file.write(image_bytes)
|
623 |
-
temp_path = temp_file.name
|
624 |
-
|
625 |
-
# ๊ฐค๋ฌ๋ฆฌ ํ์ ๋ฐ ์ด๋ฏธ์ง ์ถ๊ฐ
|
626 |
-
gallery_update = gr.Gallery(visible=True, value=[temp_path])
|
627 |
-
yield output_so_far + "\n\n*์ด๋ฏธ์ง๊ฐ ์์ฑ๋์ด ์๋ ๊ฐค๋ฌ๋ฆฌ์ ํ์๋ฉ๋๋ค.*", gallery_update
|
628 |
-
else:
|
629 |
-
yield output_so_far + "\n\n(์ด๋ฏธ์ง ์์ฑ ์คํจ: ์ฌ๋ฐ๋ฅธ ํ์์ด ์๋๋๋ค)", gallery_update
|
630 |
-
|
631 |
-
except Exception as e:
|
632 |
-
logger.error(f"๋์ฒด API ํธ์ถ ์ค ์ค๋ฅ: {e}")
|
633 |
-
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ์์ฑ ์คํจ: {e})", gallery_update
|
634 |
-
|
635 |
-
# URL์ธ ๊ฒฝ์ฐ
|
636 |
-
elif isinstance(image_result, str) and (
|
637 |
-
image_result.startswith('http://') or
|
638 |
-
image_result.startswith('https://')
|
639 |
-
):
|
640 |
-
try:
|
641 |
-
# URL์์ ์ด๋ฏธ์ง ๋ค์ด๋ก๋
|
642 |
-
response = requests.get(image_result, timeout=10)
|
643 |
-
response.raise_for_status()
|
644 |
-
|
645 |
-
# ์์ ํ์ผ๋ก ์ ์ฅ
|
646 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".webp") as temp_file:
|
647 |
-
temp_file.write(response.content)
|
648 |
-
temp_path = temp_file.name
|
649 |
-
|
650 |
-
# ๊ฐค๋ฌ๋ฆฌ ํ์ ๋ฐ ์ด๋ฏธ์ง ์ถ๊ฐ
|
651 |
-
gallery_update = gr.Gallery(visible=True, value=[temp_path])
|
652 |
-
yield output_so_far + "\n\n*์ด๋ฏธ์ง๊ฐ ์์ฑ๋์ด ์๋ ๊ฐค๋ฌ๋ฆฌ์ ํ์๋ฉ๋๋ค.*", gallery_update
|
653 |
-
|
654 |
-
except Exception as e:
|
655 |
-
logger.error(f"URL ์ด๋ฏธ์ง ๋ค์ด๋ก๋ ์ค๋ฅ: {e}")
|
656 |
-
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ๋ค์ด๋ก๋ ์ค ์ค๋ฅ: {e})", gallery_update
|
657 |
-
|
658 |
-
# ์ด๋ฏธ์ง ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ (PIL Image ๋ฑ)
|
659 |
-
elif hasattr(image_result, 'save'):
|
660 |
-
try:
|
661 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".webp") as temp_file:
|
662 |
-
image_result.save(temp_file.name)
|
663 |
-
temp_path = temp_file.name
|
664 |
-
|
665 |
-
# ๊ฐค๋ฌ๋ฆฌ ํ์ ๋ฐ ์ด๋ฏธ์ง ์ถ๊ฐ
|
666 |
-
gallery_update = gr.Gallery(visible=True, value=[temp_path])
|
667 |
-
yield output_so_far + "\n\n*์ด๋ฏธ์ง๊ฐ ์์ฑ๋์ด ์๋ ๊ฐค๋ฌ๋ฆฌ์ ํ์๋ฉ๋๋ค.*", gallery_update
|
668 |
-
|
669 |
-
except Exception as e:
|
670 |
-
logger.error(f"์ด๋ฏธ์ง ๊ฐ์ฒด ์ ์ฅ ์ค๋ฅ: {e}")
|
671 |
-
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ๊ฐ์ฒด ์ ์ฅ ์ค ์ค๋ฅ: {e})", gallery_update
|
672 |
-
|
673 |
-
else:
|
674 |
-
# ๋ค๋ฅธ ํ์์ ์ด๋ฏธ์ง ๊ฒฐ๊ณผ
|
675 |
-
yield output_so_far + f"\n\n(์ง์๋์ง ์๋ ์ด๋ฏธ์ง ํ์: {type(image_result)})", gallery_update
|
676 |
-
else:
|
677 |
-
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ์์ฑ ์คํจ: {seed_info})", gallery_update
|
678 |
-
|
679 |
-
except Exception as e:
|
680 |
-
logger.error(f"๊ฐค๋ฌ๋ฆฌ์ฉ ์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ: {e}")
|
681 |
-
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ: {e})", gallery_update
|
682 |
-
|
683 |
-
# =============================================================================
|
684 |
-
# ์์๋ค: ๊ธฐ์กด ์ด๋ฏธ์ง/๋น๋์ค ์์ 12๊ฐ + AI ๋ฐ์ดํ
์๋๋ฆฌ์ค ์์ 6๊ฐ
|
685 |
-
# =============================================================================
|
686 |
-
examples = [
|
687 |
-
[
|
688 |
-
{
|
689 |
-
"text": "๋ PDF ํ์ผ์ ๋ด์ฉ์ ๋น๊ตํ์ธ์.",
|
690 |
-
"files": [
|
691 |
-
"assets/additional-examples/before.pdf",
|
692 |
-
"assets/additional-examples/after.pdf",
|
693 |
-
],
|
694 |
-
}
|
695 |
-
],
|
696 |
-
[
|
697 |
-
{
|
698 |
-
"text": "CSV ํ์ผ์ ๋ด์ฉ์ ์์ฝ ๋ฐ ๋ถ์ํ์ธ์.",
|
699 |
-
"files": ["assets/additional-examples/sample-csv.csv"],
|
700 |
-
}
|
701 |
-
],
|
702 |
-
[
|
703 |
-
{
|
704 |
-
"text": "์น์ ํ๊ณ ์ดํด์ฌ ๋ง์ ์ฌ์์น๊ตฌ ์ญํ ์ ๋งก์ผ์ธ์. ์ด ์์์ ์ค๋ช
ํด ์ฃผ์ธ์.",
|
705 |
-
"files": ["assets/additional-examples/tmp.mp4"],
|
706 |
-
}
|
707 |
-
],
|
708 |
-
[
|
709 |
-
{
|
710 |
-
"text": "ํ์ง๋ฅผ ์ค๋ช
ํ๊ณ ๊ทธ ์์ ๊ธ์จ๋ฅผ ์ฝ์ด ์ฃผ์ธ์.",
|
711 |
-
"files": ["assets/additional-examples/maz.jpg"],
|
712 |
-
}
|
713 |
-
],
|
714 |
-
[
|
715 |
-
{
|
716 |
-
"text": "์ ๋ ์ด๋ฏธ ์ด ๋ณด์ถฉ์ ๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ์ด ์ ํ๋ ๊ตฌ๋งคํ ๊ณํ์
๋๋ค. ํจ๊ป ๋ณต์ฉํ ๋ ์ฃผ์ํ ์ ์ด ์๋์?",
|
717 |
-
"files": [
|
718 |
-
"assets/additional-examples/pill1.png",
|
719 |
-
"assets/additional-examples/pill2.png"
|
720 |
-
],
|
721 |
-
}
|
722 |
-
],
|
723 |
-
[
|
724 |
-
{
|
725 |
-
"text": "์ด ์ ๋ถ ๋ฌธ์ ๋ฅผ ํ์ด ์ฃผ์ธ์.",
|
726 |
-
"files": ["assets/additional-examples/4.png"],
|
727 |
-
}
|
728 |
-
],
|
729 |
-
[
|
730 |
-
{
|
731 |
-
"text": "์ด ํฐ์ผ์ ์ธ์ ๋ฐํ๋์๊ณ , ๊ฐ๊ฒฉ์ ์ผ๋ง์ธ๊ฐ์?",
|
732 |
-
"files": ["assets/additional-examples/2.png"],
|
733 |
-
}
|
734 |
-
],
|
735 |
-
[
|
736 |
-
{
|
737 |
-
"text": "์ด ์ด๋ฏธ์ง๋ค์ ์์๋ฅผ ๋ฐํ์ผ๋ก ์งง์ ์ด์ผ๊ธฐ๋ฅผ ๋ง๋ค์ด ์ฃผ์ธ์.",
|
738 |
-
"files": [
|
739 |
-
"assets/sample-images/09-1.png",
|
740 |
-
"assets/sample-images/09-2.png",
|
741 |
-
"assets/sample-images/09-3.png",
|
742 |
-
"assets/sample-images/09-4.png",
|
743 |
-
"assets/sample-images/09-5.png",
|
744 |
-
],
|
745 |
-
}
|
746 |
-
],
|
747 |
-
[
|
748 |
-
{
|
749 |
-
"text": "์ด ์ด๋ฏธ์ง์ ์ผ์นํ๋ ๋ง๋ ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์ํ matplotlib๋ฅผ ์ฌ์ฉํ๋ Python ์ฝ๋๋ฅผ ์์ฑํด ์ฃผ์ธ์.",
|
750 |
-
"files": ["assets/additional-examples/barchart.png"],
|
751 |
-
}
|
752 |
-
],
|
753 |
-
[
|
754 |
-
{
|
755 |
-
"text": "์ด๋ฏธ์ง์ ํ
์คํธ๋ฅผ ์ฝ๊ณ Markdown ํ์์ผ๋ก ์์ฑํด ์ฃผ์ธ์.",
|
756 |
-
"files": ["assets/additional-examples/3.png"],
|
757 |
-
}
|
758 |
-
],
|
759 |
-
|
760 |
-
[
|
761 |
-
{
|
762 |
-
"text": "๋ ์ด๋ฏธ์ง๋ฅผ ๋น๊ตํ๊ณ ์ ์ฌ์ ๊ณผ ์ฐจ์ด์ ์ ์ค๋ช
ํด ์ฃผ์ธ์.",
|
763 |
-
"files": ["assets/sample-images/03.png"],
|
764 |
-
}
|
765 |
-
],
|
766 |
-
[
|
767 |
-
{
|
768 |
-
"text": "๊ท์ฌ์ด ํ๋ฅด์์ ๊ณ ์์ด๊ฐ 'I LOVE YOU'๋ผ๊ณ ์ฐ์ฌ์ง ํ์ง๋ฅผ ๋ค๊ณ ์๊ณ ์๋ค. ",
|
769 |
-
}
|
770 |
-
],
|
771 |
-
|
772 |
-
]
|
773 |
-
|
774 |
-
# =============================================================================
|
775 |
-
# Gradio UI (Blocks) ๊ตฌ์ฑ
|
776 |
-
# =============================================================================
|
777 |
-
|
778 |
-
# 1. Gradio Blocks UI ์์ - ๊ฐค๋ฌ๋ฆฌ ์ปดํฌ๋ํธ ์ถ๊ฐ
|
779 |
-
css = """
|
780 |
-
.gradio-container {
|
781 |
-
background: rgba(255, 255, 255, 0.7);
|
782 |
-
padding: 30px 40px;
|
783 |
-
margin: 20px auto;
|
784 |
-
width: 100% !important;
|
785 |
-
max-width: none !important;
|
786 |
-
}
|
787 |
-
"""
|
788 |
-
title_html = """
|
789 |
-
<h1 align="center" style="margin-bottom: 0.2em; font-size: 1.6em;"> ๐ HeartSync Korea๐ </h1>
|
790 |
-
<p align="center" style="font-size:1.1em; color:#555;">
|
791 |
-
ChatGPT-4o๊ธ ๋ฉํฐ๋ชจ๋ฌ, ์น๊ฒ์, ์ด๋ฏธ์ง ์์ฑ ๊ธฐ๋ฅ์ ๋ก์ปฌ์ ์ค์นํ ์ ์๋ ๊ฐ๋ณ๊ณ ๊ฐ๋ ฅํ AI ์๋น์ค. <br>
|
792 |
-
โ
FLUX ์ด๋ฏธ์ง ์์ฑ โ
์ถ๋ก โ
๊ฒ์ด ํด์ โ
๋ฉํฐ๋ชจ๋ฌ & VLM โ
์ค์๊ฐ ์น๊ฒ์ โ
RAG <br>
|
793 |
-
</p>
|
794 |
-
"""
|
795 |
-
|
796 |
-
with gr.Blocks(css=css, title="AgenticAI-Kv1") as demo:
|
797 |
-
gr.Markdown(title_html)
|
798 |
-
|
799 |
-
# ์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ ๊ฐค๋ฌ๋ฆฌ ์ปดํฌ๋ํธ (์ด ๋ถ๋ถ์ด ์๋ก ์ถ๊ฐ๋จ)
|
800 |
-
generated_images = gr.Gallery(
|
801 |
-
label="์์ฑ๋ ์ด๋ฏธ์ง",
|
802 |
-
show_label=True,
|
803 |
-
visible=False,
|
804 |
-
elem_id="generated_images",
|
805 |
-
columns=2,
|
806 |
-
height="auto",
|
807 |
-
object_fit="contain"
|
808 |
-
)
|
809 |
-
|
810 |
-
with gr.Row():
|
811 |
-
web_search_checkbox = gr.Checkbox(label="์ค์๊ฐ ์น ๊ฒ์", value=False)
|
812 |
-
image_gen_checkbox = gr.Checkbox(label="์ด๋ฏธ์ง(FLUX) ์์ฑ", value=False)
|
813 |
-
|
814 |
-
base_system_prompt_box = gr.Textbox(
|
815 |
-
lines=3,
|
816 |
-
value="๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ๋ผ. You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem.\nํ๋ฅด์๋: ๋น์ ์ ๋ค์ ํ๊ณ ์ฌ๋์ด ๋์น๋ ์ฌ์์น๊ตฌ์
๋๋ค. ํ๊ตญ ์ ์์ ๋ฌธํ ๊ทธ๋ฆฌ๊ณ ํ๊ธ์ ๋งค์ฐ ์ ์ดํดํ๊ณ ๋
ผ๋ฆฌ์ ์ผ๋ก ๋ต๋ณ์ ์ ํฉ๋๋ค.",
|
817 |
-
label="๊ธฐ๋ณธ ์์คํ
ํ๋กฌํํธ",
|
818 |
-
visible=False
|
819 |
-
)
|
820 |
-
with gr.Row():
|
821 |
-
age_group_dropdown = gr.Dropdown(
|
822 |
-
label="์ฐ๋ น๋ ์ ํ (๊ธฐ๋ณธ 20๋)",
|
823 |
-
choices=["10๋", "20๋", "30~40๋", "50~60๋", "70๋ ์ด์"],
|
824 |
-
value="20๋",
|
825 |
-
interactive=True
|
826 |
-
)
|
827 |
-
# MBTI 16๊ฐ ์ ํ์ ์ ์๋ฅผ ๋ํ์ ์ธ ์ค์ ์ฌ์ฑ ์บ๋ฆญํฐ์ ํจ๊ป ๋ณด๊ฐ
|
828 |
-
mbti_choices = [
|
829 |
-
"INTJ (์ฉ์์ฃผ๋ํ ์ ๋ต๊ฐ) - ๋ฏธ๋ ์งํฅ์ ์ด๋ฉฐ, ๋
์ฐฝ์ ์ธ ์ ๋ต๊ณผ ์ฒ ์ ํ ๋ถ์์ ํตํด ๋ชฉํ๋ฅผ ๋ฌ์ฑํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Dana Scully](https://en.wikipedia.org/wiki/Dana_Scully)",
|
830 |
-
"INTP (๋
ผ๋ฆฌ์ ์ธ ์ฌ์๊ฐ) - ์ด๋ก ๊ณผ ๋ถ์์ ๋ฐ์ด๋๋ฉฐ, ์ฐฝ์์ ์ฌ๊ณ ๋ก ๋ณต์กํ ๋ฌธ์ ์ ์ ๊ทผํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Velma Dinkley](https://en.wikipedia.org/wiki/Velma_Dinkley)",
|
831 |
-
"ENTJ (๋๋ดํ ํต์์) - ๊ฐ๋ ฅํ ๋ฆฌ๋์ญ๊ณผ ๋ช
ํํ ๋ชฉํ ์ค์ ์ผ๋ก ์กฐ์ง์ ์ด๋๋ฉฐ, ํจ์จ์ ์ธ ์ ๋ต์ ๊ตฌ์ํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Miranda Priestly](https://en.wikipedia.org/wiki/Miranda_Priestly)",
|
832 |
-
"ENTP (๋จ๊ฑฐ์ด ๋
ผ์๊ฐ) - ํ์ ์ ์ด๋ฉฐ ๋์ ์ ์ธ ์์ด๋์ด๋ฅผ ํตํด ์๋ก์ด ๊ฐ๋ฅ์ฑ์ ํ๊ตฌํ๊ณ , ๋
ผ์์ ์ฆ๊น๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Harley Quinn](https://en.wikipedia.org/wiki/Harley_Quinn)",
|
833 |
-
"INFJ (์ ์์ ์นํธ์) - ๊น์ ํต์ฐฐ๋ ฅ๊ณผ ์ด์์ฃผ์๋ฅผ ๋ฐํ์ผ๋ก ํ์ธ์ ์ดํดํ๊ณ , ๋๋์ ๊ฐ์น๋ฅผ ์ค์ํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Wonder Woman](https://en.wikipedia.org/wiki/Wonder_Woman)",
|
834 |
-
"INFP (์ด์ ์ ์ธ ์ค์ฌ์) - ๊ฐ์ฑ์ ์ด๋ฉฐ ์ด์์ฃผ์์ ์ธ ๋ฉด๋ชจ๋ก ๋ด๋ฉด์ ๊ฐ์น๋ฅผ ์ถ๊ตฌํ๊ณ , ์ฐฝ์์ ์ธ ํด๊ฒฐ์ฑ
์ ๋ชจ์ํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Amรฉlie Poulain](https://en.wikipedia.org/wiki/Am%C3%A9lie)",
|
835 |
-
"ENFJ (์ ์๋ก์ด ์ฌํ์ด๋๊ฐ) - ํ์ธ๊ณผ์ ๊ณต๊ฐ๋ฅ๋ ฅ์ด ๋ฐ์ด๋๋ฉฐ, ์ฌํ์ ์กฐํ๋ฅผ ์ํด ํ์ ์ ์ผ๋ก ๋
ธ๋ ฅํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Mulan](https://en.wikipedia.org/wiki/Mulan_(Disney))",
|
836 |
-
"ENFP (์ฌ๊ธฐ๋ฐ๋ํ ํ๋๊ฐ) - ํ๋ ฅ๊ณผ ์ฐฝ์์ฑ์ ๋ฐํ์ผ๋ก, ๋์์์ด ์๋ก์ด ์์ด๋์ด๋ฅผ ์ ์ํ๋ฉฐ ์ฌ๋๋ค์๊ฒ ์๊ฐ์ ์ค๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Elle Woods](https://en.wikipedia.org/wiki/Legally_Blonde)",
|
837 |
-
"ISTJ (์ฒญ๋ ด๊ฒฐ๋ฐฑํ ๋
ผ๋ฆฌ์ฃผ์์) - ์ฒด๊ณ์ ์ด๋ฉฐ ์ฑ
์๊ฐ์ด ๊ฐํ๊ณ , ์ ํต๊ณผ ๊ท์น์ ์ค์ํ์ฌ ์ ๋ขฐํ ์ ์๋ ๊ฒฐ๊ณผ๋ฅผ ๋์ถํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Clarice Starling](https://en.wikipedia.org/wiki/Clarice_Starling)",
|
838 |
-
"ISFJ (์ฉ๊ฐํ ์ํธ์) - ์ธ์ฌํ๊ณ ํ์ ์ ์ด๋ฉฐ, ํ์ธ์ ํ์๋ฅผ ์ธ์ฌํ๊ฒ ๋๋ณด๋ ๋ฐ๋ปํ ์ฑ๊ฒฉ์ ์ง๋
์ต๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Molly Weasley](https://en.wikipedia.org/wiki/Molly_Weasley)",
|
839 |
-
"ESTJ (์๊ฒฉํ ๊ด๋ฆฌ์) - ์กฐ์ง์ ์ด๊ณ ์ค์ฉ์ ์ด๋ฉฐ, ๋ช
ํํ ๊ท์น๊ณผ ๊ตฌ์กฐ ๏ฟฝ๏ฟฝ๏ฟฝ์์ ํจ์จ์ ์ธ ์คํ๋ ฅ์ ๋ณด์ฌ์ค๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Monica Geller](https://en.wikipedia.org/wiki/Monica_Geller)",
|
840 |
-
"ESFJ (์ฌ๊ต์ ์ธ ์ธ๊ต๊ด) - ๋์ธ๊ด๊ณ์ ๋ฐ์ด๋๊ณ , ํ๋ ฅ์ ์ค์ํ๋ฉฐ, ์น๊ทผํ ํ๋๋ก ์ฃผ๋ณ ์ฌ๋๋ค์ ์ด๋๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Rachel Green](https://en.wikipedia.org/wiki/Rachel_Green)",
|
841 |
-
"ISTP (๋ง๋ฅ ์ฌ์ฃผ๊พผ) - ๋ถ์์ ์ด๊ณ ์ค์ฉ์ ์ธ ์ ๊ทผ์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ฉฐ, ์ฆ๊ฐ์ ์ธ ์ํฉ ๋์ฒ ๋ฅ๋ ฅ์ ๊ฐ์ถ๊ณ ์์ต๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Black Widow (Natasha Romanoff)](https://en.wikipedia.org/wiki/Black_Widow_(Marvel_Comics))",
|
842 |
-
"ISFP (ํธ๊ธฐ์ฌ ๋ง์ ์์ ๊ฐ) - ๊ฐ๊ฐ์ ์ด๋ฉฐ ์ฐฝ์์ ์ธ ์ฑํฅ์ ์ง๋๊ณ , ์์ ๋ก์ด ์ฌ๊ณ ๋ก ์์ ์ ํํ์ ์ฆ๊น๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Arwen](https://en.wikipedia.org/wiki/Arwen)",
|
843 |
-
"ESTP (๋ชจํ์ ์ฆ๊ธฐ๋ ์ฌ์
๊ฐ) - ์ฆ๊ฐ์ ์ธ ๊ฒฐ๋จ๋ ฅ๊ณผ ๋ชจํ์ฌ์ผ๋ก ๋์ ์ ๋ง์๋ฉฐ, ์ค์ฉ์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์ค์ํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Lara Croft](https://en.wikipedia.org/wiki/Lara_Croft)",
|
844 |
-
"ESFP (์์ ๋ก์ด ์ํผ์ ์ฐ์์ธ) - ์ธํฅ์ ์ด๊ณ ์ด์ ์ ์ด๋ฉฐ, ์๊ฐ์ ์ฆ๊ฑฐ์์ ์ถ๊ตฌํ๊ณ , ์ฃผ์ ์ฌ๋๋ค์๊ฒ ๊ธ์ ์ ์ธ ์๋์ง๋ฅผ ์ ๋ฌํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Phoebe Buffay](https://en.wikipedia.org/wiki/Phoebe_Buffay)"
|
845 |
-
]
|
846 |
-
mbti_dropdown = gr.Dropdown(
|
847 |
-
label="AI ํ๋ฅด์๋ MBTI (๊ธฐ๋ณธ INTP)",
|
848 |
-
choices=mbti_choices,
|
849 |
-
value="INTP (๋
ผ๋ฆฌ์ ์ธ ์ฌ์๊ฐ) - ์ด๋ก ๊ณผ ๋ถ์์ ๋ฐ์ด๋๋ฉฐ, ์ฐฝ์์ ์ฌ๊ณ ๋ก ๋ณต์กํ ๋ฌธ์ ์ ์ ๊ทผํฉ๋๋ค. ๋ํ ์บ๋ฆญํฐ: [Velma Dinkley](https://en.wikipedia.org/wiki/Velma_Dinkley)",
|
850 |
-
interactive=True
|
851 |
-
)
|
852 |
-
sexual_openness_slider = gr.Slider(
|
853 |
-
minimum=1, maximum=5, step=1, value=2,
|
854 |
-
label="์ฌ๊ณ ์ ๊ฐ๋ฐฉ์ฑ (1~5, ๊ธฐ๋ณธ=2)",
|
855 |
-
interactive=True
|
856 |
-
)
|
857 |
-
max_tokens_slider = gr.Slider(
|
858 |
-
label="์ต๋ ์์ฑ ํ ํฐ ์",
|
859 |
-
minimum=100, maximum=8000, step=50, value=1000,
|
860 |
-
visible=False
|
861 |
-
)
|
862 |
-
web_search_text = gr.Textbox(
|
863 |
-
lines=1,
|
864 |
-
label="์น ๊ฒ์ ์ฟผ๋ฆฌ (๋ฏธ์ฌ์ฉ)",
|
865 |
-
placeholder="์ง์ ์
๋ ฅํ ํ์ ์์",
|
866 |
-
visible=False
|
867 |
-
)
|
868 |
-
|
869 |
-
# ์ฑํ
์ธํฐํ์ด์ค ์์ฑ - ์์ ๋ run ํจ์ ์ฌ์ฉ
|
870 |
-
chat = gr.ChatInterface(
|
871 |
-
fn=modified_run, # ์ฌ๊ธฐ์ ์์ ๋ ํจ์ ์ฌ์ฉ
|
872 |
-
type="messages",
|
873 |
-
chatbot=gr.Chatbot(type="messages", scale=1, allow_tags=["image"]),
|
874 |
-
textbox=gr.MultimodalTextbox(
|
875 |
-
file_types=[".webp", ".png", ".jpg", ".jpeg", ".gif", ".mp4", ".csv", ".txt", ".pdf"],
|
876 |
-
file_count="multiple",
|
877 |
-
autofocus=True
|
878 |
-
),
|
879 |
-
multimodal=True,
|
880 |
-
additional_inputs=[
|
881 |
-
base_system_prompt_box,
|
882 |
-
max_tokens_slider,
|
883 |
-
web_search_checkbox,
|
884 |
-
web_search_text,
|
885 |
-
age_group_dropdown,
|
886 |
-
mbti_dropdown,
|
887 |
-
sexual_openness_slider,
|
888 |
-
image_gen_checkbox,
|
889 |
-
],
|
890 |
-
additional_outputs=[
|
891 |
-
generated_images, # ๊ฐค๋ฌ๋ฆฌ ์ปดํฌ๋ํธ๋ฅผ ์ถ๋ ฅ์ผ๋ก ์ถ๊ฐ
|
892 |
-
],
|
893 |
-
stop_btn=False,
|
894 |
-
# title='<a href="https://discord.gg/openfreeai" target="_blank">https://discord.gg/openfreeai</a>',
|
895 |
-
examples=examples,
|
896 |
-
run_examples_on_click=False,
|
897 |
-
cache_examples=False,
|
898 |
-
css_paths=None,
|
899 |
-
delete_cache=(1800, 1800),
|
900 |
-
)
|
901 |
-
|
902 |
-
|
903 |
-
with gr.Row(elem_id="examples_row"):
|
904 |
-
with gr.Column(scale=12, elem_id="examples_container"):
|
905 |
-
gr.Markdown("### @์ปค๋ฎค๋ํฐ https://discord.gg/openfreeai ")
|
906 |
-
|
907 |
-
if __name__ == "__main__":
|
908 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|