Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ import pandas as pd
|
|
| 20 |
# PDF ํ
์คํธ ์ถ์ถ
|
| 21 |
import PyPDF2
|
| 22 |
|
| 23 |
-
MAX_CONTENT_CHARS =
|
| 24 |
|
| 25 |
model_id = os.getenv("MODEL_ID", "google/gemma-3-27b-it")
|
| 26 |
processor = AutoProcessor.from_pretrained(model_id, padding_side="left")
|
|
@@ -43,6 +43,10 @@ def analyze_csv_file(path: str) -> str:
|
|
| 43 |
"""
|
| 44 |
try:
|
| 45 |
df = pd.read_csv(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
df_str = df.to_string()
|
| 47 |
if len(df_str) > MAX_CONTENT_CHARS:
|
| 48 |
df_str = df_str[:MAX_CONTENT_CHARS] + "\n...(truncated)..."
|
|
@@ -73,11 +77,20 @@ def pdf_to_markdown(pdf_path: str) -> str:
|
|
| 73 |
try:
|
| 74 |
with open(pdf_path, "rb") as f:
|
| 75 |
reader = PyPDF2.PdfReader(f)
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
page_text = page.extract_text() or ""
|
| 78 |
page_text = page_text.strip()
|
| 79 |
if page_text:
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
return f"Failed to read PDF ({os.path.basename(pdf_path)}): {str(e)}"
|
| 83 |
|
|
@@ -97,7 +110,7 @@ def count_files_in_new_message(paths: list[str]) -> tuple[int, int]:
|
|
| 97 |
for path in paths:
|
| 98 |
if path.endswith(".mp4"):
|
| 99 |
video_count += 1
|
| 100 |
-
|
| 101 |
image_count += 1
|
| 102 |
return image_count, video_count
|
| 103 |
|
|
@@ -108,10 +121,13 @@ def count_files_in_history(history: list[dict]) -> tuple[int, int]:
|
|
| 108 |
for item in history:
|
| 109 |
if item["role"] != "user" or isinstance(item["content"], str):
|
| 110 |
continue
|
| 111 |
-
if item["content"][
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
return image_count, video_count
|
| 116 |
|
| 117 |
|
|
@@ -123,11 +139,9 @@ def validate_media_constraints(message: dict, history: list[dict]) -> bool:
|
|
| 123 |
- <image> ํ๊ทธ๊ฐ ์์ผ๋ฉด ํ๊ทธ ์์ ์ค์ ์ด๋ฏธ์ง ์ ์ผ์น
|
| 124 |
- CSV, TXT, PDF ๋ฑ์ ์ฌ๊ธฐ์ ์ ํํ์ง ์์
|
| 125 |
"""
|
|
|
|
| 126 |
media_files = []
|
| 127 |
for f in message["files"]:
|
| 128 |
-
# ์ด๋ฏธ์ง: png/jpg/jpeg/gif/webp
|
| 129 |
-
# ๋น๋์ค: mp4
|
| 130 |
-
# cf) PDF, CSV, TXT ๋ฑ์ ์ ์ธ
|
| 131 |
if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE) or f.endswith(".mp4"):
|
| 132 |
media_files.append(f)
|
| 133 |
|
|
@@ -149,9 +163,15 @@ def validate_media_constraints(message: dict, history: list[dict]) -> bool:
|
|
| 149 |
if video_count == 0 and image_count > MAX_NUM_IMAGES:
|
| 150 |
gr.Warning(f"You can upload up to {MAX_NUM_IMAGES} images.")
|
| 151 |
return False
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
return True
|
| 157 |
|
|
@@ -164,7 +184,8 @@ def downsample_video(video_path: str) -> list[tuple[Image.Image, float]]:
|
|
| 164 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
| 165 |
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 166 |
|
| 167 |
-
|
|
|
|
| 168 |
frames = []
|
| 169 |
|
| 170 |
for i in range(0, total_frames, frame_interval):
|
|
@@ -175,6 +196,10 @@ def downsample_video(video_path: str) -> list[tuple[Image.Image, float]]:
|
|
| 175 |
pil_image = Image.fromarray(image)
|
| 176 |
timestamp = round(i / fps, 2)
|
| 177 |
frames.append((pil_image, timestamp))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
vidcap.release()
|
| 180 |
return frames
|
|
@@ -200,9 +225,13 @@ def process_interleaved_images(message: dict) -> list[dict]:
|
|
| 200 |
parts = re.split(r"(<image>)", message["text"])
|
| 201 |
content = []
|
| 202 |
image_index = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
for part in parts:
|
| 204 |
-
if part == "<image>":
|
| 205 |
-
content.append({"type": "image", "url":
|
| 206 |
image_index += 1
|
| 207 |
elif part.strip():
|
| 208 |
content.append({"type": "text", "text": part.strip()})
|
|
@@ -216,13 +245,30 @@ def process_interleaved_images(message: dict) -> list[dict]:
|
|
| 216 |
##################################################
|
| 217 |
# PDF + CSV + TXT + ์ด๋ฏธ์ง/๋น๋์ค
|
| 218 |
##################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
def process_new_user_message(message: dict) -> list[dict]:
|
| 220 |
if not message["files"]:
|
| 221 |
return [{"type": "text", "text": message["text"]}]
|
| 222 |
|
| 223 |
# 1) ํ์ผ ๋ถ๋ฅ
|
| 224 |
-
video_files = [f for f in message["files"] if f
|
| 225 |
-
image_files = [f for f in message["files"] if
|
| 226 |
csv_files = [f for f in message["files"] if f.lower().endswith(".csv")]
|
| 227 |
txt_files = [f for f in message["files"] if f.lower().endswith(".txt")]
|
| 228 |
pdf_files = [f for f in message["files"] if f.lower().endswith(".pdf")]
|
|
@@ -251,9 +297,13 @@ def process_new_user_message(message: dict) -> list[dict]:
|
|
| 251 |
return content_list
|
| 252 |
|
| 253 |
# 7) ์ด๋ฏธ์ง ์ฒ๋ฆฌ
|
| 254 |
-
if "<image>" in message["text"] and image_files:
|
| 255 |
# interleaved
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
else:
|
| 258 |
# ์ผ๋ฐ ์ฌ๋ฌ ์ฅ
|
| 259 |
for img_path in image_files:
|
|
@@ -261,6 +311,7 @@ def process_new_user_message(message: dict) -> list[dict]:
|
|
| 261 |
|
| 262 |
return content_list
|
| 263 |
|
|
|
|
| 264 |
##################################################
|
| 265 |
# history -> LLM ๋ฉ์์ง ๋ณํ
|
| 266 |
##################################################
|
|
@@ -280,9 +331,18 @@ def process_history(history: list[dict]) -> list[dict]:
|
|
| 280 |
content = item["content"]
|
| 281 |
if isinstance(content, str):
|
| 282 |
current_user_content.append({"type": "text", "text": content})
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
return messages
|
| 287 |
|
| 288 |
|
|
@@ -295,55 +355,79 @@ def run(message: dict, history: list[dict], system_prompt: str = "", max_new_tok
|
|
| 295 |
yield ""
|
| 296 |
return
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
-
##################################################
|
| 345 |
-
# ์์๋ค (๊ธฐ์กด)
|
| 346 |
-
##################################################
|
| 347 |
##################################################
|
| 348 |
# ์์๋ค (ํ๊ธํ ๋ฒ์ )
|
| 349 |
##################################################
|
|
@@ -477,7 +561,9 @@ examples = [
|
|
| 477 |
]
|
| 478 |
|
| 479 |
|
| 480 |
-
|
|
|
|
|
|
|
| 481 |
demo = gr.ChatInterface(
|
| 482 |
fn=run,
|
| 483 |
type="messages",
|
|
@@ -512,4 +598,4 @@ demo = gr.ChatInterface(
|
|
| 512 |
)
|
| 513 |
|
| 514 |
if __name__ == "__main__":
|
| 515 |
-
demo.launch()
|
|
|
|
| 20 |
# PDF ํ
์คํธ ์ถ์ถ
|
| 21 |
import PyPDF2
|
| 22 |
|
| 23 |
+
MAX_CONTENT_CHARS = 4000 # ๋๋ฌด ํฐ ํ์ผ์ ๋ง๊ธฐ ์ํด ์ต๋ ํ์ 4000์
|
| 24 |
|
| 25 |
model_id = os.getenv("MODEL_ID", "google/gemma-3-27b-it")
|
| 26 |
processor = AutoProcessor.from_pretrained(model_id, padding_side="left")
|
|
|
|
| 43 |
"""
|
| 44 |
try:
|
| 45 |
df = pd.read_csv(path)
|
| 46 |
+
# ๋ฐ์ดํฐ ํ๋ ์ ํฌ๊ธฐ ์ ํ (ํ/์ด ์๊ฐ ๋ง์ ๊ฒฝ์ฐ)
|
| 47 |
+
if df.shape[0] > 50 or df.shape[1] > 10:
|
| 48 |
+
df = df.iloc[:50, :10]
|
| 49 |
+
|
| 50 |
df_str = df.to_string()
|
| 51 |
if len(df_str) > MAX_CONTENT_CHARS:
|
| 52 |
df_str = df_str[:MAX_CONTENT_CHARS] + "\n...(truncated)..."
|
|
|
|
| 77 |
try:
|
| 78 |
with open(pdf_path, "rb") as f:
|
| 79 |
reader = PyPDF2.PdfReader(f)
|
| 80 |
+
# ์ต๋ 5ํ์ด์ง๋ง ์ฒ๋ฆฌ
|
| 81 |
+
max_pages = min(5, len(reader.pages))
|
| 82 |
+
for page_num in range(max_pages):
|
| 83 |
+
page = reader.pages[page_num]
|
| 84 |
page_text = page.extract_text() or ""
|
| 85 |
page_text = page_text.strip()
|
| 86 |
if page_text:
|
| 87 |
+
# ํ์ด์ง๋ณ ํ
์คํธ๋ ์ ํ
|
| 88 |
+
if len(page_text) > MAX_CONTENT_CHARS // max_pages:
|
| 89 |
+
page_text = page_text[:MAX_CONTENT_CHARS // max_pages] + "...(truncated)"
|
| 90 |
+
text_chunks.append(f"## Page {page_num+1}\n\n{page_text}\n")
|
| 91 |
+
|
| 92 |
+
if len(reader.pages) > max_pages:
|
| 93 |
+
text_chunks.append(f"\n...(Showing {max_pages} of {len(reader.pages)} pages)...")
|
| 94 |
except Exception as e:
|
| 95 |
return f"Failed to read PDF ({os.path.basename(pdf_path)}): {str(e)}"
|
| 96 |
|
|
|
|
| 110 |
for path in paths:
|
| 111 |
if path.endswith(".mp4"):
|
| 112 |
video_count += 1
|
| 113 |
+
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", path, re.IGNORECASE):
|
| 114 |
image_count += 1
|
| 115 |
return image_count, video_count
|
| 116 |
|
|
|
|
| 121 |
for item in history:
|
| 122 |
if item["role"] != "user" or isinstance(item["content"], str):
|
| 123 |
continue
|
| 124 |
+
if isinstance(item["content"], list) and len(item["content"]) > 0:
|
| 125 |
+
file_path = item["content"][0]
|
| 126 |
+
if isinstance(file_path, str):
|
| 127 |
+
if file_path.endswith(".mp4"):
|
| 128 |
+
video_count += 1
|
| 129 |
+
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE):
|
| 130 |
+
image_count += 1
|
| 131 |
return image_count, video_count
|
| 132 |
|
| 133 |
|
|
|
|
| 139 |
- <image> ํ๊ทธ๊ฐ ์์ผ๋ฉด ํ๊ทธ ์์ ์ค์ ์ด๋ฏธ์ง ์ ์ผ์น
|
| 140 |
- CSV, TXT, PDF ๋ฑ์ ์ฌ๊ธฐ์ ์ ํํ์ง ์์
|
| 141 |
"""
|
| 142 |
+
# ์ด๋ฏธ์ง์ ๋น๋์ค ํ์ผ๋ง ํํฐ๋ง
|
| 143 |
media_files = []
|
| 144 |
for f in message["files"]:
|
|
|
|
|
|
|
|
|
|
| 145 |
if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE) or f.endswith(".mp4"):
|
| 146 |
media_files.append(f)
|
| 147 |
|
|
|
|
| 163 |
if video_count == 0 and image_count > MAX_NUM_IMAGES:
|
| 164 |
gr.Warning(f"You can upload up to {MAX_NUM_IMAGES} images.")
|
| 165 |
return False
|
| 166 |
+
|
| 167 |
+
# ์ด๋ฏธ์ง ํ๊ทธ ๊ฒ์ฆ (์ค์ ์ด๋ฏธ์ง ํ์ผ๋ง ๊ณ์ฐ)
|
| 168 |
+
if "<image>" in message["text"]:
|
| 169 |
+
# ์ด๋ฏธ์ง ํ์ผ๋ง ํํฐ๋ง
|
| 170 |
+
image_files = [f for f in message["files"] if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE)]
|
| 171 |
+
image_tag_count = message["text"].count("<image>")
|
| 172 |
+
if image_tag_count != len(image_files):
|
| 173 |
+
gr.Warning("The number of <image> tags in the text does not match the number of image files.")
|
| 174 |
+
return False
|
| 175 |
|
| 176 |
return True
|
| 177 |
|
|
|
|
| 184 |
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
| 185 |
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 186 |
|
| 187 |
+
# ๋ ์ ์ ํ๋ ์์ ์ถ์ถํ๋๋ก ์กฐ์
|
| 188 |
+
frame_interval = max(int(fps), int(total_frames / 10)) # ์ด๋น 1ํ๋ ์ ๋๋ ์ต๋ 10ํ๋ ์
|
| 189 |
frames = []
|
| 190 |
|
| 191 |
for i in range(0, total_frames, frame_interval):
|
|
|
|
| 196 |
pil_image = Image.fromarray(image)
|
| 197 |
timestamp = round(i / fps, 2)
|
| 198 |
frames.append((pil_image, timestamp))
|
| 199 |
+
|
| 200 |
+
# ์ต๋ 5ํ๋ ์๋ง ์ฌ์ฉ
|
| 201 |
+
if len(frames) >= 5:
|
| 202 |
+
break
|
| 203 |
|
| 204 |
vidcap.release()
|
| 205 |
return frames
|
|
|
|
| 225 |
parts = re.split(r"(<image>)", message["text"])
|
| 226 |
content = []
|
| 227 |
image_index = 0
|
| 228 |
+
|
| 229 |
+
# ์ด๋ฏธ์ง ํ์ผ๋ง ํํฐ๋ง
|
| 230 |
+
image_files = [f for f in message["files"] if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE)]
|
| 231 |
+
|
| 232 |
for part in parts:
|
| 233 |
+
if part == "<image>" and image_index < len(image_files):
|
| 234 |
+
content.append({"type": "image", "url": image_files[image_index]})
|
| 235 |
image_index += 1
|
| 236 |
elif part.strip():
|
| 237 |
content.append({"type": "text", "text": part.strip()})
|
|
|
|
| 245 |
##################################################
|
| 246 |
# PDF + CSV + TXT + ์ด๋ฏธ์ง/๋น๋์ค
|
| 247 |
##################################################
|
| 248 |
+
def is_image_file(file_path: str) -> bool:
|
| 249 |
+
"""์ด๋ฏธ์ง ํ์ผ์ธ์ง ํ์ธ"""
|
| 250 |
+
return bool(re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE))
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def is_video_file(file_path: str) -> bool:
|
| 254 |
+
"""๋น๋์ค ํ์ผ์ธ์ง ํ์ธ"""
|
| 255 |
+
return file_path.endswith(".mp4")
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def is_document_file(file_path: str) -> bool:
|
| 259 |
+
"""๋ฌธ์ ํ์ผ์ธ์ง ํ์ธ (PDF, CSV, TXT)"""
|
| 260 |
+
return (file_path.lower().endswith(".pdf") or
|
| 261 |
+
file_path.lower().endswith(".csv") or
|
| 262 |
+
file_path.lower().endswith(".txt"))
|
| 263 |
+
|
| 264 |
+
|
| 265 |
def process_new_user_message(message: dict) -> list[dict]:
|
| 266 |
if not message["files"]:
|
| 267 |
return [{"type": "text", "text": message["text"]}]
|
| 268 |
|
| 269 |
# 1) ํ์ผ ๋ถ๋ฅ
|
| 270 |
+
video_files = [f for f in message["files"] if is_video_file(f)]
|
| 271 |
+
image_files = [f for f in message["files"] if is_image_file(f)]
|
| 272 |
csv_files = [f for f in message["files"] if f.lower().endswith(".csv")]
|
| 273 |
txt_files = [f for f in message["files"] if f.lower().endswith(".txt")]
|
| 274 |
pdf_files = [f for f in message["files"] if f.lower().endswith(".pdf")]
|
|
|
|
| 297 |
return content_list
|
| 298 |
|
| 299 |
# 7) ์ด๋ฏธ์ง ์ฒ๋ฆฌ
|
| 300 |
+
if "<image>" in message["text"] and image_files:
|
| 301 |
# interleaved
|
| 302 |
+
interleaved_content = process_interleaved_images({"text": message["text"], "files": image_files})
|
| 303 |
+
# ์๋ณธ content_list ์๋ถ๋ถ(ํ
์คํธ)์ ์ ๊ฑฐํ๊ณ interleaved๋ก ๋์ฒด
|
| 304 |
+
if content_list[0]["type"] == "text":
|
| 305 |
+
content_list = content_list[1:] # ์๋ณธ ํ
์คํธ ์ ๊ฑฐ
|
| 306 |
+
return interleaved_content + content_list # interleaved + ๋๋จธ์ง ๋ฌธ์ ๋ถ์ ๋ด์ฉ
|
| 307 |
else:
|
| 308 |
# ์ผ๋ฐ ์ฌ๋ฌ ์ฅ
|
| 309 |
for img_path in image_files:
|
|
|
|
| 311 |
|
| 312 |
return content_list
|
| 313 |
|
| 314 |
+
|
| 315 |
##################################################
|
| 316 |
# history -> LLM ๋ฉ์์ง ๋ณํ
|
| 317 |
##################################################
|
|
|
|
| 331 |
content = item["content"]
|
| 332 |
if isinstance(content, str):
|
| 333 |
current_user_content.append({"type": "text", "text": content})
|
| 334 |
+
elif isinstance(content, list) and len(content) > 0:
|
| 335 |
+
file_path = content[0]
|
| 336 |
+
if is_image_file(file_path):
|
| 337 |
+
current_user_content.append({"type": "image", "url": file_path})
|
| 338 |
+
else:
|
| 339 |
+
# ๋น์ด๋ฏธ์ง ํ์ผ์ ํ
์คํธ๋ก ์ฒ๋ฆฌ
|
| 340 |
+
current_user_content.append({"type": "text", "text": f"[File: {os.path.basename(file_path)}]"})
|
| 341 |
+
|
| 342 |
+
# ๋ง์ง๋ง ์ฌ์ฉ์ ๋ฉ์์ง๊ฐ ์ฒ๋ฆฌ๋์ง ์์ ๊ฒฝ์ฐ ์ถ๊ฐ
|
| 343 |
+
if current_user_content:
|
| 344 |
+
messages.append({"role": "user", "content": current_user_content})
|
| 345 |
+
|
| 346 |
return messages
|
| 347 |
|
| 348 |
|
|
|
|
| 355 |
yield ""
|
| 356 |
return
|
| 357 |
|
| 358 |
+
try:
|
| 359 |
+
messages = []
|
| 360 |
+
if system_prompt:
|
| 361 |
+
messages.append({"role": "system", "content": [{"type": "text", "text": system_prompt}]})
|
| 362 |
+
messages.extend(process_history(history))
|
| 363 |
+
|
| 364 |
+
# ์ฌ์ฉ์ ๋ฉ์์ง ์ฒ๋ฆฌ
|
| 365 |
+
user_content = process_new_user_message(message)
|
| 366 |
+
|
| 367 |
+
# ํ ํฐ ์๋ฅผ ์ค์ด๊ธฐ ์ํด ๋๋ฌด ๊ธด ํ
์คํธ๋ ์๋ผ๋ด๊ธฐ
|
| 368 |
+
for item in user_content:
|
| 369 |
+
if item["type"] == "text" and len(item["text"]) > MAX_CONTENT_CHARS:
|
| 370 |
+
item["text"] = item["text"][:MAX_CONTENT_CHARS] + "\n...(truncated)..."
|
| 371 |
+
|
| 372 |
+
messages.append({"role": "user", "content": user_content})
|
| 373 |
+
|
| 374 |
+
# ๋ชจ๋ธ ์
๋ ฅ ์์ฑ ์ ์ต์ข
ํ์ธ
|
| 375 |
+
# ์ด๋ฏธ์ง๋ ๋น๋์ค๊ฐ ์๋ ํ์ผ๋ค์ ๋ชจ๋ธ์ "image" ํ์ดํ๋ผ์ธ์ผ๋ก ์ ๋ฌ๋์ง ์๋๋ก ํํฐ๋ง
|
| 376 |
+
for msg in messages:
|
| 377 |
+
if msg["role"] != "user":
|
| 378 |
+
continue
|
| 379 |
+
|
| 380 |
+
filtered_content = []
|
| 381 |
+
for item in msg["content"]:
|
| 382 |
+
if item["type"] == "image":
|
| 383 |
+
if is_image_file(item["url"]):
|
| 384 |
+
filtered_content.append(item)
|
| 385 |
+
else:
|
| 386 |
+
# ์ด๋ฏธ์ง ํ์ผ์ด ์๋ ๊ฒฝ์ฐ ํ
์คํธ๋ก ๋ณํ
|
| 387 |
+
filtered_content.append({
|
| 388 |
+
"type": "text",
|
| 389 |
+
"text": f"[Non-image file: {os.path.basename(item['url'])}]"
|
| 390 |
+
})
|
| 391 |
+
else:
|
| 392 |
+
filtered_content.append(item)
|
| 393 |
+
|
| 394 |
+
msg["content"] = filtered_content
|
| 395 |
+
|
| 396 |
+
# ๋ชจ๋ธ ์
๋ ฅ ์์ฑ
|
| 397 |
+
inputs = processor.apply_chat_template(
|
| 398 |
+
messages,
|
| 399 |
+
add_generation_prompt=True,
|
| 400 |
+
tokenize=True,
|
| 401 |
+
return_dict=True,
|
| 402 |
+
return_tensors="pt",
|
| 403 |
+
).to(device=model.device, dtype=torch.bfloat16)
|
| 404 |
+
|
| 405 |
+
# ํ
์คํธ ์์ฑ ์คํธ๋ฆฌ๋จธ ์ค์
|
| 406 |
+
streamer = TextIteratorStreamer(processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
| 407 |
+
gen_kwargs = dict(
|
| 408 |
+
inputs,
|
| 409 |
+
streamer=streamer,
|
| 410 |
+
max_new_tokens=max_new_tokens,
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
# ๋ณ๋ ์ค๋ ๋์์ ํ
์คํธ ์์ฑ
|
| 414 |
+
t = Thread(target=model.generate, kwargs=gen_kwargs)
|
| 415 |
+
t.start()
|
| 416 |
+
|
| 417 |
+
# ๊ฒฐ๊ณผ ์คํธ๋ฆฌ๋ฐ
|
| 418 |
+
output = ""
|
| 419 |
+
for new_text in streamer:
|
| 420 |
+
output += new_text
|
| 421 |
+
yield output
|
| 422 |
+
|
| 423 |
+
except Exception as e:
|
| 424 |
+
logger.error(f"Error in run: {str(e)}")
|
| 425 |
+
yield f"์ฃ์กํฉ๋๋ค. ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
|
| 430 |
|
|
|
|
|
|
|
|
|
|
| 431 |
##################################################
|
| 432 |
# ์์๋ค (ํ๊ธํ ๋ฒ์ )
|
| 433 |
##################################################
|
|
|
|
| 561 |
]
|
| 562 |
|
| 563 |
|
| 564 |
+
##################################################
|
| 565 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
| 566 |
+
##################################################
|
| 567 |
demo = gr.ChatInterface(
|
| 568 |
fn=run,
|
| 569 |
type="messages",
|
|
|
|
| 598 |
)
|
| 599 |
|
| 600 |
if __name__ == "__main__":
|
| 601 |
+
demo.launch()
|