Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
#!/usr/bin/env
|
| 2 |
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import tempfile
|
| 6 |
-
import gc
|
| 7 |
from collections.abc import Iterator
|
| 8 |
from threading import Thread
|
| 9 |
import json
|
|
@@ -12,17 +12,41 @@ import cv2
|
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
| 14 |
import torch
|
|
|
|
| 15 |
from loguru import logger
|
| 16 |
from PIL import Image
|
| 17 |
from transformers import AutoProcessor, Gemma3ForConditionalGeneration, TextIteratorStreamer
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# CSV/TXT ๋ถ์
|
| 20 |
import pandas as pd
|
| 21 |
# PDF ํ
์คํธ ์ถ์ถ
|
| 22 |
import PyPDF2
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
##############################################################################
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
##############################################################################
|
| 27 |
def clear_cuda_cache():
|
| 28 |
"""CUDA ์บ์๋ฅผ ๋ช
์์ ์ผ๋ก ๋น์๋๋ค."""
|
|
@@ -31,177 +55,117 @@ def clear_cuda_cache():
|
|
| 31 |
gc.collect()
|
| 32 |
|
| 33 |
##############################################################################
|
| 34 |
-
#
|
| 35 |
-
##############################################################################
|
| 36 |
-
SERPHOUSE_API_KEY = os.getenv("SERPHOUSE_API_KEY", "")
|
| 37 |
-
|
| 38 |
-
##############################################################################
|
| 39 |
-
# ๊ฐ๋จํ ํค์๋ ์ถ์ถ ํจ์ (ํ๊ธ + ์ํ๋ฒณ + ์ซ์ + ๊ณต๋ฐฑ ๋ณด์กด)
|
| 40 |
##############################################################################
|
| 41 |
def extract_keywords(text: str, top_k: int = 5) -> str:
|
| 42 |
-
"""
|
| 43 |
-
1) ํ๊ธ(๊ฐ-ํฃ), ์์ด(a-zA-Z), ์ซ์(0-9), ๊ณต๋ฐฑ๋ง ๋จ๊น
|
| 44 |
-
2) ๊ณต๋ฐฑ ๊ธฐ์ค ํ ํฐ ๋ถ๋ฆฌ
|
| 45 |
-
3) ์ต๋ top_k๊ฐ๋ง
|
| 46 |
-
"""
|
| 47 |
text = re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ\s]", "", text)
|
| 48 |
tokens = text.split()
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return " ".join(key_tokens)
|
| 51 |
|
| 52 |
##############################################################################
|
| 53 |
-
#
|
| 54 |
-
# - ์์ 20๊ฐ ๊ฒฐ๊ณผ JSON์ LLM์ ๋๊ธธ ๋ link, snippet ๋ฑ ๋ชจ๋ ํฌํจ
|
| 55 |
##############################################################################
|
| 56 |
def do_web_search(query: str) -> str:
|
| 57 |
-
"""
|
| 58 |
-
์์ 20๊ฐ 'organic' ๊ฒฐ๊ณผ item ์ ์ฒด(์ ๋ชฉ, link, snippet ๋ฑ)๋ฅผ
|
| 59 |
-
JSON ๋ฌธ์์ด ํํ๋ก ๋ฐํ
|
| 60 |
-
"""
|
| 61 |
try:
|
| 62 |
url = "https://api.serphouse.com/serp/live"
|
| 63 |
|
| 64 |
-
# ๊ธฐ๋ณธ GET ๋ฐฉ์์ผ๋ก ํ๋ผ๋ฏธํฐ ๊ฐ์ํํ๊ณ ๊ฒฐ๊ณผ ์๋ฅผ 20๊ฐ๋ก ์ ํ
|
| 65 |
params = {
|
| 66 |
"q": query,
|
| 67 |
"domain": "google.com",
|
| 68 |
-
"serp_type": "web",
|
| 69 |
"device": "desktop",
|
| 70 |
-
"lang": "
|
| 71 |
-
"num": "
|
| 72 |
}
|
| 73 |
|
| 74 |
headers = {
|
| 75 |
"Authorization": f"Bearer {SERPHOUSE_API_KEY}"
|
| 76 |
}
|
| 77 |
|
| 78 |
-
logger.info(f"
|
| 79 |
-
logger.info(f"์์ฒญ URL: {url} - ํ๋ผ๋ฏธํฐ: {params}")
|
| 80 |
|
| 81 |
-
# GET ์์ฒญ ์ํ
|
| 82 |
response = requests.get(url, headers=headers, params=params, timeout=60)
|
| 83 |
response.raise_for_status()
|
| 84 |
|
| 85 |
-
logger.info(f"SerpHouse API ์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
| 86 |
data = response.json()
|
| 87 |
|
| 88 |
-
# ๋ค์ํ ์๋ต ๊ตฌ์กฐ ์ฒ๋ฆฌ
|
| 89 |
results = data.get("results", {})
|
| 90 |
-
organic =
|
| 91 |
-
|
| 92 |
-
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 1
|
| 93 |
-
if isinstance(results, dict) and "organic" in results:
|
| 94 |
-
organic = results["organic"]
|
| 95 |
-
|
| 96 |
-
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 2 (์ค์ฒฉ๋ results)
|
| 97 |
-
elif isinstance(results, dict) and "results" in results:
|
| 98 |
-
if isinstance(results["results"], dict) and "organic" in results["results"]:
|
| 99 |
-
organic = results["results"]["organic"]
|
| 100 |
|
| 101 |
-
# ๊ฐ๋ฅํ ์๋ต ๊ตฌ์กฐ 3 (์ต์์ organic)
|
| 102 |
-
elif "organic" in data:
|
| 103 |
-
organic = data["organic"]
|
| 104 |
-
|
| 105 |
if not organic:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
logger.debug(f"results ๊ตฌ์กฐ: {list(results.keys())}")
|
| 110 |
-
return "No web search results found or unexpected API response structure."
|
| 111 |
-
|
| 112 |
-
# ๊ฒฐ๊ณผ ์ ์ ํ ๋ฐ ์ปจํ
์คํธ ๊ธธ์ด ์ต์ ํ
|
| 113 |
-
max_results = min(20, len(organic))
|
| 114 |
limited_organic = organic[:max_results]
|
| 115 |
|
| 116 |
-
# ๊ฒฐ๊ณผ ํ์ ๊ฐ์ - ๋งํฌ๋ค์ด ํ์์ผ๋ก ์ถ๋ ฅํ์ฌ ๊ฐ๋
์ฑ ํฅ์
|
| 117 |
summary_lines = []
|
| 118 |
for idx, item in enumerate(limited_organic, start=1):
|
| 119 |
-
title = item.get("title", "
|
| 120 |
link = item.get("link", "#")
|
| 121 |
-
snippet = item.get("snippet", "
|
| 122 |
displayed_link = item.get("displayed_link", link)
|
| 123 |
|
| 124 |
-
# ๋งํฌ๋ค์ด ํ์ (๋งํฌ ํด๋ฆญ ๊ฐ๋ฅ)
|
| 125 |
summary_lines.append(
|
| 126 |
-
f"###
|
| 127 |
f"{snippet}\n\n"
|
| 128 |
f"**์ถ์ฒ**: [{displayed_link}]({link})\n\n"
|
| 129 |
f"---\n"
|
| 130 |
)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
2. ๋ต๋ณ์ ๊ด๋ จ ์ ๋ณด์ ์ถ์ฒ๋ฅผ ๋ช
์์ ์ผ๋ก ์ธ์ฉํ์ธ์ (์: "X ์ถ์ฒ์ ๋ฐ๋ฅด๋ฉด...")
|
| 138 |
-
3. ์๋ต์ ์ค์ ์ถ์ฒ ๋งํฌ๋ฅผ ํฌํจํ์ธ์
|
| 139 |
-
4. ์ฌ๋ฌ ์ถ์ฒ์ ์ ๋ณด๋ฅผ ์ข
ํฉํ์ฌ ๋ต๋ณํ์ธ์
|
| 140 |
"""
|
| 141 |
|
| 142 |
search_results = instructions + "\n".join(summary_lines)
|
| 143 |
-
logger.info(f"๊ฒ์ ๊ฒฐ๊ณผ {len(limited_organic)}๊ฐ ์ฒ๋ฆฌ ์๋ฃ")
|
| 144 |
return search_results
|
| 145 |
|
| 146 |
except Exception as e:
|
| 147 |
-
logger.error(f"
|
| 148 |
-
return f"
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
##############################################################################
|
| 152 |
-
# ๋ชจ๋ธ/ํ๋ก์ธ์ ๋ก๋ฉ
|
| 153 |
-
##############################################################################
|
| 154 |
-
MAX_CONTENT_CHARS = 2000
|
| 155 |
-
MAX_INPUT_LENGTH = 2096 # ์ต๋ ์
๋ ฅ ํ ํฐ ์ ์ ํ ์ถ๊ฐ
|
| 156 |
-
model_id = os.getenv("MODEL_ID", "VIDraft/Gemma-3-R1984-4B")
|
| 157 |
-
|
| 158 |
-
processor = AutoProcessor.from_pretrained(model_id, padding_side="left")
|
| 159 |
-
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 160 |
-
model_id,
|
| 161 |
-
device_map="auto",
|
| 162 |
-
torch_dtype=torch.bfloat16,
|
| 163 |
-
attn_implementation="eager" # ๊ฐ๋ฅํ๋ค๋ฉด "flash_attention_2"๋ก ๋ณ๊ฒฝ
|
| 164 |
-
)
|
| 165 |
-
MAX_NUM_IMAGES = int(os.getenv("MAX_NUM_IMAGES", "5"))
|
| 166 |
-
|
| 167 |
|
| 168 |
##############################################################################
|
| 169 |
-
#
|
| 170 |
##############################################################################
|
| 171 |
def analyze_csv_file(path: str) -> str:
|
| 172 |
-
"""
|
| 173 |
-
CSV ํ์ผ์ ์ ์ฒด ๋ฌธ์์ด๋ก ๋ณํ. ๋๋ฌด ๊ธธ ๊ฒฝ์ฐ ์ผ๋ถ๋ง ํ์.
|
| 174 |
-
"""
|
| 175 |
try:
|
| 176 |
df = pd.read_csv(path)
|
| 177 |
if df.shape[0] > 50 or df.shape[1] > 10:
|
| 178 |
df = df.iloc[:50, :10]
|
| 179 |
df_str = df.to_string()
|
| 180 |
if len(df_str) > MAX_CONTENT_CHARS:
|
| 181 |
-
df_str = df_str[:MAX_CONTENT_CHARS] + "\n...(
|
| 182 |
-
return f"**[CSV
|
| 183 |
except Exception as e:
|
| 184 |
-
return f"
|
| 185 |
-
|
| 186 |
|
| 187 |
def analyze_txt_file(path: str) -> str:
|
| 188 |
-
"""
|
| 189 |
-
TXT ํ์ผ ์ ๋ฌธ ์ฝ๊ธฐ. ๋๋ฌด ๊ธธ๋ฉด ์ผ๋ถ๋ง ํ์.
|
| 190 |
-
"""
|
| 191 |
try:
|
| 192 |
with open(path, "r", encoding="utf-8") as f:
|
| 193 |
text = f.read()
|
| 194 |
if len(text) > MAX_CONTENT_CHARS:
|
| 195 |
-
text = text[:MAX_CONTENT_CHARS] + "\n...(
|
| 196 |
-
return f"**[TXT
|
| 197 |
except Exception as e:
|
| 198 |
-
return f"
|
| 199 |
-
|
| 200 |
|
| 201 |
def pdf_to_markdown(pdf_path: str) -> str:
|
| 202 |
-
"""
|
| 203 |
-
PDF ํ
์คํธ๋ฅผ Markdown์ผ๋ก ๋ณํ. ํ์ด์ง๋ณ๋ก ๊ฐ๋จํ ํ
์คํธ ์ถ์ถ.
|
| 204 |
-
"""
|
| 205 |
text_chunks = []
|
| 206 |
try:
|
| 207 |
with open(pdf_path, "rb") as f:
|
|
@@ -213,321 +177,226 @@ def pdf_to_markdown(pdf_path: str) -> str:
|
|
| 213 |
page_text = page_text.strip()
|
| 214 |
if page_text:
|
| 215 |
if len(page_text) > MAX_CONTENT_CHARS // max_pages:
|
| 216 |
-
page_text = page_text[:MAX_CONTENT_CHARS // max_pages] + "...(
|
| 217 |
-
text_chunks.append(f"##
|
| 218 |
if len(reader.pages) > max_pages:
|
| 219 |
-
text_chunks.append(f"\n...(
|
| 220 |
except Exception as e:
|
| 221 |
-
return f"
|
| 222 |
|
| 223 |
full_text = "\n".join(text_chunks)
|
| 224 |
if len(full_text) > MAX_CONTENT_CHARS:
|
| 225 |
-
full_text = full_text[:MAX_CONTENT_CHARS] + "\n...(
|
| 226 |
-
|
| 227 |
-
return f"**[PDF File: {os.path.basename(pdf_path)}]**\n\n{full_text}"
|
| 228 |
|
|
|
|
| 229 |
|
| 230 |
##############################################################################
|
| 231 |
-
#
|
| 232 |
##############################################################################
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
for path in paths:
|
| 237 |
-
if path.endswith(".mp4"):
|
| 238 |
-
video_count += 1
|
| 239 |
-
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", path, re.IGNORECASE):
|
| 240 |
-
image_count += 1
|
| 241 |
-
return image_count, video_count
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
def count_files_in_history(history: list[dict]) -> tuple[int, int]:
|
| 245 |
-
image_count = 0
|
| 246 |
-
video_count = 0
|
| 247 |
-
for item in history:
|
| 248 |
-
if item["role"] != "user" or isinstance(item["content"], str):
|
| 249 |
-
continue
|
| 250 |
-
if isinstance(item["content"], list) and len(item["content"]) > 0:
|
| 251 |
-
file_path = item["content"][0]
|
| 252 |
-
if isinstance(file_path, str):
|
| 253 |
-
if file_path.endswith(".mp4"):
|
| 254 |
-
video_count += 1
|
| 255 |
-
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE):
|
| 256 |
-
image_count += 1
|
| 257 |
-
return image_count, video_count
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
def validate_media_constraints(message: dict, history: list[dict]) -> bool:
|
| 261 |
-
media_files = []
|
| 262 |
-
for f in message["files"]:
|
| 263 |
-
if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE) or f.endswith(".mp4"):
|
| 264 |
-
media_files.append(f)
|
| 265 |
-
|
| 266 |
-
new_image_count, new_video_count = count_files_in_new_message(media_files)
|
| 267 |
-
history_image_count, history_video_count = count_files_in_history(history)
|
| 268 |
-
image_count = history_image_count + new_image_count
|
| 269 |
-
video_count = history_video_count + new_video_count
|
| 270 |
-
|
| 271 |
-
if video_count > 1:
|
| 272 |
-
gr.Warning("Only one video is supported.")
|
| 273 |
-
return False
|
| 274 |
-
if video_count == 1:
|
| 275 |
-
if image_count > 0:
|
| 276 |
-
gr.Warning("Mixing images and videos is not allowed.")
|
| 277 |
-
return False
|
| 278 |
-
if "<image>" in message["text"]:
|
| 279 |
-
gr.Warning("Using <image> tags with video files is not supported.")
|
| 280 |
-
return False
|
| 281 |
-
if video_count == 0 and image_count > MAX_NUM_IMAGES:
|
| 282 |
-
gr.Warning(f"You can upload up to {MAX_NUM_IMAGES} images.")
|
| 283 |
-
return False
|
| 284 |
-
|
| 285 |
-
if "<image>" in message["text"]:
|
| 286 |
-
image_files = [f for f in message["files"] if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE)]
|
| 287 |
-
image_tag_count = message["text"].count("<image>")
|
| 288 |
-
if image_tag_count != len(image_files):
|
| 289 |
-
gr.Warning("The number of <image> tags in the text does not match the number of image files.")
|
| 290 |
-
return False
|
| 291 |
-
|
| 292 |
-
return True
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
##############################################################################
|
| 296 |
-
# ๋น๋์ค ์ฒ๋ฆฌ - ์์ ํ์ผ ์ถ์ ์ฝ๋ ์ถ๊ฐ
|
| 297 |
-
##############################################################################
|
| 298 |
-
def downsample_video(video_path: str) -> list[tuple[Image.Image, float]]:
|
| 299 |
-
vidcap = cv2.VideoCapture(video_path)
|
| 300 |
-
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
| 301 |
-
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 302 |
-
frame_interval = max(int(fps), int(total_frames / 10))
|
| 303 |
-
frames = []
|
| 304 |
-
|
| 305 |
-
for i in range(0, total_frames, frame_interval):
|
| 306 |
-
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 307 |
-
success, image = vidcap.read()
|
| 308 |
-
if success:
|
| 309 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 310 |
-
# ์ด๋ฏธ์ง ํฌ๊ธฐ ์ค์ด๊ธฐ ์ถ๊ฐ
|
| 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 |
-
|
| 318 |
-
vidcap.release()
|
| 319 |
-
return frames
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
def process_video(video_path: str) -> tuple[list[dict], list[str]]:
|
| 323 |
-
content = []
|
| 324 |
-
temp_files = [] # ์์ ํ์ผ ์ถ์ ์ ์ํ ๋ฆฌ์คํธ
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
| 330 |
-
pil_image.save(temp_file.name)
|
| 331 |
-
temp_files.append(temp_file.name) # ์ถ์ ์ ์ํด ๊ฒฝ๋ก ์ ์ฅ
|
| 332 |
-
content.append({"type": "text", "text": f"Frame {timestamp}:"})
|
| 333 |
-
content.append({"type": "image", "url": temp_file.name})
|
| 334 |
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
##############################################################################
|
| 339 |
-
#
|
| 340 |
##############################################################################
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
-
|
| 349 |
-
if
|
| 350 |
-
|
| 351 |
-
image_index += 1
|
| 352 |
-
elif part.strip():
|
| 353 |
-
content.append({"type": "text", "text": part.strip()})
|
| 354 |
-
else:
|
| 355 |
-
if isinstance(part, str) and part != "<image>":
|
| 356 |
-
content.append({"type": "text", "text": part})
|
| 357 |
-
return content
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
##############################################################################
|
| 361 |
-
# PDF + CSV + TXT + ์ด๋ฏธ์ง/๋น๋์ค
|
| 362 |
-
##############################################################################
|
| 363 |
-
def is_image_file(file_path: str) -> bool:
|
| 364 |
-
return bool(re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE))
|
| 365 |
-
|
| 366 |
-
def is_video_file(file_path: str) -> bool:
|
| 367 |
-
return file_path.endswith(".mp4")
|
| 368 |
-
|
| 369 |
-
def is_document_file(file_path: str) -> bool:
|
| 370 |
-
return (
|
| 371 |
-
file_path.lower().endswith(".pdf")
|
| 372 |
-
or file_path.lower().endswith(".csv")
|
| 373 |
-
or file_path.lower().endswith(".txt")
|
| 374 |
-
)
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
def process_new_user_message(message: dict) -> tuple[list[dict], list[str]]:
|
| 378 |
-
temp_files = [] # ์์ ํ์ผ ์ถ์ ์ฉ ๋ฆฌ์คํธ
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
image_files = [f for f in message["files"] if is_image_file(f)]
|
| 385 |
-
csv_files = [f for f in message["files"] if f.lower().endswith(".csv")]
|
| 386 |
-
txt_files = [f for f in message["files"] if f.lower().endswith(".txt")]
|
| 387 |
-
pdf_files = [f for f in message["files"] if f.lower().endswith(".pdf")]
|
| 388 |
-
|
| 389 |
-
content_list = [{"type": "text", "text": message["text"]}]
|
| 390 |
-
|
| 391 |
-
for csv_path in csv_files:
|
| 392 |
-
csv_analysis = analyze_csv_file(csv_path)
|
| 393 |
-
content_list.append({"type": "text", "text": csv_analysis})
|
| 394 |
-
|
| 395 |
-
for txt_path in txt_files:
|
| 396 |
-
txt_analysis = analyze_txt_file(txt_path)
|
| 397 |
-
content_list.append({"type": "text", "text": txt_analysis})
|
| 398 |
-
|
| 399 |
-
for pdf_path in pdf_files:
|
| 400 |
-
pdf_markdown = pdf_to_markdown(pdf_path)
|
| 401 |
-
content_list.append({"type": "text", "text": pdf_markdown})
|
| 402 |
-
|
| 403 |
-
if video_files:
|
| 404 |
-
video_content, video_temp_files = process_video(video_files[0])
|
| 405 |
-
content_list += video_content
|
| 406 |
-
temp_files.extend(video_temp_files)
|
| 407 |
-
return content_list, temp_files
|
| 408 |
-
|
| 409 |
-
if "<image>" in message["text"] and image_files:
|
| 410 |
-
interleaved_content = process_interleaved_images({"text": message["text"], "files": image_files})
|
| 411 |
-
if content_list and content_list[0]["type"] == "text":
|
| 412 |
-
content_list = content_list[1:]
|
| 413 |
-
return interleaved_content + content_list, temp_files
|
| 414 |
-
else:
|
| 415 |
-
for img_path in image_files:
|
| 416 |
-
content_list.append({"type": "image", "url": img_path})
|
| 417 |
-
|
| 418 |
-
return content_list, temp_files
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
##############################################################################
|
| 422 |
-
# history -> LLM ๋ฉ์์ง ๋ณํ
|
| 423 |
-
##############################################################################
|
| 424 |
-
def process_history(history: list[dict]) -> list[dict]:
|
| 425 |
-
messages = []
|
| 426 |
-
current_user_content: list[dict] = []
|
| 427 |
-
for item in history:
|
| 428 |
-
if item["role"] == "assistant":
|
| 429 |
-
if current_user_content:
|
| 430 |
-
messages.append({"role": "user", "content": current_user_content})
|
| 431 |
-
current_user_content = []
|
| 432 |
-
messages.append({"role": "assistant", "content": [{"type": "text", "text": item["content"]}]})
|
| 433 |
-
else:
|
| 434 |
-
content = item["content"]
|
| 435 |
-
if isinstance(content, str):
|
| 436 |
-
current_user_content.append({"type": "text", "text": content})
|
| 437 |
-
elif isinstance(content, list) and len(content) > 0:
|
| 438 |
-
file_path = content[0]
|
| 439 |
-
if is_image_file(file_path):
|
| 440 |
-
current_user_content.append({"type": "image", "url": file_path})
|
| 441 |
-
else:
|
| 442 |
-
current_user_content.append({"type": "text", "text": f"[File: {os.path.basename(file_path)}]"})
|
| 443 |
-
|
| 444 |
-
if current_user_content:
|
| 445 |
-
messages.append({"role": "user", "content": current_user_content})
|
| 446 |
|
| 447 |
-
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
|
| 450 |
##############################################################################
|
| 451 |
-
#
|
| 452 |
##############################################################################
|
| 453 |
def _model_gen_with_oom_catch(**kwargs):
|
| 454 |
-
"""
|
| 455 |
-
|
| 456 |
-
"""
|
| 457 |
try:
|
| 458 |
model.generate(**kwargs)
|
| 459 |
except torch.cuda.OutOfMemoryError:
|
| 460 |
-
raise RuntimeError(
|
| 461 |
-
"[OutOfMemoryError] GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํฉ๋๋ค. "
|
| 462 |
-
"Max New Tokens์ ์ค์ด๊ฑฐ๋, ํ๋กฌํํธ ๊ธธ์ด๋ฅผ ์ค์ฌ์ฃผ์ธ์."
|
| 463 |
-
)
|
| 464 |
finally:
|
| 465 |
-
# ์์ฑ ์๋ฃ ํ ํ๋ฒ ๋ ์บ์ ๋น์ฐ๊ธฐ
|
| 466 |
clear_cuda_cache()
|
| 467 |
|
| 468 |
-
|
| 469 |
-
##############################################################################
|
| 470 |
-
# ๋ฉ์ธ ์ถ๋ก ํจ์ (web search ์ฒดํฌ ์ ์๋ ํค์๋์ถ์ถ->๊ฒ์->๊ฒฐ๊ณผ system msg)
|
| 471 |
-
##############################################################################
|
| 472 |
@spaces.GPU(duration=120)
|
| 473 |
-
def
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
system_prompt: str = "",
|
| 477 |
-
max_new_tokens: int = 512,
|
| 478 |
use_web_search: bool = False,
|
| 479 |
-
|
| 480 |
) -> Iterator[str]:
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
|
|
|
| 487 |
|
| 488 |
try:
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
combined_system_msg += f"[System Prompt]\n{system_prompt.strip()}\n\n"
|
| 494 |
-
|
| 495 |
if use_web_search:
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
"""
|
| 511 |
else:
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
|
|
|
| 517 |
"role": "system",
|
| 518 |
-
"content": [{"type": "text", "text":
|
| 519 |
-
}
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
|
|
|
|
|
|
| 525 |
|
| 526 |
-
|
| 527 |
-
if item["type"] == "text" and len(item["text"]) > MAX_CONTENT_CHARS:
|
| 528 |
-
item["text"] = item["text"][:MAX_CONTENT_CHARS] + "\n...(truncated)..."
|
| 529 |
-
messages.append({"role": "user", "content": user_content})
|
| 530 |
-
|
| 531 |
inputs = processor.apply_chat_template(
|
| 532 |
messages,
|
| 533 |
add_generation_prompt=True,
|
|
@@ -536,314 +405,337 @@ def run(
|
|
| 536 |
return_tensors="pt",
|
| 537 |
).to(device=model.device, dtype=torch.bfloat16)
|
| 538 |
|
| 539 |
-
#
|
| 540 |
-
if inputs.input_ids.shape[1] > MAX_INPUT_LENGTH:
|
| 541 |
-
inputs.input_ids = inputs.input_ids[:, -MAX_INPUT_LENGTH:]
|
| 542 |
-
if 'attention_mask' in inputs:
|
| 543 |
-
inputs.attention_mask = inputs.attention_mask[:, -MAX_INPUT_LENGTH:]
|
| 544 |
-
|
| 545 |
streamer = TextIteratorStreamer(processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
| 546 |
gen_kwargs = dict(
|
| 547 |
inputs,
|
| 548 |
streamer=streamer,
|
| 549 |
max_new_tokens=max_new_tokens,
|
|
|
|
|
|
|
| 550 |
)
|
| 551 |
-
|
|
|
|
| 552 |
t = Thread(target=_model_gen_with_oom_catch, kwargs=gen_kwargs)
|
| 553 |
t.start()
|
| 554 |
-
|
|
|
|
| 555 |
output = ""
|
| 556 |
for new_text in streamer:
|
| 557 |
output += new_text
|
| 558 |
yield output
|
| 559 |
-
|
| 560 |
except Exception as e:
|
| 561 |
-
logger.error(f"
|
| 562 |
-
yield f"
|
| 563 |
-
|
| 564 |
finally:
|
| 565 |
-
# ์์ ํ์ผ ์ญ์
|
| 566 |
-
for temp_file in temp_files:
|
| 567 |
-
try:
|
| 568 |
-
if os.path.exists(temp_file):
|
| 569 |
-
os.unlink(temp_file)
|
| 570 |
-
logger.info(f"Deleted temp file: {temp_file}")
|
| 571 |
-
except Exception as e:
|
| 572 |
-
logger.warning(f"Failed to delete temp file {temp_file}: {e}")
|
| 573 |
-
|
| 574 |
-
# ๋ช
์์ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
| 575 |
-
try:
|
| 576 |
-
del inputs, streamer
|
| 577 |
-
except:
|
| 578 |
-
pass
|
| 579 |
-
|
| 580 |
clear_cuda_cache()
|
| 581 |
|
| 582 |
-
|
| 583 |
-
|
| 584 |
##############################################################################
|
| 585 |
-
#
|
| 586 |
-
##############################################################################
|
| 587 |
-
examples = [
|
| 588 |
-
[
|
| 589 |
-
{
|
| 590 |
-
"text": "Compare the contents of the two PDF files.",
|
| 591 |
-
"files": [
|
| 592 |
-
"assets/additional-examples/before.pdf",
|
| 593 |
-
"assets/additional-examples/after.pdf",
|
| 594 |
-
],
|
| 595 |
-
}
|
| 596 |
-
],
|
| 597 |
-
[
|
| 598 |
-
{
|
| 599 |
-
"text": "Summarize and analyze the contents of the CSV file.",
|
| 600 |
-
"files": ["assets/additional-examples/sample-csv.csv"],
|
| 601 |
-
}
|
| 602 |
-
],
|
| 603 |
-
[
|
| 604 |
-
{
|
| 605 |
-
"text": "Assume the role of a friendly and understanding girlfriend. Describe this video.",
|
| 606 |
-
"files": ["assets/additional-examples/tmp.mp4"],
|
| 607 |
-
}
|
| 608 |
-
],
|
| 609 |
-
[
|
| 610 |
-
{
|
| 611 |
-
"text": "Describe the cover and read the text on it.",
|
| 612 |
-
"files": ["assets/additional-examples/maz.jpg"],
|
| 613 |
-
}
|
| 614 |
-
],
|
| 615 |
-
[
|
| 616 |
-
{
|
| 617 |
-
"text": "I already have this supplement <image> and I plan to buy this product <image>. Are there any precautions when taking them together?",
|
| 618 |
-
"files": ["assets/additional-examples/pill1.png", "assets/additional-examples/pill2.png"],
|
| 619 |
-
}
|
| 620 |
-
],
|
| 621 |
-
[
|
| 622 |
-
{
|
| 623 |
-
"text": "Solve this integral.",
|
| 624 |
-
"files": ["assets/additional-examples/4.png"],
|
| 625 |
-
}
|
| 626 |
-
],
|
| 627 |
-
[
|
| 628 |
-
{
|
| 629 |
-
"text": "When was this ticket issued, and what is its price?",
|
| 630 |
-
"files": ["assets/additional-examples/2.png"],
|
| 631 |
-
}
|
| 632 |
-
],
|
| 633 |
-
[
|
| 634 |
-
{
|
| 635 |
-
"text": "Based on the sequence of these images, create a short story.",
|
| 636 |
-
"files": [
|
| 637 |
-
"assets/sample-images/09-1.png",
|
| 638 |
-
"assets/sample-images/09-2.png",
|
| 639 |
-
"assets/sample-images/09-3.png",
|
| 640 |
-
"assets/sample-images/09-4.png",
|
| 641 |
-
"assets/sample-images/09-5.png",
|
| 642 |
-
],
|
| 643 |
-
}
|
| 644 |
-
],
|
| 645 |
-
[
|
| 646 |
-
{
|
| 647 |
-
"text": "Write Python code using matplotlib to plot a bar chart that matches this image.",
|
| 648 |
-
"files": ["assets/additional-examples/barchart.png"],
|
| 649 |
-
}
|
| 650 |
-
],
|
| 651 |
-
[
|
| 652 |
-
{
|
| 653 |
-
"text": "Read the text in the image and write it out in Markdown format.",
|
| 654 |
-
"files": ["assets/additional-examples/3.png"],
|
| 655 |
-
}
|
| 656 |
-
],
|
| 657 |
-
[
|
| 658 |
-
{
|
| 659 |
-
"text": "What does this sign say?",
|
| 660 |
-
"files": ["assets/sample-images/02.png"],
|
| 661 |
-
}
|
| 662 |
-
],
|
| 663 |
-
[
|
| 664 |
-
{
|
| 665 |
-
"text": "Compare the two images and describe their similarities and differences.",
|
| 666 |
-
"files": ["assets/sample-images/03.png"],
|
| 667 |
-
}
|
| 668 |
-
],
|
| 669 |
-
]
|
| 670 |
-
|
| 671 |
-
##############################################################################
|
| 672 |
-
# Gradio UI (Blocks) ๊ตฌ์ฑ (์ข์ธก ์ฌ์ด๋ ๋ฉ๋ด ์์ด ์ ์ฒดํ๋ฉด ์ฑํ
)
|
| 673 |
##############################################################################
|
| 674 |
css = """
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
background:
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
|
|
|
| 682 |
}
|
| 683 |
-
.
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
body {
|
| 689 |
-
background: transparent; /* ์์ ํฌ๋ช
๋ฐฐ๊ฒฝ */
|
| 690 |
-
margin: 0;
|
| 691 |
-
padding: 0;
|
| 692 |
-
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
| 693 |
-
color: #333;
|
| 694 |
-
}
|
| 695 |
-
/* ๋ฒํผ ์์ ์์ ํ ์ ๊ฑฐํ๊ณ ํฌ๋ช
ํ๊ฒ */
|
| 696 |
-
button, .btn {
|
| 697 |
-
background: transparent !important; /* ์์ ์์ ํ ์ ๊ฑฐ */
|
| 698 |
-
border: 1px solid #ddd; /* ๊ฒฝ๊ณ์ ๋ง ์ด์ง ์ถ๊ฐ */
|
| 699 |
-
color: #333;
|
| 700 |
-
padding: 12px 24px;
|
| 701 |
-
text-transform: uppercase;
|
| 702 |
font-weight: bold;
|
| 703 |
-
letter-spacing: 1px;
|
| 704 |
-
cursor: pointer;
|
| 705 |
-
}
|
| 706 |
-
button:hover, .btn:hover {
|
| 707 |
-
background: rgba(0, 0, 0, 0.05) !important; /* ํธ๋ฒ ์ ์์ฃผ ์ด์ง ์ด๋ก๊ฒ๋ง */
|
| 708 |
}
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
}
|
| 716 |
-
#examples_row, .examples-row {
|
| 717 |
-
justify-content: center;
|
| 718 |
-
background: transparent !important;
|
| 719 |
-
}
|
| 720 |
-
|
| 721 |
-
/* examples ๋ฒํผ ๋ด๋ถ์ ๋ชจ๋ ์์ ์ ๊ฑฐ */
|
| 722 |
-
.gr-samples-table button,
|
| 723 |
-
.gr-samples-table .gr-button,
|
| 724 |
-
.gr-samples-table .gr-sample-btn,
|
| 725 |
-
.gr-examples button,
|
| 726 |
-
.gr-examples .gr-button,
|
| 727 |
-
.gr-examples .gr-sample-btn,
|
| 728 |
-
.examples button,
|
| 729 |
-
.examples .gr-button,
|
| 730 |
-
.examples .gr-sample-btn {
|
| 731 |
-
background: transparent !important;
|
| 732 |
-
border: 1px solid #ddd;
|
| 733 |
-
color: #333;
|
| 734 |
-
}
|
| 735 |
-
|
| 736 |
-
/* examples ๋ฒํผ ํธ๋ฒ ์์๋ ์์ ์๊ฒ */
|
| 737 |
-
.gr-samples-table button:hover,
|
| 738 |
-
.gr-samples-table .gr-button:hover,
|
| 739 |
-
.gr-samples-table .gr-sample-btn:hover,
|
| 740 |
-
.gr-examples button:hover,
|
| 741 |
-
.gr-examples .gr-button:hover,
|
| 742 |
-
.gr-examples .gr-sample-btn:hover,
|
| 743 |
-
.examples button:hover,
|
| 744 |
-
.examples .gr-button:hover,
|
| 745 |
-
.examples .gr-sample-btn:hover {
|
| 746 |
-
background: rgba(0, 0, 0, 0.05) !important;
|
| 747 |
-
}
|
| 748 |
-
|
| 749 |
-
/* ์ฑํ
์ธํฐํ์ด์ค ์์๋ค๋ ํฌ๋ช
ํ๊ฒ */
|
| 750 |
-
.chatbox, .chatbot, .message {
|
| 751 |
-
background: transparent !important;
|
| 752 |
}
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
background: rgba(255, 255, 255, 0.5) !important;
|
| 757 |
}
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
/* ์์ ์น์
์ ๋ชจ๋ ์์์์ ๋ฐฐ๊ฒฝ์ ์ ๊ฑฐ */
|
| 765 |
-
.gr-examples-container, .gr-examples, .gr-sample, .gr-sample-row, .gr-sample-cell {
|
| 766 |
-
background: transparent !important;
|
| 767 |
}
|
| 768 |
"""
|
| 769 |
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
<
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
</
|
| 777 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 778 |
|
|
|
|
| 779 |
|
| 780 |
-
|
| 781 |
-
|
| 782 |
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 787 |
)
|
| 788 |
-
|
| 789 |
-
#
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
)
|
| 795 |
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
step=50,
|
| 801 |
-
value=1000,
|
| 802 |
-
visible=False # hidden from view
|
| 803 |
)
|
| 804 |
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
)
|
| 811 |
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
)
|
| 840 |
-
|
| 841 |
-
# Example section - since examples are already set in ChatInterface, this is for display only
|
| 842 |
-
with gr.Row(elem_id="examples_row"):
|
| 843 |
-
with gr.Column(scale=12, elem_id="examples_container"):
|
| 844 |
-
gr.Markdown("### Example Inputs (click to load)")
|
| 845 |
-
|
| 846 |
|
| 847 |
if __name__ == "__main__":
|
| 848 |
-
|
| 849 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import tempfile
|
| 6 |
+
import gc
|
| 7 |
from collections.abc import Iterator
|
| 8 |
from threading import Thread
|
| 9 |
import json
|
|
|
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
| 14 |
import torch
|
| 15 |
+
import numpy as np
|
| 16 |
from loguru import logger
|
| 17 |
from PIL import Image
|
| 18 |
from transformers import AutoProcessor, Gemma3ForConditionalGeneration, TextIteratorStreamer
|
| 19 |
+
import time
|
| 20 |
+
import warnings
|
| 21 |
+
from typing import Dict, List, Optional, Union
|
| 22 |
|
| 23 |
# CSV/TXT ๋ถ์
|
| 24 |
import pandas as pd
|
| 25 |
# PDF ํ
์คํธ ์ถ์ถ
|
| 26 |
import PyPDF2
|
| 27 |
|
| 28 |
+
warnings.filterwarnings('ignore')
|
| 29 |
+
|
| 30 |
+
print("๐ฎ ๋ก๋ด ์๊ฐ ์์คํ
์ด๊ธฐํ (Gemma3-R1984-4B)...")
|
| 31 |
+
|
| 32 |
+
##############################################################################
|
| 33 |
+
# ์์ ์ ์
|
| 34 |
+
##############################################################################
|
| 35 |
+
MAX_CONTENT_CHARS = 2000
|
| 36 |
+
MAX_INPUT_LENGTH = 2096
|
| 37 |
+
MAX_NUM_IMAGES = 5
|
| 38 |
+
SERPHOUSE_API_KEY = os.getenv("SERPHOUSE_API_KEY", "")
|
| 39 |
+
|
| 40 |
##############################################################################
|
| 41 |
+
# ์ ์ญ ๋ณ์
|
| 42 |
+
##############################################################################
|
| 43 |
+
model = None
|
| 44 |
+
processor = None
|
| 45 |
+
model_loaded = False
|
| 46 |
+
model_name = "Gemma3-R1984-4B"
|
| 47 |
+
|
| 48 |
+
##############################################################################
|
| 49 |
+
# ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ
|
| 50 |
##############################################################################
|
| 51 |
def clear_cuda_cache():
|
| 52 |
"""CUDA ์บ์๋ฅผ ๋ช
์์ ์ผ๋ก ๋น์๋๋ค."""
|
|
|
|
| 55 |
gc.collect()
|
| 56 |
|
| 57 |
##############################################################################
|
| 58 |
+
# ํค์๋ ์ถ์ถ ํจ์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
##############################################################################
|
| 60 |
def extract_keywords(text: str, top_k: int = 5) -> str:
|
| 61 |
+
"""ํค์๋ ์ถ์ถ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
text = re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ\s]", "", text)
|
| 63 |
tokens = text.split()
|
| 64 |
+
|
| 65 |
+
seen = set()
|
| 66 |
+
unique_tokens = []
|
| 67 |
+
for token in tokens:
|
| 68 |
+
if token not in seen and len(token) > 1:
|
| 69 |
+
seen.add(token)
|
| 70 |
+
unique_tokens.append(token)
|
| 71 |
+
|
| 72 |
+
key_tokens = unique_tokens[:top_k]
|
| 73 |
return " ".join(key_tokens)
|
| 74 |
|
| 75 |
##############################################################################
|
| 76 |
+
# ์น ๊ฒ์ ํจ์
|
|
|
|
| 77 |
##############################################################################
|
| 78 |
def do_web_search(query: str) -> str:
|
| 79 |
+
"""SerpHouse API๋ฅผ ์ฌ์ฉํ ์น ๊ฒ์"""
|
|
|
|
|
|
|
|
|
|
| 80 |
try:
|
| 81 |
url = "https://api.serphouse.com/serp/live"
|
| 82 |
|
|
|
|
| 83 |
params = {
|
| 84 |
"q": query,
|
| 85 |
"domain": "google.com",
|
| 86 |
+
"serp_type": "web",
|
| 87 |
"device": "desktop",
|
| 88 |
+
"lang": "ko", # ํ๊ตญ์ด ์ฐ์
|
| 89 |
+
"num": "10" # 10๊ฐ๋ก ์ ํ
|
| 90 |
}
|
| 91 |
|
| 92 |
headers = {
|
| 93 |
"Authorization": f"Bearer {SERPHOUSE_API_KEY}"
|
| 94 |
}
|
| 95 |
|
| 96 |
+
logger.info(f"์น ๊ฒ์ ์ค... ๊ฒ์์ด: {query}")
|
|
|
|
| 97 |
|
|
|
|
| 98 |
response = requests.get(url, headers=headers, params=params, timeout=60)
|
| 99 |
response.raise_for_status()
|
| 100 |
|
|
|
|
| 101 |
data = response.json()
|
| 102 |
|
|
|
|
| 103 |
results = data.get("results", {})
|
| 104 |
+
organic = results.get("organic", []) if isinstance(results, dict) else []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
if not organic:
|
| 107 |
+
return "๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 108 |
+
|
| 109 |
+
max_results = min(10, len(organic))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
limited_organic = organic[:max_results]
|
| 111 |
|
|
|
|
| 112 |
summary_lines = []
|
| 113 |
for idx, item in enumerate(limited_organic, start=1):
|
| 114 |
+
title = item.get("title", "์ ๋ชฉ ์์")
|
| 115 |
link = item.get("link", "#")
|
| 116 |
+
snippet = item.get("snippet", "์ค๋ช
์์")
|
| 117 |
displayed_link = item.get("displayed_link", link)
|
| 118 |
|
|
|
|
| 119 |
summary_lines.append(
|
| 120 |
+
f"### ๊ฒฐ๊ณผ {idx}: {title}\n\n"
|
| 121 |
f"{snippet}\n\n"
|
| 122 |
f"**์ถ์ฒ**: [{displayed_link}]({link})\n\n"
|
| 123 |
f"---\n"
|
| 124 |
)
|
| 125 |
|
| 126 |
+
instructions = """# ์น ๊ฒ์ ๊ฒฐ๊ณผ
|
| 127 |
+
์๋๋ ๊ฒ์ ๊ฒฐ๊ณผ์
๋๋ค. ๋ต๋ณ ์ ์ด ์ ๋ณด๋ฅผ ํ์ฉํ์ธ์:
|
| 128 |
+
1. ๊ฐ ๊ฒฐ๊ณผ์ ์ ๋ชฉ, ๋ด์ฉ, ์ถ์ฒ ๋งํฌ๋ฅผ ์ฐธ์กฐํ์ธ์
|
| 129 |
+
2. ๊ด๋ จ ์ถ์ฒ๋ฅผ ๋ช
์์ ์ผ๋ก ์ธ์ฉํ์ธ์
|
| 130 |
+
3. ์ฌ๋ฌ ์ถ์ฒ์ ์ ๋ณด๋ฅผ ์ข
ํฉํ์ฌ ๋ต๋ณํ์ธ์
|
|
|
|
|
|
|
|
|
|
| 131 |
"""
|
| 132 |
|
| 133 |
search_results = instructions + "\n".join(summary_lines)
|
|
|
|
| 134 |
return search_results
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
+
logger.error(f"์น ๊ฒ์ ์คํจ: {e}")
|
| 138 |
+
return f"์น ๊ฒ์ ์คํจ: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
##############################################################################
|
| 141 |
+
# ๋ฌธ์ ์ฒ๋ฆฌ ํจ์
|
| 142 |
##############################################################################
|
| 143 |
def analyze_csv_file(path: str) -> str:
|
| 144 |
+
"""CSV ํ์ผ ๋ถ์"""
|
|
|
|
|
|
|
| 145 |
try:
|
| 146 |
df = pd.read_csv(path)
|
| 147 |
if df.shape[0] > 50 or df.shape[1] > 10:
|
| 148 |
df = df.iloc[:50, :10]
|
| 149 |
df_str = df.to_string()
|
| 150 |
if len(df_str) > MAX_CONTENT_CHARS:
|
| 151 |
+
df_str = df_str[:MAX_CONTENT_CHARS] + "\n...(์ค๋ต)..."
|
| 152 |
+
return f"**[CSV ํ์ผ: {os.path.basename(path)}]**\n\n{df_str}"
|
| 153 |
except Exception as e:
|
| 154 |
+
return f"CSV ์ฝ๊ธฐ ์คํจ ({os.path.basename(path)}): {str(e)}"
|
|
|
|
| 155 |
|
| 156 |
def analyze_txt_file(path: str) -> str:
|
| 157 |
+
"""TXT ํ์ผ ๋ถ์"""
|
|
|
|
|
|
|
| 158 |
try:
|
| 159 |
with open(path, "r", encoding="utf-8") as f:
|
| 160 |
text = f.read()
|
| 161 |
if len(text) > MAX_CONTENT_CHARS:
|
| 162 |
+
text = text[:MAX_CONTENT_CHARS] + "\n...(์ค๋ต)..."
|
| 163 |
+
return f"**[TXT ํ์ผ: {os.path.basename(path)}]**\n\n{text}"
|
| 164 |
except Exception as e:
|
| 165 |
+
return f"TXT ์ฝ๊ธฐ ์คํจ ({os.path.basename(path)}): {str(e)}"
|
|
|
|
| 166 |
|
| 167 |
def pdf_to_markdown(pdf_path: str) -> str:
|
| 168 |
+
"""PDF๋ฅผ ๋งํฌ๋ค์ด์ผ๋ก ๋ณํ"""
|
|
|
|
|
|
|
| 169 |
text_chunks = []
|
| 170 |
try:
|
| 171 |
with open(pdf_path, "rb") as f:
|
|
|
|
| 177 |
page_text = page_text.strip()
|
| 178 |
if page_text:
|
| 179 |
if len(page_text) > MAX_CONTENT_CHARS // max_pages:
|
| 180 |
+
page_text = page_text[:MAX_CONTENT_CHARS // max_pages] + "...(์ค๋ต)"
|
| 181 |
+
text_chunks.append(f"## ํ์ด์ง {page_num+1}\n\n{page_text}\n")
|
| 182 |
if len(reader.pages) > max_pages:
|
| 183 |
+
text_chunks.append(f"\n...({max_pages}/{len(reader.pages)} ํ์ด์ง ํ์)...")
|
| 184 |
except Exception as e:
|
| 185 |
+
return f"PDF ์ฝ๊ธฐ ์คํจ ({os.path.basename(pdf_path)}): {str(e)}"
|
| 186 |
|
| 187 |
full_text = "\n".join(text_chunks)
|
| 188 |
if len(full_text) > MAX_CONTENT_CHARS:
|
| 189 |
+
full_text = full_text[:MAX_CONTENT_CHARS] + "\n...(์ค๋ต)..."
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
return f"**[PDF ํ์ผ: {os.path.basename(pdf_path)}]**\n\n{full_text}"
|
| 192 |
|
| 193 |
##############################################################################
|
| 194 |
+
# ๋ชจ๋ธ ๋ก๋
|
| 195 |
##############################################################################
|
| 196 |
+
@spaces.GPU(duration=120)
|
| 197 |
+
def load_model():
|
| 198 |
+
global model, processor, model_loaded
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
if model_loaded:
|
| 201 |
+
logger.info("๋ชจ๋ธ์ด ์ด๋ฏธ ๋ก๋๋์ด ์์ต๋๋ค.")
|
| 202 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
try:
|
| 205 |
+
logger.info("Gemma3-R1984-4B ๋ชจ๋ธ ๋ก๋ฉ ์์...")
|
| 206 |
+
clear_cuda_cache()
|
| 207 |
+
|
| 208 |
+
model_id = os.getenv("MODEL_ID", "VIDraft/Gemma-3-R1984-4B")
|
| 209 |
+
|
| 210 |
+
processor = AutoProcessor.from_pretrained(model_id, padding_side="left")
|
| 211 |
+
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 212 |
+
model_id,
|
| 213 |
+
device_map="auto",
|
| 214 |
+
torch_dtype=torch.bfloat16,
|
| 215 |
+
attn_implementation="eager"
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
model_loaded = True
|
| 219 |
+
logger.info(f"โ
{model_name} ๋ก๋ฉ ์๋ฃ!")
|
| 220 |
+
return True
|
| 221 |
+
|
| 222 |
+
except Exception as e:
|
| 223 |
+
logger.error(f"๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
| 224 |
+
return False
|
| 225 |
|
| 226 |
##############################################################################
|
| 227 |
+
# ์ด๋ฏธ์ง ๋ถ์ (๋ก๋ด ํ์คํฌ ์ค์ฌ)
|
| 228 |
##############################################################################
|
| 229 |
+
@spaces.GPU(duration=60)
|
| 230 |
+
def analyze_image_for_robot(
|
| 231 |
+
image: Union[np.ndarray, Image.Image],
|
| 232 |
+
prompt: str,
|
| 233 |
+
task_type: str = "general",
|
| 234 |
+
use_web_search: bool = False,
|
| 235 |
+
enable_thinking: bool = True,
|
| 236 |
+
max_new_tokens: int = 1024
|
| 237 |
+
) -> str:
|
| 238 |
+
"""๋ก๋ด ์์
์ ์ํ ์ด๋ฏธ์ง ๋ถ์"""
|
| 239 |
+
global model, processor
|
| 240 |
|
| 241 |
+
if not model_loaded:
|
| 242 |
+
if not load_model():
|
| 243 |
+
return "โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
+
try:
|
| 246 |
+
# numpy ๋ฐฐ์ด์ PIL ์ด๋ฏธ์ง๋ก ๋ณํ
|
| 247 |
+
if isinstance(image, np.ndarray):
|
| 248 |
+
image = Image.fromarray(image).convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
+
# ํ์คํฌ๋ณ ์์คํ
ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 251 |
+
system_prompts = {
|
| 252 |
+
"general": "๋น์ ์ ๋ก๋ด ์๊ฐ ์์คํ
์
๋๋ค. ์ด๋ฏธ์ง๋ฅผ ์์ธํ ๋ถ์ํ๊ณ ์ค๋ช
ํ์ธ์.",
|
| 253 |
+
"planning": """๋น์ ์ ๋ก๋ด ์์
๊ณํ์ ์๋ฆฝํ๋ AI์
๋๋ค.
|
| 254 |
+
์ฃผ์ด์ง ์ด๋ฏธ์ง์ ์์
์ ๋ถ์ํ์ฌ ๋จ๊ณ๋ณ ์คํ ๊ณํ์ ์์ฑํ์ธ์.
|
| 255 |
+
ํ์: Step_1: xxx\nStep_2: xxx\n...\nStep_n: xxx""",
|
| 256 |
+
"grounding": "๋น์ ์ ๊ฐ์ฒด ์์น๋ฅผ ์ฐพ๋ ๋ก๋ด ์๊ฐ ์์คํ
์
๋๋ค. ์์ฒญ๋ ๊ฐ์ฒด์ ์์น๋ฅผ [x1, y1, x2, y2] ์ขํ๋ก ๋ฐํํ์ธ์.",
|
| 257 |
+
"affordance": "๋น์ ์ ๋ก๋ด ํ์ง์ ์ ๋ถ์ํ๋ AI์
๋๋ค. ์ฃผ์ด์ง ์์
์ ์ํ ์ต์ ์ ํ์ง ์์ญ์ [x1, y1, x2, y2] ์ขํ๋ก ์์ธกํ์ธ์.",
|
| 258 |
+
"trajectory": "๋น์ ์ ๋ก๋ด ๊ฒฝ๋ก๋ฅผ ๊ณํํ๋ AI์
๋๋ค. ๋ชฉํ ์ง์ ๊น์ง์ ๊ฒฝ๋ก๋ฅผ [(x1,y1), (x2,y2), ...] ํ์์ผ๋ก ์ ์ํ์ธ์.",
|
| 259 |
+
"pointing": "๋น์ ์ ๋ค์ค ์ง์ ์ ์ง์ ํ๋ ๋ก๋ด ์๊ฐ ์์คํ
์
๋๋ค. ์์ฒญ๋ ์์น๋ค์ [(x1,y1), (x2,y2), ...] ํ์์ผ๋ก ๋ฐํํ์ธ์."
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
system_prompt = system_prompts.get(task_type, system_prompts["general"])
|
| 263 |
+
|
| 264 |
+
# Chain-of-Thought ์ถ๊ฐ
|
| 265 |
+
if enable_thinking:
|
| 266 |
+
system_prompt += "\n\n์ถ๋ก ๊ณผ์ ์ <thinking></thinking> ํ๊ทธ ์์ ์์ธํ ์์ฑํ ํ ์ต์ข
๋ต๋ณ์ ์ ์ํ์ธ์."
|
| 267 |
+
|
| 268 |
+
# ์น ๊ฒ์ ์ํ
|
| 269 |
+
combined_system = system_prompt
|
| 270 |
+
if use_web_search:
|
| 271 |
+
keywords = extract_keywords(prompt, top_k=5)
|
| 272 |
+
if keywords:
|
| 273 |
+
logger.info(f"์น ๊ฒ์ ํค์๋: {keywords}")
|
| 274 |
+
search_results = do_web_search(keywords)
|
| 275 |
+
combined_system = f"{search_results}\n\n{system_prompt}"
|
| 276 |
+
|
| 277 |
+
# ๋ฉ์์ง ๊ตฌ์ฑ
|
| 278 |
+
messages = [
|
| 279 |
+
{
|
| 280 |
+
"role": "system",
|
| 281 |
+
"content": [{"type": "text", "text": combined_system}]
|
| 282 |
+
},
|
| 283 |
+
{
|
| 284 |
+
"role": "user",
|
| 285 |
+
"content": [
|
| 286 |
+
{"type": "image", "url": image},
|
| 287 |
+
{"type": "text", "text": prompt}
|
| 288 |
+
]
|
| 289 |
+
}
|
| 290 |
+
]
|
| 291 |
+
|
| 292 |
+
# ์
๋ ฅ ์ฒ๋ฆฌ
|
| 293 |
+
inputs = processor.apply_chat_template(
|
| 294 |
+
messages,
|
| 295 |
+
add_generation_prompt=True,
|
| 296 |
+
tokenize=True,
|
| 297 |
+
return_dict=True,
|
| 298 |
+
return_tensors="pt",
|
| 299 |
+
).to(device=model.device, dtype=torch.bfloat16)
|
| 300 |
+
|
| 301 |
+
# ์
๋ ฅ ํ ํฐ ์ ์ ํ
|
| 302 |
+
if inputs.input_ids.shape[1] > MAX_INPUT_LENGTH:
|
| 303 |
+
inputs.input_ids = inputs.input_ids[:, -MAX_INPUT_LENGTH:]
|
| 304 |
+
if 'attention_mask' in inputs:
|
| 305 |
+
inputs.attention_mask = inputs.attention_mask[:, -MAX_INPUT_LENGTH:]
|
| 306 |
+
|
| 307 |
+
# ์์ฑ
|
| 308 |
+
with torch.no_grad():
|
| 309 |
+
outputs = model.generate(
|
| 310 |
+
**inputs,
|
| 311 |
+
max_new_tokens=max_new_tokens,
|
| 312 |
+
do_sample=True,
|
| 313 |
+
temperature=0.7,
|
| 314 |
+
top_p=0.9,
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
# ๋์ฝ๋ฉ
|
| 318 |
+
response = processor.decode(outputs[0], skip_special_tokens=True)
|
| 319 |
+
|
| 320 |
+
# ํ๋กฌํํธ ์ ๊ฑฐ
|
| 321 |
+
if "Assistant:" in response:
|
| 322 |
+
response = response.split("Assistant:")[-1].strip()
|
| 323 |
+
|
| 324 |
+
return response
|
| 325 |
+
|
| 326 |
+
except Exception as e:
|
| 327 |
+
logger.error(f"์ด๋ฏธ์ง ๋ถ์ ์ค๋ฅ: {e}")
|
| 328 |
+
import traceback
|
| 329 |
+
return f"โ ๋ถ์ ์ค๋ฅ: {str(e)}\n{traceback.format_exc()}"
|
| 330 |
+
finally:
|
| 331 |
+
clear_cuda_cache()
|
| 332 |
|
| 333 |
##############################################################################
|
| 334 |
+
# ๋ฌธ์ ๋ถ์ (์คํธ๋ฆฌ๋ฐ)
|
| 335 |
##############################################################################
|
| 336 |
def _model_gen_with_oom_catch(**kwargs):
|
| 337 |
+
"""OOM ์ฒ๋ฆฌ๋ฅผ ์ํ ์์ฑ ํจ์"""
|
| 338 |
+
global model
|
|
|
|
| 339 |
try:
|
| 340 |
model.generate(**kwargs)
|
| 341 |
except torch.cuda.OutOfMemoryError:
|
| 342 |
+
raise RuntimeError("GPU ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ. Max Tokens๋ฅผ ์ค์ฌ์ฃผ์ธ์.")
|
|
|
|
|
|
|
|
|
|
| 343 |
finally:
|
|
|
|
| 344 |
clear_cuda_cache()
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
@spaces.GPU(duration=120)
|
| 347 |
+
def analyze_documents_streaming(
|
| 348 |
+
files: List[str],
|
| 349 |
+
prompt: str,
|
|
|
|
|
|
|
| 350 |
use_web_search: bool = False,
|
| 351 |
+
max_new_tokens: int = 2048
|
| 352 |
) -> Iterator[str]:
|
| 353 |
+
"""๋ฌธ์ ๋ถ์ (์คํธ๋ฆฌ๋ฐ)"""
|
| 354 |
+
global model, processor
|
| 355 |
+
|
| 356 |
+
if not model_loaded:
|
| 357 |
+
if not load_model():
|
| 358 |
+
yield "โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ"
|
| 359 |
+
return
|
| 360 |
|
| 361 |
try:
|
| 362 |
+
# ์์คํ
ํ๋กฌํํธ
|
| 363 |
+
system_content = "๋น์ ์ ๋ฌธ์๋ฅผ ๋ถ์ํ๊ณ ์์ฝํ๋ ์ ๋ฌธ AI์
๋๋ค."
|
| 364 |
+
|
| 365 |
+
# ์น ๊ฒ์
|
|
|
|
|
|
|
| 366 |
if use_web_search:
|
| 367 |
+
keywords = extract_keywords(prompt, top_k=5)
|
| 368 |
+
if keywords:
|
| 369 |
+
search_results = do_web_search(keywords)
|
| 370 |
+
system_content = f"{search_results}\n\n{system_content}"
|
| 371 |
+
|
| 372 |
+
# ๋ฌธ์ ๋ด์ฉ ์ฒ๋ฆฌ
|
| 373 |
+
doc_contents = []
|
| 374 |
+
for file_path in files:
|
| 375 |
+
if file_path.lower().endswith('.csv'):
|
| 376 |
+
content = analyze_csv_file(file_path)
|
| 377 |
+
elif file_path.lower().endswith('.txt'):
|
| 378 |
+
content = analyze_txt_file(file_path)
|
| 379 |
+
elif file_path.lower().endswith('.pdf'):
|
| 380 |
+
content = pdf_to_markdown(file_path)
|
|
|
|
| 381 |
else:
|
| 382 |
+
continue
|
| 383 |
+
doc_contents.append(content)
|
| 384 |
+
|
| 385 |
+
# ๋ฉ์์ง ๊ตฌ์ฑ
|
| 386 |
+
messages = [
|
| 387 |
+
{
|
| 388 |
"role": "system",
|
| 389 |
+
"content": [{"type": "text", "text": system_content}]
|
| 390 |
+
},
|
| 391 |
+
{
|
| 392 |
+
"role": "user",
|
| 393 |
+
"content": [
|
| 394 |
+
{"type": "text", "text": "\n\n".join(doc_contents) + f"\n\n{prompt}"}
|
| 395 |
+
]
|
| 396 |
+
}
|
| 397 |
+
]
|
| 398 |
|
| 399 |
+
# ์
๋ ฅ ์ฒ๋ฆฌ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
inputs = processor.apply_chat_template(
|
| 401 |
messages,
|
| 402 |
add_generation_prompt=True,
|
|
|
|
| 405 |
return_tensors="pt",
|
| 406 |
).to(device=model.device, dtype=torch.bfloat16)
|
| 407 |
|
| 408 |
+
# ์คํธ๋ฆฌ๋ฐ ์ค์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
streamer = TextIteratorStreamer(processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
| 410 |
gen_kwargs = dict(
|
| 411 |
inputs,
|
| 412 |
streamer=streamer,
|
| 413 |
max_new_tokens=max_new_tokens,
|
| 414 |
+
temperature=0.8,
|
| 415 |
+
top_p=0.9,
|
| 416 |
)
|
| 417 |
+
|
| 418 |
+
# ๋ณ๋ ์ค๋ ๋์์ ์์ฑ
|
| 419 |
t = Thread(target=_model_gen_with_oom_catch, kwargs=gen_kwargs)
|
| 420 |
t.start()
|
| 421 |
+
|
| 422 |
+
# ์คํธ๋ฆฌ๋ฐ ์ถ๋ ฅ
|
| 423 |
output = ""
|
| 424 |
for new_text in streamer:
|
| 425 |
output += new_text
|
| 426 |
yield output
|
| 427 |
+
|
| 428 |
except Exception as e:
|
| 429 |
+
logger.error(f"๋ฌธ์ ๋ถ์ ์ค๋ฅ: {e}")
|
| 430 |
+
yield f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"
|
|
|
|
| 431 |
finally:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
clear_cuda_cache()
|
| 433 |
|
|
|
|
|
|
|
| 434 |
##############################################################################
|
| 435 |
+
# Gradio UI (๋ก๋ด ์๊ฐํ ์ค์ฌ)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
##############################################################################
|
| 437 |
css = """
|
| 438 |
+
.robot-header {
|
| 439 |
+
text-align: center;
|
| 440 |
+
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 50%, #667eea 100%);
|
| 441 |
+
color: white;
|
| 442 |
+
padding: 20px;
|
| 443 |
+
border-radius: 10px;
|
| 444 |
+
margin-bottom: 20px;
|
| 445 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 446 |
}
|
| 447 |
+
.status-box {
|
| 448 |
+
text-align: center;
|
| 449 |
+
padding: 10px;
|
| 450 |
+
border-radius: 5px;
|
| 451 |
+
margin: 10px 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
font-weight: bold;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
}
|
| 454 |
+
.info-box {
|
| 455 |
+
background: #f0f0f0;
|
| 456 |
+
padding: 15px;
|
| 457 |
+
border-radius: 8px;
|
| 458 |
+
margin: 10px 0;
|
| 459 |
+
border-left: 4px solid #2a5298;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
}
|
| 461 |
+
.task-button {
|
| 462 |
+
min-height: 60px;
|
| 463 |
+
font-size: 1.1em;
|
|
|
|
| 464 |
}
|
| 465 |
+
.webcam-container {
|
| 466 |
+
border: 3px solid #2a5298;
|
| 467 |
+
border-radius: 10px;
|
| 468 |
+
padding: 10px;
|
| 469 |
+
background: #f8f9fa;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
}
|
| 471 |
"""
|
| 472 |
|
| 473 |
+
with gr.Blocks(title="๐ค ๋ก๋ด ์๊ฐ ์์คํ
(Gemma3-4B)", css=css) as demo:
|
| 474 |
+
gr.HTML("""
|
| 475 |
+
<div class="robot-header">
|
| 476 |
+
<h1>๐ค ๋ก๋ด ์๊ฐ ์์คํ
</h1>
|
| 477 |
+
<h3>๐ฎ Gemma3-R1984-4B + ๐ท ์ค์๊ฐ ์น์บ + ๐ ์น ๊ฒ์</h3>
|
| 478 |
+
<p>โก ์ต์ ๋ฉํฐ๋ชจ๋ฌ AI๋ก ๋ก๋ด ์์
๋ถ์ ๋ฐ ๊ณํ ์๋ฆฝ!</p>
|
| 479 |
+
</div>
|
| 480 |
+
""")
|
| 481 |
+
|
| 482 |
+
gr.HTML("""
|
| 483 |
+
<div class="info-box">
|
| 484 |
+
<h4>๐ ์์คํ
ํน์ง:</h4>
|
| 485 |
+
<ul>
|
| 486 |
+
<li>๐ผ๏ธ ๊ณ ๊ธ ์ด๋ฏธ์ง/๋น๋์ค ๋ถ์ (Gemma3-4B VLM)</li>
|
| 487 |
+
<li>๐ ๋ค๋จ๊ณ ์์
๊ณํ ๋ฐ ์ถ๋ก </li>
|
| 488 |
+
<li>๐ ์ ๋ฐํ ๊ฐ์ฒด ์์น ํ์
(Grounding)</li>
|
| 489 |
+
<li>๐ค ๋ก๋ด ํ์ง์ ๋ถ์ (Affordance)</li>
|
| 490 |
+
<li>๐ค๏ธ ๊ฒฝ๋ก ๊ณํ (Trajectory Planning)</li>
|
| 491 |
+
<li>๐ ์ค์๊ฐ ์น ๊ฒ์ ํตํฉ</li>
|
| 492 |
+
<li>๐ ๋ฌธ์ ๋ถ์ (PDF, CSV, TXT)</li>
|
| 493 |
+
</ul>
|
| 494 |
+
</div>
|
| 495 |
+
""")
|
| 496 |
+
|
| 497 |
+
with gr.Row():
|
| 498 |
+
# ์ผ์ชฝ: ์น์บ ๋ฐ ์
๋ ฅ
|
| 499 |
+
with gr.Column(scale=1):
|
| 500 |
+
gr.Markdown("### ๐ท ์ค์๊ฐ ์น์บ ")
|
| 501 |
+
|
| 502 |
+
with gr.Group(elem_classes="webcam-container"):
|
| 503 |
+
webcam = gr.Image(
|
| 504 |
+
sources=["webcam"],
|
| 505 |
+
streaming=True,
|
| 506 |
+
type="numpy",
|
| 507 |
+
label="์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ",
|
| 508 |
+
height=350
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
# ์บก์ฒ๋ ์ด๋ฏธ์ง ํ์
|
| 512 |
+
captured_image = gr.Image(
|
| 513 |
+
label="์บก์ฒ๋ ์ด๋ฏธ์ง",
|
| 514 |
+
height=200,
|
| 515 |
+
visible=False
|
| 516 |
+
)
|
| 517 |
+
|
| 518 |
+
# ๋ก๋ด ์์
๋ฒํผ๋ค
|
| 519 |
+
gr.Markdown("### ๐ฏ ๋ก๋ด ์์
์ ํ")
|
| 520 |
+
with gr.Row():
|
| 521 |
+
capture_btn = gr.Button("๐ธ ์บก์ฒ", variant="primary", elem_classes="task-button")
|
| 522 |
+
clear_capture_btn = gr.Button("๐๏ธ ์ด๊ธฐํ", elem_classes="task-button")
|
| 523 |
+
|
| 524 |
+
with gr.Row():
|
| 525 |
+
planning_btn = gr.Button("๐ ์์
๊ณํ", elem_classes="task-button")
|
| 526 |
+
grounding_btn = gr.Button("๐ ๊ฐ์ฒด ์์น", elem_classes="task-button")
|
| 527 |
+
|
| 528 |
+
with gr.Row():
|
| 529 |
+
affordance_btn = gr.Button("๐ค ํ์ง์ ๋ถ์", elem_classes="task-button")
|
| 530 |
+
trajectory_btn = gr.Button("๐ค๏ธ ๊ฒฝ๋ก ๊ณํ", elem_classes="task-button")
|
| 531 |
+
|
| 532 |
+
# ์ค๋ฅธ์ชฝ: ๋ถ์ ์ค์ ๋ฐ ๊ฒฐ๊ณผ
|
| 533 |
+
with gr.Column(scale=2):
|
| 534 |
+
gr.Markdown("### โ๏ธ ๋ถ์ ์ค์ ")
|
| 535 |
+
|
| 536 |
+
with gr.Row():
|
| 537 |
+
with gr.Column():
|
| 538 |
+
task_prompt = gr.Textbox(
|
| 539 |
+
label="์์
์ค๋ช
/ ์ง๋ฌธ",
|
| 540 |
+
placeholder="์: ํ
์ด๋ธ ์์ ์ปต์ ์ก์์ ์ฑํฌ๋์ ๋๊ธฐ",
|
| 541 |
+
value="์ด ์ฅ๋ฉด์์ ๋ก๋ด์ด ์ํํ ์ ์๋ ์์
์ ๋ถ์ํ์ธ์.",
|
| 542 |
+
lines=2
|
| 543 |
+
)
|
| 544 |
+
|
| 545 |
+
with gr.Row():
|
| 546 |
+
use_web_search = gr.Checkbox(
|
| 547 |
+
label="๐ ์น ๊ฒ์ ์ฌ์ฉ",
|
| 548 |
+
value=False,
|
| 549 |
+
info="๊ด๋ จ ์ ๋ณด๋ฅผ ์น์์ ๊ฒ์ํฉ๋๋ค"
|
| 550 |
+
)
|
| 551 |
+
|
| 552 |
+
enable_thinking = gr.Checkbox(
|
| 553 |
+
label="๐ค ์ถ๋ก ๊ณผ์ ํ์",
|
| 554 |
+
value=True,
|
| 555 |
+
info="Chain-of-Thought ์ถ๋ก ๊ณผ์ ์ ๋ณด์ฌ์ค๋๋ค"
|
| 556 |
+
)
|
| 557 |
+
|
| 558 |
+
max_tokens = gr.Slider(
|
| 559 |
+
label="์ต๋ ํ ํฐ ์",
|
| 560 |
+
minimum=256,
|
| 561 |
+
maximum=4096,
|
| 562 |
+
value=1024,
|
| 563 |
+
step=256
|
| 564 |
+
)
|
| 565 |
+
|
| 566 |
+
gr.Markdown("### ๐ ๋ถ์ ๊ฒฐ๊ณผ")
|
| 567 |
+
result_output = gr.Textbox(
|
| 568 |
+
label="AI ๋ถ์ ๊ฒฐ๊ณผ",
|
| 569 |
+
lines=20,
|
| 570 |
+
max_lines=40,
|
| 571 |
+
show_copy_button=True,
|
| 572 |
+
elem_id="result"
|
| 573 |
+
)
|
| 574 |
+
|
| 575 |
+
status_display = gr.HTML(
|
| 576 |
+
'<div class="status-box" style="background:#d4edda; color:#155724;">๐ฎ ์์คํ
์ค๋น ์๋ฃ</div>'
|
| 577 |
+
)
|
| 578 |
+
|
| 579 |
+
# ๋ฌธ์ ๋ถ์ ํญ
|
| 580 |
+
with gr.Tab("๐ ๋ฌธ์ ๋ถ์"):
|
| 581 |
+
with gr.Row():
|
| 582 |
+
with gr.Column():
|
| 583 |
+
doc_files = gr.File(
|
| 584 |
+
label="๋ฌธ์ ์
๋ก๋",
|
| 585 |
+
file_count="multiple",
|
| 586 |
+
file_types=[".pdf", ".csv", ".txt"],
|
| 587 |
+
type="filepath"
|
| 588 |
+
)
|
| 589 |
+
|
| 590 |
+
doc_prompt = gr.Textbox(
|
| 591 |
+
label="๋ถ์ ์์ฒญ",
|
| 592 |
+
placeholder="์: ์ด ๋ฌธ์๋ค์ ํต์ฌ ๋ด์ฉ์ ์์ฝํ๊ณ ๋น๊ต ๋ถ์ํ์ธ์.",
|
| 593 |
+
lines=3
|
| 594 |
+
)
|
| 595 |
+
|
| 596 |
+
doc_web_search = gr.Checkbox(
|
| 597 |
+
label="๐ ์น ๊ฒ์ ์ฌ์ฉ",
|
| 598 |
+
value=False
|
| 599 |
+
)
|
| 600 |
+
|
| 601 |
+
analyze_docs_btn = gr.Button("๐ ๋ฌธ์ ๋ถ์", variant="primary")
|
| 602 |
+
|
| 603 |
+
with gr.Column():
|
| 604 |
+
doc_result = gr.Textbox(
|
| 605 |
+
label="๋ถ์ ๊ฒฐ๊ณผ",
|
| 606 |
+
lines=25,
|
| 607 |
+
max_lines=50
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 611 |
+
webcam_state = gr.State(None)
|
| 612 |
+
|
| 613 |
+
def capture_webcam(frame):
|
| 614 |
+
"""์น์บ ํ๋ ์ ์บก์ฒ"""
|
| 615 |
+
if frame is None:
|
| 616 |
+
return None, None, '<div class="status-box" style="background:#f8d7da; color:#721c24;">โ ์น์บ ํ๋ ์ ์์</div>'
|
| 617 |
+
return frame, gr.update(value=frame, visible=True), '<div class="status-box" style="background:#d4edda; color:#155724;">โ
์ด๋ฏธ์ง ์บก์ฒ ์๋ฃ</div>'
|
| 618 |
+
|
| 619 |
+
def clear_capture():
|
| 620 |
+
"""์บก์ฒ ์ด๊ธฐํ"""
|
| 621 |
+
return None, gr.update(visible=False), '<div class="status-box" style="background:#d4edda; color:#155724;">๐ฎ ์์คํ
์ค๋น ์๋ฃ</div>'
|
| 622 |
+
|
| 623 |
+
def analyze_with_task(image, prompt, task_type, use_search, thinking, tokens):
|
| 624 |
+
"""ํน์ ํ์คํฌ๋ก ์ด๋ฏธ์ง ๋ถ์"""
|
| 625 |
+
if image is None:
|
| 626 |
+
return "โ ๋จผ์ ์ด๋ฏธ์ง๋ฅผ ์บก์ฒํ์ธ์.", '<div class="status-box" style="background:#f8d7da; color:#721c24;">โ ์ด๋ฏธ์ง ์์</div>'
|
| 627 |
+
|
| 628 |
+
status = f'<div class="status-box" style="background:#cce5ff; color:#004085;">๐ {task_type} ๋ถ์ ์ค...</div>'
|
| 629 |
+
|
| 630 |
+
result = analyze_image_for_robot(
|
| 631 |
+
image=image,
|
| 632 |
+
prompt=prompt,
|
| 633 |
+
task_type=task_type,
|
| 634 |
+
use_web_search=use_search,
|
| 635 |
+
enable_thinking=thinking,
|
| 636 |
+
max_new_tokens=tokens
|
| 637 |
+
)
|
| 638 |
+
|
| 639 |
+
# ๊ฒฐ๊ณผ ํฌ๋งทํ
|
| 640 |
+
timestamp = time.strftime("%H:%M:%S")
|
| 641 |
+
task_names = {
|
| 642 |
+
"planning": "์์
๊ณํ",
|
| 643 |
+
"grounding": "๊ฐ์ฒด ์์น ํ์
",
|
| 644 |
+
"affordance": "ํ์ง์ ๋ถ์",
|
| 645 |
+
"trajectory": "๊ฒฝ๋ก ๊ณํ"
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
formatted_result = f"""๐ค ๋ก๋ด {task_names.get(task_type, '๋ถ์')} ๊ฒฐ๊ณผ:
|
| 649 |
|
| 650 |
+
๐ธ **์์
**: {prompt}
|
| 651 |
|
| 652 |
+
๐ **๋ถ์ ๊ฒฐ๊ณผ**:
|
| 653 |
+
{result}
|
| 654 |
|
| 655 |
+
โฐ ๋ถ์ ์๊ฐ: {timestamp}
|
| 656 |
+
๐ฏ ๋ชจ๋ธ: {model_name}
|
| 657 |
+
๐ง ํ์คํฌ: {task_type}"""
|
| 658 |
+
|
| 659 |
+
complete_status = '<div class="status-box" style="background:#d4edda; color:#155724;">โ
๋ถ์ ์๋ฃ!</div>'
|
| 660 |
+
return formatted_result, complete_status
|
| 661 |
+
|
| 662 |
+
# ์น์บ ์คํธ๋ฆฌ๋ฐ
|
| 663 |
+
webcam.stream(
|
| 664 |
+
fn=lambda x: x,
|
| 665 |
+
inputs=[webcam],
|
| 666 |
+
outputs=[webcam_state]
|
| 667 |
)
|
| 668 |
+
|
| 669 |
+
# ์บก์ฒ ๋ฒํผ
|
| 670 |
+
capture_btn.click(
|
| 671 |
+
fn=capture_webcam,
|
| 672 |
+
inputs=[webcam_state],
|
| 673 |
+
outputs=[webcam_state, captured_image, status_display]
|
| 674 |
)
|
| 675 |
|
| 676 |
+
# ์ด๊ธฐํ ๋ฒํผ
|
| 677 |
+
clear_capture_btn.click(
|
| 678 |
+
fn=clear_capture,
|
| 679 |
+
outputs=[webcam_state, captured_image, status_display]
|
|
|
|
|
|
|
|
|
|
| 680 |
)
|
| 681 |
|
| 682 |
+
# ์์
๋ฒํผ๋ค
|
| 683 |
+
planning_btn.click(
|
| 684 |
+
fn=lambda img, p, s, t, tk: analyze_with_task(img, p, "planning", s, t, tk),
|
| 685 |
+
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens],
|
| 686 |
+
outputs=[result_output, status_display]
|
| 687 |
)
|
| 688 |
|
| 689 |
+
grounding_btn.click(
|
| 690 |
+
fn=lambda img, p, s, t, tk: analyze_with_task(img, p, "grounding", s, t, tk),
|
| 691 |
+
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens],
|
| 692 |
+
outputs=[result_output, status_display]
|
| 693 |
+
)
|
| 694 |
+
|
| 695 |
+
affordance_btn.click(
|
| 696 |
+
fn=lambda img, p, s, t, tk: analyze_with_task(img, p, "affordance", s, t, tk),
|
| 697 |
+
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens],
|
| 698 |
+
outputs=[result_output, status_display]
|
| 699 |
+
)
|
| 700 |
+
|
| 701 |
+
trajectory_btn.click(
|
| 702 |
+
fn=lambda img, p, s, t, tk: analyze_with_task(img, p, "trajectory", s, t, tk),
|
| 703 |
+
inputs=[captured_image, task_prompt, use_web_search, enable_thinking, max_tokens],
|
| 704 |
+
outputs=[result_output, status_display]
|
| 705 |
+
)
|
| 706 |
+
|
| 707 |
+
# ๋ฌธ์ ๋ถ์
|
| 708 |
+
def analyze_docs(files, prompt, use_search):
|
| 709 |
+
if not files:
|
| 710 |
+
return "โ ๋ฌธ์๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 711 |
+
|
| 712 |
+
output = ""
|
| 713 |
+
for chunk in analyze_documents_streaming(files, prompt, use_search):
|
| 714 |
+
output = chunk
|
| 715 |
+
return output
|
| 716 |
+
|
| 717 |
+
analyze_docs_btn.click(
|
| 718 |
+
fn=analyze_docs,
|
| 719 |
+
inputs=[doc_files, doc_prompt, doc_web_search],
|
| 720 |
+
outputs=[doc_result]
|
| 721 |
+
)
|
| 722 |
+
|
| 723 |
+
# ์ด๊ธฐ ๋ชจ๋ธ ๋ก๋
|
| 724 |
+
def initial_load():
|
| 725 |
+
load_model()
|
| 726 |
+
return "์์คํ
์ค๋น ์๋ฃ! ๐"
|
| 727 |
+
|
| 728 |
+
demo.load(
|
| 729 |
+
fn=initial_load,
|
| 730 |
+
outputs=None
|
| 731 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 732 |
|
| 733 |
if __name__ == "__main__":
|
| 734 |
+
print("๐ ๋ก๋ด ์๊ฐ ์์คํ
์์ (Gemma3-R1984-4B)...")
|
| 735 |
+
demo.launch(
|
| 736 |
+
server_name="0.0.0.0",
|
| 737 |
+
server_port=7860,
|
| 738 |
+
share=False,
|
| 739 |
+
show_error=True,
|
| 740 |
+
debug=False
|
| 741 |
+
)
|