Spaces:
Sleeping
Sleeping
Auto commit at 07-2025-08 4:43:48
Browse files- .gitignore +219 -0
- app.py +101 -498
- app_250807_0427.py +574 -0
- app_local.py +245 -0
- app_local_250807_0427.py +245 -0
- test_input.py +100 -0
- test_text.py +100 -0
- test_tokenizer.py +159 -0
.gitignore
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.github/
|
| 2 |
+
.env
|
| 3 |
+
lily_llm_env/
|
| 4 |
+
lily_llm_core/models/
|
| 5 |
+
__pycache__/
|
| 6 |
+
*.pyc
|
| 7 |
+
.ipynb_checkpoints/
|
| 8 |
+
lily_llm_media/
|
| 9 |
+
vector_stores/
|
| 10 |
+
latex_ocr_env/
|
| 11 |
+
lily_llm_ignore/
|
| 12 |
+
|
| 13 |
+
# Byte-compiled / optimized / DLL files
|
| 14 |
+
__pycache__/
|
| 15 |
+
*.py[codz]
|
| 16 |
+
*$py.class
|
| 17 |
+
|
| 18 |
+
# C extensions
|
| 19 |
+
*.so
|
| 20 |
+
|
| 21 |
+
# Distribution / packaging
|
| 22 |
+
.Python
|
| 23 |
+
build/
|
| 24 |
+
develop-eggs/
|
| 25 |
+
dist/
|
| 26 |
+
downloads/
|
| 27 |
+
eggs/
|
| 28 |
+
.eggs/
|
| 29 |
+
lib/
|
| 30 |
+
lib64/
|
| 31 |
+
parts/
|
| 32 |
+
sdist/
|
| 33 |
+
var/
|
| 34 |
+
wheels/
|
| 35 |
+
share/python-wheels/
|
| 36 |
+
*.egg-info/
|
| 37 |
+
.installed.cfg
|
| 38 |
+
*.egg
|
| 39 |
+
MANIFEST
|
| 40 |
+
|
| 41 |
+
# PyInstaller
|
| 42 |
+
# Usually these files are written by a python script from a template
|
| 43 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 44 |
+
*.manifest
|
| 45 |
+
*.spec
|
| 46 |
+
|
| 47 |
+
# Installer logs
|
| 48 |
+
pip-log.txt
|
| 49 |
+
pip-delete-this-directory.txt
|
| 50 |
+
|
| 51 |
+
# Unit test / coverage reports
|
| 52 |
+
htmlcov/
|
| 53 |
+
.tox/
|
| 54 |
+
.nox/
|
| 55 |
+
.coverage
|
| 56 |
+
.coverage.*
|
| 57 |
+
.cache
|
| 58 |
+
nosetests.xml
|
| 59 |
+
coverage.xml
|
| 60 |
+
*.cover
|
| 61 |
+
*.py.cover
|
| 62 |
+
.hypothesis/
|
| 63 |
+
.pytest_cache/
|
| 64 |
+
cover/
|
| 65 |
+
|
| 66 |
+
# Translations
|
| 67 |
+
*.mo
|
| 68 |
+
*.pot
|
| 69 |
+
|
| 70 |
+
# Django stuff:
|
| 71 |
+
*.log
|
| 72 |
+
local_settings.py
|
| 73 |
+
db.sqlite3
|
| 74 |
+
db.sqlite3-journal
|
| 75 |
+
|
| 76 |
+
# Flask stuff:
|
| 77 |
+
instance/
|
| 78 |
+
.webassets-cache
|
| 79 |
+
|
| 80 |
+
# Scrapy stuff:
|
| 81 |
+
.scrapy
|
| 82 |
+
|
| 83 |
+
# Sphinx documentation
|
| 84 |
+
docs/_build/
|
| 85 |
+
|
| 86 |
+
# PyBuilder
|
| 87 |
+
.pybuilder/
|
| 88 |
+
target/
|
| 89 |
+
|
| 90 |
+
# Jupyter Notebook
|
| 91 |
+
.ipynb_checkpoints
|
| 92 |
+
|
| 93 |
+
# IPython
|
| 94 |
+
profile_default/
|
| 95 |
+
ipython_config.py
|
| 96 |
+
|
| 97 |
+
# pyenv
|
| 98 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 99 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 100 |
+
# .python-version
|
| 101 |
+
|
| 102 |
+
# pipenv
|
| 103 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 104 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 105 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 106 |
+
# install all needed dependencies.
|
| 107 |
+
#Pipfile.lock
|
| 108 |
+
|
| 109 |
+
# UV
|
| 110 |
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
| 111 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 112 |
+
# commonly ignored for libraries.
|
| 113 |
+
#uv.lock
|
| 114 |
+
|
| 115 |
+
# poetry
|
| 116 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 117 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 118 |
+
# commonly ignored for libraries.
|
| 119 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 120 |
+
#poetry.lock
|
| 121 |
+
#poetry.toml
|
| 122 |
+
|
| 123 |
+
# pdm
|
| 124 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 125 |
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
| 126 |
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
| 127 |
+
#pdm.lock
|
| 128 |
+
#pdm.toml
|
| 129 |
+
.pdm-python
|
| 130 |
+
.pdm-build/
|
| 131 |
+
|
| 132 |
+
# pixi
|
| 133 |
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
| 134 |
+
#pixi.lock
|
| 135 |
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
| 136 |
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
| 137 |
+
.pixi
|
| 138 |
+
|
| 139 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 140 |
+
__pypackages__/
|
| 141 |
+
|
| 142 |
+
# Celery stuff
|
| 143 |
+
celerybeat-schedule
|
| 144 |
+
celerybeat.pid
|
| 145 |
+
|
| 146 |
+
# SageMath parsed files
|
| 147 |
+
*.sage.py
|
| 148 |
+
|
| 149 |
+
# Environments
|
| 150 |
+
.env
|
| 151 |
+
.envrc
|
| 152 |
+
.venv
|
| 153 |
+
env/
|
| 154 |
+
venv/
|
| 155 |
+
ENV/
|
| 156 |
+
env.bak/
|
| 157 |
+
venv.bak/
|
| 158 |
+
|
| 159 |
+
# Spyder project settings
|
| 160 |
+
.spyderproject
|
| 161 |
+
.spyproject
|
| 162 |
+
|
| 163 |
+
# Rope project settings
|
| 164 |
+
.ropeproject
|
| 165 |
+
|
| 166 |
+
# mkdocs documentation
|
| 167 |
+
/site
|
| 168 |
+
|
| 169 |
+
# mypy
|
| 170 |
+
.mypy_cache/
|
| 171 |
+
.dmypy.json
|
| 172 |
+
dmypy.json
|
| 173 |
+
|
| 174 |
+
# Pyre type checker
|
| 175 |
+
.pyre/
|
| 176 |
+
|
| 177 |
+
# pytype static type analyzer
|
| 178 |
+
.pytype/
|
| 179 |
+
|
| 180 |
+
# Cython debug symbols
|
| 181 |
+
cython_debug/
|
| 182 |
+
|
| 183 |
+
# PyCharm
|
| 184 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 185 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 186 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 187 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 188 |
+
#.idea/
|
| 189 |
+
|
| 190 |
+
# Abstra
|
| 191 |
+
# Abstra is an AI-powered process automation framework.
|
| 192 |
+
# Ignore directories containing user credentials, local state, and settings.
|
| 193 |
+
# Learn more at https://abstra.io/docs
|
| 194 |
+
.abstra/
|
| 195 |
+
|
| 196 |
+
# Visual Studio Code
|
| 197 |
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
| 198 |
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
| 199 |
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
| 200 |
+
# you could uncomment the following to ignore the entire vscode folder
|
| 201 |
+
# .vscode/
|
| 202 |
+
|
| 203 |
+
# Ruff stuff:
|
| 204 |
+
.ruff_cache/
|
| 205 |
+
|
| 206 |
+
# PyPI configuration file
|
| 207 |
+
.pypirc
|
| 208 |
+
|
| 209 |
+
# Cursor
|
| 210 |
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
| 211 |
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
| 212 |
+
# refer to https://docs.cursor.com/context/ignore-files
|
| 213 |
+
.cursorignore
|
| 214 |
+
.cursorindexingignore
|
| 215 |
+
|
| 216 |
+
# Marimo
|
| 217 |
+
marimo/_static/
|
| 218 |
+
marimo/_lsp/
|
| 219 |
+
__marimo__/
|
app.py
CHANGED
|
@@ -1,574 +1,177 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import requests
|
| 4 |
-
import json
|
| 5 |
import traceback
|
| 6 |
-
from transformers import AutoTokenizer
|
| 7 |
import torch
|
| 8 |
import fitz # PyMuPDF
|
| 9 |
from PIL import Image
|
| 10 |
import io
|
| 11 |
-
import base64
|
| 12 |
|
| 13 |
-
# ์ ์ญ
|
| 14 |
tokenizer = None
|
| 15 |
model = None
|
| 16 |
MODEL_LOADED = False
|
| 17 |
|
| 18 |
-
# .env ํ์ผ์์ ํ๊ฒฝ ๋ณ์ ๋ก๋
|
| 19 |
try:
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
load_dotenv()
|
| 22 |
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 23 |
except ImportError:
|
| 24 |
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์, ์์คํ
ํ๊ฒฝ ๋ณ์ ์ฌ์ฉ")
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f"โ ๏ธ .env ํ์ผ ๋ก๋ ์คํจ: {e}")
|
| 27 |
|
| 28 |
-
# ํ๊ฒฝ
|
| 29 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 30 |
MODEL_NAME = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 31 |
|
| 32 |
-
print("๐
|
| 33 |
-
print("
|
| 34 |
-
print(f"1. ํ๊ฒฝ ๋ณ์ ํ์ธ:")
|
| 35 |
-
print(f" HF_TOKEN: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 36 |
-
print(f" MODEL_NAME: {MODEL_NAME}")
|
| 37 |
-
if HF_TOKEN:
|
| 38 |
-
print(f" ํ ํฐ ๊ธธ์ด: {len(HF_TOKEN)}")
|
| 39 |
-
print(f" ํ ํฐ ์์: {HF_TOKEN[:10]}...")
|
| 40 |
-
print(f" ํ ํฐ ๋: ...{HF_TOKEN[-10:]}")
|
| 41 |
|
| 42 |
-
#
|
| 43 |
try:
|
| 44 |
-
print(
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
if HF_TOKEN:
|
| 49 |
-
print(" ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 50 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 51 |
MODEL_NAME,
|
| 52 |
token=HF_TOKEN,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
trust_remote_code=True,
|
| 54 |
-
|
| 55 |
)
|
| 56 |
-
|
| 57 |
-
print(
|
| 58 |
-
print(f" ํ ํฌ๋์ด์ hasattr('encode'): {hasattr(tokenizer, 'encode')}")
|
| 59 |
-
|
| 60 |
-
print(" ์ปค์คํ
๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 61 |
-
# ์ปค์คํ
๋ชจ๋ธ ํด๋์ค import (Space ํด๋์ modeling.py ์ฌ์ฉ)
|
| 62 |
-
try:
|
| 63 |
-
from modeling import KananaVForConditionalGeneration
|
| 64 |
-
print(" โ
modeling.py import ์ฑ๊ณต")
|
| 65 |
-
except Exception as import_error:
|
| 66 |
-
print(f" โ modeling.py import ์คํจ: {import_error}")
|
| 67 |
-
raise import_error
|
| 68 |
-
|
| 69 |
-
try:
|
| 70 |
-
print(f" ๋ชจ๋ธ ๋ก๋ฉ ํ๋ผ๋ฏธํฐ:")
|
| 71 |
-
print(f" MODEL_NAME: {MODEL_NAME}")
|
| 72 |
-
print(f" torch_dtype: {torch.float16}")
|
| 73 |
-
print(f" trust_remote_code: True")
|
| 74 |
-
print(f" device_map: None")
|
| 75 |
-
print(f" low_cpu_mem_usage: True")
|
| 76 |
-
|
| 77 |
-
model = KananaVForConditionalGeneration.from_pretrained(
|
| 78 |
-
MODEL_NAME,
|
| 79 |
-
token=HF_TOKEN,
|
| 80 |
-
torch_dtype=torch.float16,
|
| 81 |
-
trust_remote_code=True,
|
| 82 |
-
device_map=None,
|
| 83 |
-
low_cpu_mem_usage=True
|
| 84 |
-
)
|
| 85 |
-
print(" โ
์ปค์คํ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
| 86 |
-
print(f" ๋ชจ๋ธ ํ์
: {type(model)}")
|
| 87 |
-
print(f" ๋ชจ๋ธ ๋๋ฐ์ด์ค: {next(model.parameters()).device}")
|
| 88 |
-
except Exception as model_error:
|
| 89 |
-
print(f" โ ์ปค์คํ
๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {model_error}")
|
| 90 |
-
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 91 |
-
import traceback
|
| 92 |
-
traceback.print_exc()
|
| 93 |
-
raise model_error
|
| 94 |
else:
|
| 95 |
-
print("
|
| 96 |
MODEL_NAME = "microsoft/DialoGPT-medium"
|
| 97 |
-
print(f" ๊ณต๊ฐ ๋ชจ๋ธ: {MODEL_NAME}")
|
| 98 |
-
|
| 99 |
-
print(" ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 100 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
print(" ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 104 |
-
from transformers import AutoModelForCausalLM
|
| 105 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
|
| 106 |
-
print(" โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
| 107 |
-
|
| 108 |
-
print("โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 109 |
-
MODEL_LOADED = True
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
-
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ:")
|
| 113 |
-
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 114 |
-
print(f" ์ค๋ฅ ๋ฉ์์ง: {str(e)}")
|
| 115 |
-
print(f" ์์ธ ์ค๋ฅ:")
|
| 116 |
traceback.print_exc()
|
| 117 |
MODEL_LOADED = False
|
| 118 |
|
| 119 |
-
|
| 120 |
-
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 121 |
-
print(f" ์ต์ข
๋ชจ๋ธ๋ช
: {MODEL_NAME}")
|
| 122 |
-
|
| 123 |
def extract_text_from_pdf(pdf_file):
|
| 124 |
-
"""PDF์์ ํ
์คํธ ์ถ์ถ"""
|
| 125 |
try:
|
| 126 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 127 |
-
text = ""
|
| 128 |
-
for page in doc:
|
| 129 |
-
text += page.get_text()
|
| 130 |
doc.close()
|
| 131 |
return text
|
| 132 |
except Exception as e:
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
def extract_text_from_image(image_file):
|
| 136 |
-
"""์ด๋ฏธ์ง์์ OCR๋ก ํ
์คํธ ์ถ์ถ"""
|
| 137 |
-
try:
|
| 138 |
-
# PIL๋ก ์ด๋ฏธ์ง ์ด๊ธฐ
|
| 139 |
-
image = Image.open(image_file)
|
| 140 |
-
|
| 141 |
-
# ๊ฐ๋จํ OCR (์ค์ ๋ก๋ ๋ ์ ๊ตํ OCR ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ ํ์)
|
| 142 |
-
# ์ฌ๊ธฐ์๋ ์ด๋ฏธ์ง ์ ๋ณด๋ง ๋ฐํ
|
| 143 |
-
return f"์ด๋ฏธ์ง ํ์ผ: {image.size[0]}x{image.size[1]} ํฝ์
"
|
| 144 |
-
except Exception as e:
|
| 145 |
-
return f"์ด๋ฏธ์ง ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}"
|
| 146 |
|
| 147 |
def process_uploaded_file(file):
|
| 148 |
-
"""์
๋ก๋๋
|
| 149 |
if file is None:
|
| 150 |
-
return
|
| 151 |
-
|
| 152 |
file_path = file.name
|
| 153 |
-
file_extension =
|
| 154 |
|
| 155 |
-
if file_extension == 'pdf':
|
| 156 |
text_content = extract_text_from_pdf(file)
|
| 157 |
-
return text_content, None
|
| 158 |
-
elif file_extension in ['png', 'jpg', 'jpeg']:
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 161 |
else:
|
| 162 |
return f"์ง์ํ์ง ์๋ ํ์ผ ํ์: {file_extension}", None
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
print(f" ๋ฉ์์ง: {message}")
|
| 168 |
-
print(f" ํ์ผ: {file}")
|
| 169 |
-
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 170 |
-
|
| 171 |
if not MODEL_LOADED:
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
try:
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
token=HF_TOKEN,
|
| 205 |
-
trust_remote_code=True,
|
| 206 |
-
use_fast=False
|
| 207 |
-
)
|
| 208 |
-
print("โ
DEBUG: tokenizer ์ฌ๋ก๋ ์ฑ๊ณต")
|
| 209 |
-
print(f" ์๋ก์ด tokenizer ํ์
: {type(tokenizer)}")
|
| 210 |
-
except Exception as reload_error:
|
| 211 |
-
print(f"โ DEBUG: tokenizer ์ฌ๋ก๋ ์คํจ: {reload_error}")
|
| 212 |
-
return f"ํ ํฌ๋์ด์ ์ค๋ฅ: {str(reload_error)}"
|
| 213 |
-
|
| 214 |
-
inputs = tokenizer(full_message, return_tensors="pt")
|
| 215 |
-
print(f" ์
๋ ฅ shape: {inputs['input_ids'].shape}")
|
| 216 |
-
print(f" attention_mask shape: {inputs['attention_mask'].shape}")
|
| 217 |
-
|
| 218 |
-
print("๐ค DEBUG: ๋ชจ๋ธ ์ถ๋ก ์์")
|
| 219 |
-
with torch.no_grad():
|
| 220 |
-
if image_file is not None:
|
| 221 |
-
print("๐ผ๏ธ DEBUG: ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 222 |
-
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฉํฐ๋ชจ๋ฌ ์์ฑ
|
| 223 |
-
import torchvision.transforms as transforms
|
| 224 |
-
|
| 225 |
-
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 226 |
-
transform = transforms.Compose([
|
| 227 |
-
transforms.Resize((224, 224)),
|
| 228 |
-
transforms.ToTensor(),
|
| 229 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 230 |
-
])
|
| 231 |
-
|
| 232 |
-
pil_image = Image.open(image_file).convert('RGB')
|
| 233 |
-
pixel_values = transform(pil_image).unsqueeze(0)
|
| 234 |
-
image_metas = {"vision_grid_thw": torch.tensor([[1, 14, 14]])} # ๊ธฐ๋ณธ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ
|
| 235 |
-
|
| 236 |
-
print(f" ์ด๋ฏธ์ง shape: {pixel_values.shape}")
|
| 237 |
-
print(f" ์ด๋ฏธ์ง ๋ฉํ: {image_metas}")
|
| 238 |
-
|
| 239 |
-
# ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ์ forward ๋ฉ์๋ ์ฌ์ฉ
|
| 240 |
-
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (๋ฉํฐ๋ชจ๋ฌ)")
|
| 241 |
-
try:
|
| 242 |
-
outputs = model(
|
| 243 |
-
input_ids=inputs["input_ids"],
|
| 244 |
-
attention_mask=inputs["attention_mask"],
|
| 245 |
-
pixel_values=[pixel_values],
|
| 246 |
-
image_metas=image_metas,
|
| 247 |
-
max_new_tokens=200,
|
| 248 |
-
temperature=0.7,
|
| 249 |
-
do_sample=True,
|
| 250 |
-
pad_token_id=tokenizer.eos_token_id
|
| 251 |
-
)
|
| 252 |
-
print("โ
DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 253 |
-
except Exception as model_error:
|
| 254 |
-
print(f"โ DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 255 |
-
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 256 |
-
raise model_error
|
| 257 |
-
else:
|
| 258 |
-
print("๐ DEBUG: ํ
์คํธ๋ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 259 |
-
# ํ
์คํธ๋ง ์์ฑ
|
| 260 |
-
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (ํ
์คํธ๋ง)")
|
| 261 |
-
try:
|
| 262 |
-
outputs = model(
|
| 263 |
-
input_ids=inputs["input_ids"],
|
| 264 |
-
attention_mask=inputs["attention_mask"],
|
| 265 |
-
max_new_tokens=200,
|
| 266 |
-
temperature=0.7,
|
| 267 |
-
do_sample=True,
|
| 268 |
-
pad_token_id=tokenizer.eos_token_id
|
| 269 |
-
)
|
| 270 |
-
print("โ
DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 271 |
-
except Exception as model_error:
|
| 272 |
-
print(f"โ DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 273 |
-
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 274 |
-
raise model_error
|
| 275 |
-
|
| 276 |
-
print("๐ DEBUG: ์ถ๋ ฅ ์ฒ๋ฆฌ ์์")
|
| 277 |
-
print(f" outputs ํ์
: {type(outputs)}")
|
| 278 |
-
print(f" outputs ๋ด์ฉ: {outputs}")
|
| 279 |
-
|
| 280 |
-
# outputs๊ฐ ํํ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์์ ์ฌ์ฉ
|
| 281 |
-
if isinstance(outputs, tuple):
|
| 282 |
-
print("๐ฆ DEBUG: outputs๊ฐ ํํ์")
|
| 283 |
-
logits = outputs[0]
|
| 284 |
-
print(f" logits shape: {logits.shape}")
|
| 285 |
else:
|
| 286 |
-
print("
|
| 287 |
-
if hasattr(outputs, 'logits'):
|
| 288 |
-
logits = outputs.logits
|
| 289 |
-
print(f" logits shape: {logits.shape}")
|
| 290 |
-
else:
|
| 291 |
-
logits = outputs
|
| 292 |
-
print(f" outputs shape: {logits.shape}")
|
| 293 |
-
|
| 294 |
-
print("๐ฏ DEBUG: ํ ํฐ ์์ฑ ์์")
|
| 295 |
-
# ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ํ ํฐ ์ ํ
|
| 296 |
-
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 297 |
-
generated_tokens = [next_token]
|
| 298 |
-
print(f" ์ฒซ ๋ฒ์งธ ํ ํฐ: {next_token.item()}")
|
| 299 |
-
|
| 300 |
-
# ์ถ๊ฐ ํ ํฐ ์์ฑ
|
| 301 |
-
print("๐ DEBUG: ๋ฐ๋ณต ํ ํฐ ์์ฑ ์์")
|
| 302 |
-
for i in range(199): # max_new_tokens - 1
|
| 303 |
-
if i % 50 == 0:
|
| 304 |
-
print(f" ์งํ๋ฅ : {i}/199")
|
| 305 |
-
|
| 306 |
-
inputs["input_ids"] = torch.cat([inputs["input_ids"], next_token.unsqueeze(-1)], dim=-1)
|
| 307 |
-
inputs["attention_mask"] = torch.cat([inputs["attention_mask"], torch.ones_like(next_token.unsqueeze(-1))], dim=-1)
|
| 308 |
-
|
| 309 |
-
with torch.no_grad():
|
| 310 |
-
try:
|
| 311 |
-
outputs = model(**inputs)
|
| 312 |
-
if isinstance(outputs, tuple):
|
| 313 |
-
logits = outputs[0]
|
| 314 |
-
else:
|
| 315 |
-
logits = outputs.logits if hasattr(outputs, 'logits') else outputs
|
| 316 |
-
|
| 317 |
-
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 318 |
-
generated_tokens.append(next_token)
|
| 319 |
-
|
| 320 |
-
if next_token.item() == tokenizer.eos_token_id:
|
| 321 |
-
print(f" EOS ํ ํฐ ๋ฐ๊ฒฌ: {i}๋ฒ์งธ")
|
| 322 |
-
break
|
| 323 |
-
except Exception as loop_error:
|
| 324 |
-
print(f"โ DEBUG: ํ ํฐ ์์ฑ ๋ฃจํ ์ค๋ฅ (i={i}): {loop_error}")
|
| 325 |
-
raise loop_error
|
| 326 |
-
|
| 327 |
-
print("๐ค DEBUG: ํ ํฐ ๋์ฝ๋ฉ ์์")
|
| 328 |
-
# ์์ฑ๋ ํ ํฐ๋ค์ ๋์ฝ๋ฉ
|
| 329 |
-
generated_ids = torch.cat(generated_tokens, dim=0)
|
| 330 |
-
response = tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 331 |
-
print(f" ์๋ณธ ์๋ต: {response[:200]}...")
|
| 332 |
-
|
| 333 |
-
if full_message in response:
|
| 334 |
-
response = response.replace(full_message, "").strip()
|
| 335 |
-
print(f" ์ ๋ฆฌ๋ ์๋ต: {response[:200]}...")
|
| 336 |
-
|
| 337 |
-
print("โ
DEBUG: chat_with_model ์๋ฃ")
|
| 338 |
-
return response if response else "์ฃ์กํฉ๋๋ค. ์๋ต์ ์์ฑํ ์ ์์ต๋๋ค."
|
| 339 |
-
except Exception as e:
|
| 340 |
-
print(f"โ DEBUG: chat_with_model ์ ์ฒด ์ค๋ฅ: {e}")
|
| 341 |
-
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 342 |
-
import traceback
|
| 343 |
-
traceback.print_exc()
|
| 344 |
-
return f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 345 |
|
| 346 |
-
|
| 347 |
-
global tokenizer, model
|
| 348 |
-
print(f"๐ DEBUG: solve_math_problem ์์")
|
| 349 |
-
print(f" ๋ฌธ์ : {problem}")
|
| 350 |
-
print(f" ํ์ผ: {file}")
|
| 351 |
-
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 352 |
-
|
| 353 |
-
if not MODEL_LOADED:
|
| 354 |
-
print("โ DEBUG: ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์")
|
| 355 |
-
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 356 |
-
|
| 357 |
-
try:
|
| 358 |
-
print("๐ DEBUG: ํ์ผ ์ฒ๋ฆฌ ์์")
|
| 359 |
-
# ํ์ผ ์ฒ๋ฆฌ
|
| 360 |
-
file_content = ""
|
| 361 |
-
image_file = None
|
| 362 |
-
if file is not None:
|
| 363 |
-
print(f" ํ์ผ๋ช
: {file.name}")
|
| 364 |
-
text_content, image_file = process_uploaded_file(file)
|
| 365 |
-
print(f" ํ
์คํธ ๋ด์ฉ: {text_content[:100] if text_content else 'None'}...")
|
| 366 |
-
print(f" ์ด๋ฏธ์ง ํ์ผ: {image_file}")
|
| 367 |
-
if text_content:
|
| 368 |
-
file_content = f"\n[์
๋ก๋๋ ํ์ผ ๋ด์ฉ]\n{text_content}\n"
|
| 369 |
-
|
| 370 |
-
# ๋ฉ์์ง์ ํ์ผ ๋ด์ฉ ์ถ๊ฐ
|
| 371 |
-
full_prompt = f"๋ค์ ์ํ ๋ฌธ์ ๋ฅผ ๋จ๊ณ๋ณ๋ก ํ์ด์ฃผ์ธ์: {problem}{file_content}"
|
| 372 |
-
print(f"๐ DEBUG: ์ ์ฒด ํ๋กฌํํธ: {full_prompt[:200]}...")
|
| 373 |
-
|
| 374 |
-
print("๐ค DEBUG: ํ ํฌ๋์ด์ ์ฒ๋ฆฌ ์์")
|
| 375 |
-
print(f" tokenizer ํ์
: {type(tokenizer)}")
|
| 376 |
-
print(f" tokenizer ๊ฐ: {tokenizer}")
|
| 377 |
-
|
| 378 |
-
# tokenizer๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธ
|
| 379 |
-
if not hasattr(tokenizer, 'encode') or tokenizer is None or isinstance(tokenizer, bool):
|
| 380 |
-
print("โ DEBUG: tokenizer๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์")
|
| 381 |
-
# tokenizer๋ฅผ ๋ค์ ๋ก๋
|
| 382 |
-
print("๐ DEBUG: tokenizer ์ฌ๋ก๋ ์๋")
|
| 383 |
-
try:
|
| 384 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 385 |
-
MODEL_NAME,
|
| 386 |
-
token=HF_TOKEN,
|
| 387 |
-
trust_remote_code=True,
|
| 388 |
-
use_fast=False
|
| 389 |
-
)
|
| 390 |
-
print("โ
DEBUG: tokenizer ์ฌ๋ก๋ ์ฑ๊ณต")
|
| 391 |
-
print(f" ์๋ก์ด tokenizer ํ์
: {type(tokenizer)}")
|
| 392 |
-
except Exception as reload_error:
|
| 393 |
-
print(f"โ DEBUG: tokenizer ์ฌ๋ก๋ ์คํจ: {reload_error}")
|
| 394 |
-
return f"ํ ํฌ๋์ด์ ์ค๋ฅ: {str(reload_error)}"
|
| 395 |
-
|
| 396 |
-
inputs = tokenizer(full_prompt, return_tensors="pt")
|
| 397 |
-
print(f" ์
๋ ฅ shape: {inputs['input_ids'].shape}")
|
| 398 |
-
print(f" attention_mask shape: {inputs['attention_mask'].shape}")
|
| 399 |
-
|
| 400 |
-
print("๐ค DEBUG: ๋ชจ๋ธ ์ถ๋ก ์์")
|
| 401 |
with torch.no_grad():
|
| 402 |
-
|
| 403 |
-
print("๐ผ๏ธ DEBUG: ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 404 |
-
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฉํฐ๋ชจ๋ฌ ์์ฑ
|
| 405 |
-
import torchvision.transforms as transforms
|
| 406 |
-
|
| 407 |
-
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 408 |
-
transform = transforms.Compose([
|
| 409 |
-
transforms.Resize((224, 224)),
|
| 410 |
-
transforms.ToTensor(),
|
| 411 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 412 |
-
])
|
| 413 |
-
|
| 414 |
-
pil_image = Image.open(image_file).convert('RGB')
|
| 415 |
-
pixel_values = transform(pil_image).unsqueeze(0)
|
| 416 |
-
image_metas = {"vision_grid_thw": torch.tensor([[1, 14, 14]])} # ๊ธฐ๋ณธ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ
|
| 417 |
-
|
| 418 |
-
print(f" ์ด๋ฏธ์ง shape: {pixel_values.shape}")
|
| 419 |
-
print(f" ์ด๋ฏธ์ง ๋ฉํ: {image_metas}")
|
| 420 |
-
|
| 421 |
-
# ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ์ forward ๋ฉ์๋ ์ฌ์ฉ
|
| 422 |
-
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (๋ฉํฐ๋ชจ๋ฌ)")
|
| 423 |
-
try:
|
| 424 |
-
outputs = model(
|
| 425 |
-
input_ids=inputs["input_ids"],
|
| 426 |
-
attention_mask=inputs["attention_mask"],
|
| 427 |
-
pixel_values=[pixel_values],
|
| 428 |
-
image_metas=image_metas,
|
| 429 |
-
max_new_tokens=300,
|
| 430 |
-
temperature=0.3,
|
| 431 |
-
do_sample=True,
|
| 432 |
-
pad_token_id=tokenizer.eos_token_id
|
| 433 |
-
)
|
| 434 |
-
print("โ
DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 435 |
-
except Exception as model_error:
|
| 436 |
-
print(f"โ DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 437 |
-
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 438 |
-
raise model_error
|
| 439 |
-
else:
|
| 440 |
-
print("๐ DEBUG: ํ
์คํธ๋ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 441 |
-
# ํ
์คํธ๋ง ์์ฑ
|
| 442 |
-
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (ํ
์คํธ๋ง)")
|
| 443 |
-
try:
|
| 444 |
-
outputs = model(
|
| 445 |
-
input_ids=inputs["input_ids"],
|
| 446 |
-
attention_mask=inputs["attention_mask"],
|
| 447 |
-
max_new_tokens=300,
|
| 448 |
-
temperature=0.3,
|
| 449 |
-
do_sample=True,
|
| 450 |
-
pad_token_id=tokenizer.eos_token_id
|
| 451 |
-
)
|
| 452 |
-
print("โ
DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 453 |
-
except Exception as model_error:
|
| 454 |
-
print(f"โ DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 455 |
-
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 456 |
-
raise model_error
|
| 457 |
-
|
| 458 |
-
print("๐ DEBUG: ์ถ๋ ฅ ์ฒ๋ฆฌ ์์")
|
| 459 |
-
print(f" outputs ํ์
: {type(outputs)}")
|
| 460 |
-
print(f" outputs ๋ด์ฉ: {outputs}")
|
| 461 |
-
|
| 462 |
-
# outputs๊ฐ ํํ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์์ ์ฌ์ฉ
|
| 463 |
-
if isinstance(outputs, tuple):
|
| 464 |
-
print("๐ฆ DEBUG: outputs๊ฐ ํํ์")
|
| 465 |
-
logits = outputs[0]
|
| 466 |
-
print(f" logits shape: {logits.shape}")
|
| 467 |
-
else:
|
| 468 |
-
print("๐ฆ DEBUG: outputs๊ฐ ๊ฐ์ฒด์")
|
| 469 |
-
if hasattr(outputs, 'logits'):
|
| 470 |
-
logits = outputs.logits
|
| 471 |
-
print(f" logits shape: {logits.shape}")
|
| 472 |
-
else:
|
| 473 |
-
logits = outputs
|
| 474 |
-
print(f" outputs shape: {logits.shape}")
|
| 475 |
-
|
| 476 |
-
print("๐ฏ DEBUG: ํ ํฐ ์์ฑ ์์")
|
| 477 |
-
# ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ํ ํฐ ์ ํ
|
| 478 |
-
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 479 |
-
generated_tokens = [next_token]
|
| 480 |
-
print(f" ์ฒซ ๋ฒ์งธ ํ ํฐ: {next_token.item()}")
|
| 481 |
-
|
| 482 |
-
# ์ถ๊ฐ ํ ํฐ ์์ฑ
|
| 483 |
-
print("๐ DEBUG: ๋ฐ๋ณต ํ ํฐ ์์ฑ ์์")
|
| 484 |
-
for i in range(299): # max_new_tokens - 1
|
| 485 |
-
if i % 50 == 0:
|
| 486 |
-
print(f" ์งํ๋ฅ : {i}/299")
|
| 487 |
-
|
| 488 |
-
inputs["input_ids"] = torch.cat([inputs["input_ids"], next_token.unsqueeze(-1)], dim=-1)
|
| 489 |
-
inputs["attention_mask"] = torch.cat([inputs["attention_mask"], torch.ones_like(next_token.unsqueeze(-1))], dim=-1)
|
| 490 |
-
|
| 491 |
-
with torch.no_grad():
|
| 492 |
-
try:
|
| 493 |
-
outputs = model(**inputs)
|
| 494 |
-
if isinstance(outputs, tuple):
|
| 495 |
-
logits = outputs[0]
|
| 496 |
-
else:
|
| 497 |
-
logits = outputs.logits if hasattr(outputs, 'logits') else outputs
|
| 498 |
-
|
| 499 |
-
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 500 |
-
generated_tokens.append(next_token)
|
| 501 |
-
|
| 502 |
-
if next_token.item() == tokenizer.eos_token_id:
|
| 503 |
-
print(f" EOS ํ ํฐ ๋ฐ๊ฒฌ: {i}๋ฒ์งธ")
|
| 504 |
-
break
|
| 505 |
-
except Exception as loop_error:
|
| 506 |
-
print(f"โ DEBUG: ํ ํฐ ์์ฑ ๋ฃจํ ์ค๋ฅ (i={i}): {loop_error}")
|
| 507 |
-
raise loop_error
|
| 508 |
-
|
| 509 |
-
print("๐ค DEBUG: ํ ํฐ ๋์ฝ๋ฉ ์์")
|
| 510 |
-
# ์์ฑ๋ ํ ํฐ๋ค์ ๋์ฝ๋ฉ
|
| 511 |
-
generated_ids = torch.cat(generated_tokens, dim=0)
|
| 512 |
-
response = tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 513 |
-
print(f" ์๋ณธ ์๋ต: {response[:200]}...")
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
|
|
|
|
|
|
| 518 |
|
| 519 |
-
|
| 520 |
-
|
| 521 |
except Exception as e:
|
| 522 |
-
print(f"โ
|
| 523 |
-
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 524 |
-
import traceback
|
| 525 |
traceback.print_exc()
|
| 526 |
-
return f"
|
| 527 |
|
|
|
|
| 528 |
with gr.Blocks(title="Lily Math RAG System", theme=gr.themes.Soft()) as demo:
|
| 529 |
gr.Markdown("# ๐งฎ Lily Math RAG System")
|
| 530 |
-
gr.Markdown("์ํ ๋ฌธ์
|
|
|
|
| 531 |
with gr.Tabs():
|
| 532 |
with gr.Tab("๐ฌ ์ฑํ
"):
|
|
|
|
|
|
|
| 533 |
with gr.Row():
|
| 534 |
-
with gr.Column(scale=
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
with gr.Column(scale=1):
|
| 539 |
-
gr.Markdown("### ๐ ํ์ผ ์
๋ก๋")
|
| 540 |
-
file_input = gr.File(label="PDF/์ด๋ฏธ์ง ํ์ผ (์ ํ์ฌํญ)", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 541 |
-
gr.Markdown("PDF๋ ์ด๋ฏธ์ง ํ์ผ์ ์
๋ก๋ํ๋ฉด ๋ฌธ์๋ฅผ ํด์ํ์ฌ ๋ต๋ณํฉ๋๋ค.")
|
| 542 |
|
| 543 |
def respond(message, chat_history, file):
|
| 544 |
-
bot_message =
|
| 545 |
chat_history.append({"role": "user", "content": message})
|
| 546 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 547 |
return "", chat_history
|
|
|
|
| 548 |
msg.submit(respond, [msg, chatbot, file_input], [msg, chatbot])
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
with gr.Column(scale=2):
|
| 554 |
-
math_input = gr.Textbox(label="์ํ ๋ฌธ์ ", placeholder="์: 2x + 5 = 13", lines=3)
|
| 555 |
-
solve_btn = gr.Button("๋ฌธ์ ํ๊ธฐ", variant="primary")
|
| 556 |
-
with gr.Column(scale=1):
|
| 557 |
-
gr.Markdown("### ๐ ํ์ผ ์
๋ก๋")
|
| 558 |
-
math_file_input = gr.File(label="์ํ ๋ฌธ์ ํ์ผ (์ ํ์ฌํญ)", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 559 |
-
gr.Markdown("์ํ ๋ฌธ์ PDF๋ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ ์ ํํ ๋ต๋ณ์ ๋ฐ์ ์ ์์ต๋๋ค.")
|
| 560 |
-
with gr.Column(scale=2):
|
| 561 |
-
math_output = gr.Textbox(label="ํด๋ต", lines=8, interactive=False)
|
| 562 |
-
solve_btn.click(solve_math_problem, [math_input, math_file_input], math_output)
|
| 563 |
-
|
| 564 |
-
with gr.Tab("โ๏ธ ์ค์ "):
|
| 565 |
-
gr.Markdown("## ์์คํ
์ ๋ณด")
|
| 566 |
-
gr.Markdown(f"**๋ชจ๋ธ**: {MODEL_NAME}")
|
| 567 |
-
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: {'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}")
|
| 568 |
-
gr.Markdown(f"**ํ ํฐ ์ํ**: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 569 |
-
gr.Markdown("**๋ฒ์ **: 3.0.0 (๋ฉํฐ๋ชจ๋ฌ)")
|
| 570 |
-
gr.Markdown("**๊ธฐ๋ฅ**: ํ
์คํธ + ์ด๋ฏธ์ง ๋ฉํฐ๋ชจ๋ฌ ๋ํ")
|
| 571 |
|
| 572 |
if __name__ == "__main__":
|
| 573 |
-
|
| 574 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
import traceback
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import torch
|
| 6 |
import fitz # PyMuPDF
|
| 7 |
from PIL import Image
|
| 8 |
import io
|
|
|
|
| 9 |
|
| 10 |
+
# --- 1. ์ ์ญ ๋ณ์ ๋ฐ ํ๊ฒฝ ์ค์ ---
|
| 11 |
tokenizer = None
|
| 12 |
model = None
|
| 13 |
MODEL_LOADED = False
|
| 14 |
|
| 15 |
+
# .env ํ์ผ์์ ํ๊ฒฝ ๋ณ์ ๋ก๋ (์ฃผ๋ก ๋ก์ปฌ์์ ์ฌ์ฉ)
|
| 16 |
try:
|
| 17 |
from dotenv import load_dotenv
|
| 18 |
load_dotenv()
|
| 19 |
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 20 |
except ImportError:
|
| 21 |
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์, ์์คํ
ํ๊ฒฝ ๋ณ์ ์ฌ์ฉ")
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# ํ๊ฒฝ ๋ณ์์์ ํ ํฐ ๋ฐ ๋ชจ๋ธ ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
|
| 24 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 25 |
MODEL_NAME = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 26 |
|
| 27 |
+
print(f"๐ ๋ชจ๋ธ: {MODEL_NAME}")
|
| 28 |
+
print(f"๐ HF ํ ํฐ: {'โ
์ค์ ๋จ'if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# --- 2. ํต์ฌ ๋ก์ง: ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ---
|
| 31 |
try:
|
| 32 |
+
print("๐ง ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ์์...")
|
| 33 |
+
|
| 34 |
+
# ์ปค์คํ
๋ชจ๋ธ ํด๋์ค import
|
| 35 |
+
from modeling import KananaVForConditionalGeneration
|
| 36 |
+
|
| 37 |
if HF_TOKEN:
|
|
|
|
| 38 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 39 |
MODEL_NAME,
|
| 40 |
token=HF_TOKEN,
|
| 41 |
+
trust_remote_code=True
|
| 42 |
+
)
|
| 43 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 44 |
+
MODEL_NAME,
|
| 45 |
+
token=HF_TOKEN,
|
| 46 |
+
torch_dtype=torch.float16,
|
| 47 |
trust_remote_code=True,
|
| 48 |
+
device_map="auto" # GPU ์๋ ํ ๋น (์๋ฒ ํ๊ฒฝ์ ํ์)
|
| 49 |
)
|
| 50 |
+
MODEL_LOADED = True
|
| 51 |
+
print("โ
์ปค์คํ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
+
print("โ ๏ธ HF ํ ํฐ์ด ์์ด ๊ณต๊ฐ ๋ชจ๋ธ(DialoGPT)๋ก ๋์ฒดํฉ๋๋ค.")
|
| 54 |
MODEL_NAME = "microsoft/DialoGPT-medium"
|
|
|
|
|
|
|
|
|
|
| 55 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 56 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, device_map="auto")
|
| 57 |
+
MODEL_LOADED = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
|
|
|
|
|
|
|
|
|
| 61 |
traceback.print_exc()
|
| 62 |
MODEL_LOADED = False
|
| 63 |
|
| 64 |
+
# --- 3. ํ์ผ ์ฒ๋ฆฌ ์ ํธ๋ฆฌํฐ ---
|
|
|
|
|
|
|
|
|
|
| 65 |
def extract_text_from_pdf(pdf_file):
|
|
|
|
| 66 |
try:
|
| 67 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 68 |
+
text = "".join(page.get_text() for page in doc)
|
|
|
|
|
|
|
| 69 |
doc.close()
|
| 70 |
return text
|
| 71 |
except Exception as e:
|
| 72 |
+
print(f"PDF ์ฒ๋ฆฌ ์ค๋ฅ: {e}")
|
| 73 |
+
return f"PDF ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def process_uploaded_file(file):
|
| 76 |
+
"""์
๋ก๋๋ ํ์ผ์ ํ
์คํธ์ ์ด๋ฏธ์ง ๊ฐ์ฒด๋ก ๋ถ๋ฆฌ"""
|
| 77 |
if file is None:
|
| 78 |
+
return "", None # ํ
์คํธ, ์ด๋ฏธ์ง ์์
|
| 79 |
+
|
| 80 |
file_path = file.name
|
| 81 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
| 82 |
|
| 83 |
+
if file_extension == '.pdf':
|
| 84 |
text_content = extract_text_from_pdf(file)
|
| 85 |
+
return text_content, None # PDF๋ ํ
์คํธ๋ง, ์ด๋ฏธ์ง๋ ์์
|
| 86 |
+
elif file_extension in ['.png', '.jpg', '.jpeg']:
|
| 87 |
+
image = Image.open(file).convert('RGB')
|
| 88 |
+
# ์ด๋ฏธ์ง ํ์ผ ์์ฒด๋ฅผ ๋ฐํ (OCR ๋์ ๋ฉํฐ๋ชจ๋ฌ ์
๋ ฅ์ผ๋ก ์ฌ์ฉ)
|
| 89 |
+
return "์
๋ก๋๋ ์ด๋ฏธ์ง๊ฐ ์์ต๋๋ค.", image
|
| 90 |
else:
|
| 91 |
return f"์ง์ํ์ง ์๋ ํ์ผ ํ์: {file_extension}", None
|
| 92 |
|
| 93 |
+
# --- 4. ํต์ฌ ๋ก์ง: ํตํฉ ์๋ต ์์ฑ ํจ์ ---
|
| 94 |
+
def generate_response(prompt_template: str, message: str, file: Optional = None):
|
| 95 |
+
"""ํ
์คํธ์ ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ ์ฒ๋ฆฌํ๋ ํตํฉ ์๋ต ์์ฑ ํจ์"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if not MODEL_LOADED:
|
| 97 |
+
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค. ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํ์ธ์."
|
| 98 |
+
|
|
|
|
| 99 |
try:
|
| 100 |
+
# 1. ํ์ผ ์ฒ๋ฆฌ
|
| 101 |
+
file_text, pil_image = process_uploaded_file(file)
|
| 102 |
+
|
| 103 |
+
# 2. ์ ์ฒด ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 104 |
+
full_message = message
|
| 105 |
+
if file_text:
|
| 106 |
+
full_message += f"\n\n[์ฒจ๋ถ ํ์ผ ๋ด์ฉ]\n{file_text}"
|
| 107 |
+
|
| 108 |
+
full_prompt = prompt_template.format(message=full_message)
|
| 109 |
+
|
| 110 |
+
# 3. ํ ํฌ๋์ด์ ๋ก ํ
์คํธ ์
๋ ฅ ๋ณํ
|
| 111 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 112 |
+
|
| 113 |
+
# 4. ์์ฑ ํ๋ผ๋ฏธํฐ ์ค๋น
|
| 114 |
+
generation_args = {
|
| 115 |
+
"max_new_tokens": 512,
|
| 116 |
+
"temperature": 0.7,
|
| 117 |
+
"do_sample": True,
|
| 118 |
+
"pad_token_id": tokenizer.eos_token_id
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
# 5. ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ, ๋ฉํฐ๋ชจ๋ฌ ์
๋ ฅ ์ถ๊ฐ
|
| 122 |
+
if pil_image:
|
| 123 |
+
print("๐ผ๏ธ ์ด๋ฏธ์ง ํฌํจ, ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋๋ก ์์ฑ")
|
| 124 |
+
# KananaV ๋ชจ๋ธ์ ๋ง๋ ํํ๋ก ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 125 |
+
# (๋ชจ๋ธ์ ์๊ตฌ์ฌํญ์ ๋ฐ๋ผ ์ด ๋ถ๋ถ์ ๋ฌ๋ผ์ง ์ ์์ต๋๋ค)
|
| 126 |
+
pixel_values = model.vision_model.image_processor(pil_image, return_tensors='pt')['pixel_values']
|
| 127 |
+
generation_args["pixel_values"] = pixel_values.to(model.device, dtype=torch.float16)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
else:
|
| 129 |
+
print("๐ ํ
์คํธ๋ง์ผ๋ก ์์ฑ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
# 6. ๋ชจ๋ธ์ ํตํด ์๋ต ์์ฑ (๋จ ํ ๋ฒ์ ์ฌ๋ฐ๋ฅธ ํธ์ถ)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
with torch.no_grad():
|
| 133 |
+
outputs = model.generate(**inputs, **generation_args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
# 7. ์์ฑ๋ ํ ํฐ ID๋ฅผ ํ
์คํธ๋ก ๋์ฝ๋ฉ
|
| 136 |
+
# ์
๋ ฅ ํ๋กฌํํธ ๋ถ๋ถ์ ์ ์ธํ๊ณ ์์ํ ๋ต๋ณ๋ง ์ถ์ถ
|
| 137 |
+
input_length = inputs["input_ids"].shape[1]
|
| 138 |
+
response_ids = outputs[0][input_length:]
|
| 139 |
+
response = tokenizer.decode(response_ids, skip_special_tokens=True).strip()
|
| 140 |
|
| 141 |
+
return response
|
| 142 |
+
|
| 143 |
except Exception as e:
|
| 144 |
+
print(f"โ ์๋ต ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
|
|
|
|
|
|
| 145 |
traceback.print_exc()
|
| 146 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 147 |
|
| 148 |
+
# --- 5. Gradio UI ๋ฐ ์คํ ---
|
| 149 |
with gr.Blocks(title="Lily Math RAG System", theme=gr.themes.Soft()) as demo:
|
| 150 |
gr.Markdown("# ๐งฎ Lily Math RAG System")
|
| 151 |
+
gr.Markdown("์ํ ๋ฌธ์ ํด๊ฒฐ ๋ฐ ๋ฉํฐ๋ชจ๋ฌ ๋ํ๋ฅผ ์ํ AI ์์คํ
์
๋๋ค.")
|
| 152 |
+
|
| 153 |
with gr.Tabs():
|
| 154 |
with gr.Tab("๐ฌ ์ฑํ
"):
|
| 155 |
+
chat_prompt = "<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 156 |
+
chatbot = gr.Chatbot(height=500, label="๋ํ์ฐฝ", type="messages")
|
| 157 |
with gr.Row():
|
| 158 |
+
with gr.Column(scale=4):
|
| 159 |
+
msg = gr.Textbox(label="๋ฉ์์ง", placeholder="์ด๋ฏธ์ง๋ PDF๋ฅผ ์ฒจ๋ถํ๊ณ ์ง๋ฌธํด๋ณด์ธ์!", lines=3, show_label=False)
|
| 160 |
+
with gr.Column(scale=1, min_width=150):
|
| 161 |
+
file_input = gr.File(label="ํ์ผ ์
๋ก๋", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
def respond(message, chat_history, file):
|
| 164 |
+
bot_message = generate_response(chat_prompt, message, file)
|
| 165 |
chat_history.append({"role": "user", "content": message})
|
| 166 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 167 |
return "", chat_history
|
| 168 |
+
|
| 169 |
msg.submit(respond, [msg, chatbot, file_input], [msg, chatbot])
|
| 170 |
+
|
| 171 |
+
with gr.Tab("โ๏ธ ์์คํ
์ ๋ณด"):
|
| 172 |
+
gr.Markdown(f"**๋ชจ๋ธ**: `{MODEL_NAME}`")
|
| 173 |
+
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: `{'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}`")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
+
# share=True๋ฅผ ์ฌ์ฉํ๋ฉด ์ธ๋ถ์์๋ ์ ์ ๊ฐ๋ฅํ ๊ณต๊ฐ ๋งํฌ๊ฐ ์์ฑ๋ฉ๋๋ค.
|
| 177 |
+
demo.launch(share=True)
|
app_250807_0427.py
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
import traceback
|
| 6 |
+
from transformers import AutoTokenizer
|
| 7 |
+
import torch
|
| 8 |
+
import fitz # PyMuPDF
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import io
|
| 11 |
+
import base64
|
| 12 |
+
|
| 13 |
+
# ์ ์ญ ๋ณ์๋ก ์ ์ธ
|
| 14 |
+
tokenizer = None
|
| 15 |
+
model = None
|
| 16 |
+
MODEL_LOADED = False
|
| 17 |
+
|
| 18 |
+
# .env ํ์ผ์์ ํ๊ฒฝ ๋ณ์ ๋ก๋
|
| 19 |
+
try:
|
| 20 |
+
from dotenv import load_dotenv
|
| 21 |
+
load_dotenv()
|
| 22 |
+
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 23 |
+
except ImportError:
|
| 24 |
+
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์, ์์คํ
ํ๊ฒฝ ๋ณ์ ์ฌ์ฉ")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"โ ๏ธ .env ํ์ผ ๋ก๋ ์คํจ: {e}")
|
| 27 |
+
|
| 28 |
+
# ํ๊ฒฝ ๋ณ์์์๋ง ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ (๋ณด์)
|
| 29 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 30 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 31 |
+
|
| 32 |
+
print("๐ ์์ธ ๋๋ฒ๊น
์์")
|
| 33 |
+
print("=" * 50)
|
| 34 |
+
print(f"1. ํ๊ฒฝ ๋ณ์ ํ์ธ:")
|
| 35 |
+
print(f" HF_TOKEN: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 36 |
+
print(f" MODEL_NAME: {MODEL_NAME}")
|
| 37 |
+
if HF_TOKEN:
|
| 38 |
+
print(f" ํ ํฐ ๊ธธ์ด: {len(HF_TOKEN)}")
|
| 39 |
+
print(f" ํ ํฐ ์์: {HF_TOKEN[:10]}...")
|
| 40 |
+
print(f" ํ ํฐ ๋: ...{HF_TOKEN[-10:]}")
|
| 41 |
+
|
| 42 |
+
# ๋ชจ๋ธ ๋ก๋ (์ปค์คํ
๋ชจ๋ธ ํด๋์ค ์ฌ์ฉ)
|
| 43 |
+
try:
|
| 44 |
+
print(f"\n2. ๋ชจ๋ธ ๋ก๋ฉ ์์:")
|
| 45 |
+
print(f" ๋ชจ๋ธ: {MODEL_NAME}")
|
| 46 |
+
print(f" ํ ํฐ ์ฌ์ฉ: {'์' if HF_TOKEN else '์๋์ค'}")
|
| 47 |
+
|
| 48 |
+
if HF_TOKEN:
|
| 49 |
+
print(" ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 50 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 51 |
+
MODEL_NAME,
|
| 52 |
+
token=HF_TOKEN,
|
| 53 |
+
trust_remote_code=True,
|
| 54 |
+
use_fast=False
|
| 55 |
+
)
|
| 56 |
+
print(" โ
ํ ํฌ๋์ด์ ๋ก๋ฉ ์๋ฃ")
|
| 57 |
+
print(f" ํ ํฌ๋์ด์ ํ์
: {type(tokenizer)}")
|
| 58 |
+
print(f" ํ ํฌ๋์ด์ hasattr('encode'): {hasattr(tokenizer, 'encode')}")
|
| 59 |
+
|
| 60 |
+
print(" ์ปค์คํ
๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 61 |
+
# ์ปค์คํ
๋ชจ๋ธ ํด๋์ค import (Space ํด๋์ modeling.py ์ฌ์ฉ)
|
| 62 |
+
try:
|
| 63 |
+
from modeling import KananaVForConditionalGeneration
|
| 64 |
+
print(" โ
modeling.py import ์ฑ๊ณต")
|
| 65 |
+
except Exception as import_error:
|
| 66 |
+
print(f" โ modeling.py import ์คํจ: {import_error}")
|
| 67 |
+
raise import_error
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
print(f" ๋ชจ๋ธ ๋ก๋ฉ ํ๋ผ๋ฏธํฐ:")
|
| 71 |
+
print(f" MODEL_NAME: {MODEL_NAME}")
|
| 72 |
+
print(f" torch_dtype: {torch.float16}")
|
| 73 |
+
print(f" trust_remote_code: True")
|
| 74 |
+
print(f" device_map: None")
|
| 75 |
+
print(f" low_cpu_mem_usage: True")
|
| 76 |
+
|
| 77 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 78 |
+
MODEL_NAME,
|
| 79 |
+
token=HF_TOKEN,
|
| 80 |
+
torch_dtype=torch.float16,
|
| 81 |
+
trust_remote_code=True,
|
| 82 |
+
device_map=None,
|
| 83 |
+
low_cpu_mem_usage=True
|
| 84 |
+
)
|
| 85 |
+
print(" โ
์ปค์คํ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
| 86 |
+
print(f" ๋ชจ๋ธ ํ์
: {type(model)}")
|
| 87 |
+
print(f" ๋ชจ๋ธ ๋๋ฐ์ด์ค: {next(model.parameters()).device}")
|
| 88 |
+
except Exception as model_error:
|
| 89 |
+
print(f" โ ์ปค์คํ
๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {model_error}")
|
| 90 |
+
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 91 |
+
import traceback
|
| 92 |
+
traceback.print_exc()
|
| 93 |
+
raise model_error
|
| 94 |
+
else:
|
| 95 |
+
print(" โ ๏ธ ํ ํฐ์ด ์์ด์ ๊ณต๊ฐ ๋ชจ๋ธ ์ฌ์ฉ")
|
| 96 |
+
MODEL_NAME = "microsoft/DialoGPT-medium"
|
| 97 |
+
print(f" ๊ณต๊ฐ ๋ชจ๋ธ: {MODEL_NAME}")
|
| 98 |
+
|
| 99 |
+
print(" ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 100 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 101 |
+
print(" โ
ํ ํฌ๋์ด์ ๋ก๋ฉ ์๋ฃ")
|
| 102 |
+
|
| 103 |
+
print(" ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 104 |
+
from transformers import AutoModelForCausalLM
|
| 105 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
|
| 106 |
+
print(" โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
| 107 |
+
|
| 108 |
+
print("โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 109 |
+
MODEL_LOADED = True
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ:")
|
| 113 |
+
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 114 |
+
print(f" ์ค๋ฅ ๋ฉ์์ง: {str(e)}")
|
| 115 |
+
print(f" ์์ธ ์ค๋ฅ:")
|
| 116 |
+
traceback.print_exc()
|
| 117 |
+
MODEL_LOADED = False
|
| 118 |
+
|
| 119 |
+
print(f"\n3. ์ต์ข
์ํ:")
|
| 120 |
+
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 121 |
+
print(f" ์ต์ข
๋ชจ๋ธ๋ช
: {MODEL_NAME}")
|
| 122 |
+
|
| 123 |
+
def extract_text_from_pdf(pdf_file):
|
| 124 |
+
"""PDF์์ ํ
์คํธ ์ถ์ถ"""
|
| 125 |
+
try:
|
| 126 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 127 |
+
text = ""
|
| 128 |
+
for page in doc:
|
| 129 |
+
text += page.get_text()
|
| 130 |
+
doc.close()
|
| 131 |
+
return text
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return f"PDF ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}"
|
| 134 |
+
|
| 135 |
+
def extract_text_from_image(image_file):
|
| 136 |
+
"""์ด๋ฏธ์ง์์ OCR๋ก ํ
์คํธ ์ถ์ถ"""
|
| 137 |
+
try:
|
| 138 |
+
# PIL๋ก ์ด๋ฏธ์ง ์ด๊ธฐ
|
| 139 |
+
image = Image.open(image_file)
|
| 140 |
+
|
| 141 |
+
# ๊ฐ๋จํ OCR (์ค์ ๋ก๋ ๋ ์ ๊ตํ OCR ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ ํ์)
|
| 142 |
+
# ์ฌ๊ธฐ์๋ ์ด๋ฏธ์ง ์ ๋ณด๋ง ๋ฐํ
|
| 143 |
+
return f"์ด๋ฏธ์ง ํ์ผ: {image.size[0]}x{image.size[1]} ํฝ์
"
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return f"์ด๋ฏธ์ง ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}"
|
| 146 |
+
|
| 147 |
+
def process_uploaded_file(file):
|
| 148 |
+
"""์
๋ก๋๋ ํ์ผ ์ฒ๋ฆฌ"""
|
| 149 |
+
if file is None:
|
| 150 |
+
return None, None
|
| 151 |
+
|
| 152 |
+
file_path = file.name
|
| 153 |
+
file_extension = file_path.lower().split('.')[-1]
|
| 154 |
+
|
| 155 |
+
if file_extension == 'pdf':
|
| 156 |
+
text_content = extract_text_from_pdf(file)
|
| 157 |
+
return text_content, None
|
| 158 |
+
elif file_extension in ['png', 'jpg', 'jpeg']:
|
| 159 |
+
text_content = extract_text_from_image(file)
|
| 160 |
+
return text_content, file
|
| 161 |
+
else:
|
| 162 |
+
return f"์ง์ํ์ง ์๋ ํ์ผ ํ์: {file_extension}", None
|
| 163 |
+
|
| 164 |
+
def chat_with_model(message, history, file=None):
|
| 165 |
+
global tokenizer, model
|
| 166 |
+
print(f"๐ DEBUG: chat_with_model ์์")
|
| 167 |
+
print(f" ๋ฉ์์ง: {message}")
|
| 168 |
+
print(f" ํ์ผ: {file}")
|
| 169 |
+
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 170 |
+
|
| 171 |
+
if not MODEL_LOADED:
|
| 172 |
+
print("โ DEBUG: ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์")
|
| 173 |
+
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 174 |
+
|
| 175 |
+
try:
|
| 176 |
+
print("๐ DEBUG: ํ์ผ ์ฒ๋ฆฌ ์์")
|
| 177 |
+
# ํ์ผ ์ฒ๋ฆฌ
|
| 178 |
+
file_content = ""
|
| 179 |
+
image_file = None
|
| 180 |
+
if file is not None:
|
| 181 |
+
print(f" ํ์ผ๋ช
: {file.name}")
|
| 182 |
+
text_content, image_file = process_uploaded_file(file)
|
| 183 |
+
print(f" ํ
์คํธ ๋ด์ฉ: {text_content[:100] if text_content else 'None'}...")
|
| 184 |
+
print(f" ์ด๋ฏธ์ง ํ์ผ: {image_file}")
|
| 185 |
+
if text_content:
|
| 186 |
+
file_content = f"\n[์
๋ก๋๋ ํ์ผ ๋ด์ฉ]\n{text_content}\n"
|
| 187 |
+
|
| 188 |
+
# ๋ฉ์์ง์ ํ์ผ ๋ด์ฉ ์ถ๊ฐ
|
| 189 |
+
full_message = message + file_content
|
| 190 |
+
print(f"๐ DEBUG: ์ ์ฒด ๋ฉ์์ง: {full_message[:200]}...")
|
| 191 |
+
|
| 192 |
+
print("๐ค DEBUG: ํ ํฌ๋์ด์ ์ฒ๋ฆฌ ์์")
|
| 193 |
+
print(f" tokenizer ํ์
: {type(tokenizer)}")
|
| 194 |
+
print(f" tokenizer ๊ฐ: {tokenizer}")
|
| 195 |
+
|
| 196 |
+
# tokenizer๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธ
|
| 197 |
+
if not hasattr(tokenizer, 'encode') or tokenizer is None or isinstance(tokenizer, bool):
|
| 198 |
+
print("โ DEBUG: tokenizer๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์")
|
| 199 |
+
# tokenizer๋ฅผ ๋ค์ ๋ก๋
|
| 200 |
+
print("๐ DEBUG: tokenizer ์ฌ๋ก๋ ์๋")
|
| 201 |
+
try:
|
| 202 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 203 |
+
MODEL_NAME,
|
| 204 |
+
token=HF_TOKEN,
|
| 205 |
+
trust_remote_code=True,
|
| 206 |
+
use_fast=False
|
| 207 |
+
)
|
| 208 |
+
print("โ
DEBUG: tokenizer ์ฌ๋ก๋ ์ฑ๊ณต")
|
| 209 |
+
print(f" ์๋ก์ด tokenizer ํ์
: {type(tokenizer)}")
|
| 210 |
+
except Exception as reload_error:
|
| 211 |
+
print(f"โ DEBUG: tokenizer ์ฌ๋ก๋ ์คํจ: {reload_error}")
|
| 212 |
+
return f"ํ ํฌ๋์ด์ ์ค๋ฅ: {str(reload_error)}"
|
| 213 |
+
|
| 214 |
+
inputs = tokenizer(full_message, return_tensors="pt")
|
| 215 |
+
print(f" ์
๋ ฅ shape: {inputs['input_ids'].shape}")
|
| 216 |
+
print(f" attention_mask shape: {inputs['attention_mask'].shape}")
|
| 217 |
+
|
| 218 |
+
print("๐ค DEBUG: ๋ชจ๋ธ ์ถ๋ก ์์")
|
| 219 |
+
with torch.no_grad():
|
| 220 |
+
if image_file is not None:
|
| 221 |
+
print("๐ผ๏ธ DEBUG: ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 222 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฉํฐ๋ชจ๋ฌ ์์ฑ
|
| 223 |
+
import torchvision.transforms as transforms
|
| 224 |
+
|
| 225 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 226 |
+
transform = transforms.Compose([
|
| 227 |
+
transforms.Resize((224, 224)),
|
| 228 |
+
transforms.ToTensor(),
|
| 229 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 230 |
+
])
|
| 231 |
+
|
| 232 |
+
pil_image = Image.open(image_file).convert('RGB')
|
| 233 |
+
pixel_values = transform(pil_image).unsqueeze(0)
|
| 234 |
+
image_metas = {"vision_grid_thw": torch.tensor([[1, 14, 14]])} # ๊ธฐ๋ณธ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ
|
| 235 |
+
|
| 236 |
+
print(f" ์ด๋ฏธ์ง shape: {pixel_values.shape}")
|
| 237 |
+
print(f" ์ด๋ฏธ์ง ๋ฉํ: {image_metas}")
|
| 238 |
+
|
| 239 |
+
# ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ์ forward ๋ฉ์๋ ์ฌ์ฉ
|
| 240 |
+
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (๋ฉํฐ๋ชจ๋ฌ)")
|
| 241 |
+
try:
|
| 242 |
+
outputs = model(
|
| 243 |
+
input_ids=inputs["input_ids"],
|
| 244 |
+
attention_mask=inputs["attention_mask"],
|
| 245 |
+
pixel_values=[pixel_values],
|
| 246 |
+
image_metas=image_metas,
|
| 247 |
+
max_new_tokens=200,
|
| 248 |
+
temperature=0.7,
|
| 249 |
+
do_sample=True,
|
| 250 |
+
pad_token_id=tokenizer.eos_token_id
|
| 251 |
+
)
|
| 252 |
+
print("โ
DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 253 |
+
except Exception as model_error:
|
| 254 |
+
print(f"โ DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 255 |
+
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 256 |
+
raise model_error
|
| 257 |
+
else:
|
| 258 |
+
print("๐ DEBUG: ํ
์คํธ๋ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 259 |
+
# ํ
์คํธ๋ง ์์ฑ
|
| 260 |
+
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (ํ
์คํธ๋ง)")
|
| 261 |
+
try:
|
| 262 |
+
outputs = model(
|
| 263 |
+
input_ids=inputs["input_ids"],
|
| 264 |
+
attention_mask=inputs["attention_mask"],
|
| 265 |
+
max_new_tokens=200,
|
| 266 |
+
temperature=0.7,
|
| 267 |
+
do_sample=True,
|
| 268 |
+
pad_token_id=tokenizer.eos_token_id
|
| 269 |
+
)
|
| 270 |
+
print("โ
DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 271 |
+
except Exception as model_error:
|
| 272 |
+
print(f"โ DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 273 |
+
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 274 |
+
raise model_error
|
| 275 |
+
|
| 276 |
+
print("๐ DEBUG: ์ถ๋ ฅ ์ฒ๋ฆฌ ์์")
|
| 277 |
+
print(f" outputs ํ์
: {type(outputs)}")
|
| 278 |
+
print(f" outputs ๋ด์ฉ: {outputs}")
|
| 279 |
+
|
| 280 |
+
# outputs๊ฐ ํํ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์์ ์ฌ์ฉ
|
| 281 |
+
if isinstance(outputs, tuple):
|
| 282 |
+
print("๐ฆ DEBUG: outputs๊ฐ ํํ์")
|
| 283 |
+
logits = outputs[0]
|
| 284 |
+
print(f" logits shape: {logits.shape}")
|
| 285 |
+
else:
|
| 286 |
+
print("๐ฆ DEBUG: outputs๊ฐ ๊ฐ์ฒด์")
|
| 287 |
+
if hasattr(outputs, 'logits'):
|
| 288 |
+
logits = outputs.logits
|
| 289 |
+
print(f" logits shape: {logits.shape}")
|
| 290 |
+
else:
|
| 291 |
+
logits = outputs
|
| 292 |
+
print(f" outputs shape: {logits.shape}")
|
| 293 |
+
|
| 294 |
+
print("๐ฏ DEBUG: ํ ํฐ ์์ฑ ์์")
|
| 295 |
+
# ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ํ ํฐ ์ ํ
|
| 296 |
+
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 297 |
+
generated_tokens = [next_token]
|
| 298 |
+
print(f" ์ฒซ ๋ฒ์งธ ํ ํฐ: {next_token.item()}")
|
| 299 |
+
|
| 300 |
+
# ์ถ๊ฐ ํ ํฐ ์์ฑ
|
| 301 |
+
print("๐ DEBUG: ๋ฐ๋ณต ํ ํฐ ์์ฑ ์์")
|
| 302 |
+
for i in range(199): # max_new_tokens - 1
|
| 303 |
+
if i % 50 == 0:
|
| 304 |
+
print(f" ์งํ๋ฅ : {i}/199")
|
| 305 |
+
|
| 306 |
+
inputs["input_ids"] = torch.cat([inputs["input_ids"], next_token.unsqueeze(-1)], dim=-1)
|
| 307 |
+
inputs["attention_mask"] = torch.cat([inputs["attention_mask"], torch.ones_like(next_token.unsqueeze(-1))], dim=-1)
|
| 308 |
+
|
| 309 |
+
with torch.no_grad():
|
| 310 |
+
try:
|
| 311 |
+
outputs = model(**inputs)
|
| 312 |
+
if isinstance(outputs, tuple):
|
| 313 |
+
logits = outputs[0]
|
| 314 |
+
else:
|
| 315 |
+
logits = outputs.logits if hasattr(outputs, 'logits') else outputs
|
| 316 |
+
|
| 317 |
+
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 318 |
+
generated_tokens.append(next_token)
|
| 319 |
+
|
| 320 |
+
if next_token.item() == tokenizer.eos_token_id:
|
| 321 |
+
print(f" EOS ํ ํฐ ๋ฐ๊ฒฌ: {i}๋ฒ์งธ")
|
| 322 |
+
break
|
| 323 |
+
except Exception as loop_error:
|
| 324 |
+
print(f"โ DEBUG: ํ ํฐ ์์ฑ ๋ฃจํ ์ค๋ฅ (i={i}): {loop_error}")
|
| 325 |
+
raise loop_error
|
| 326 |
+
|
| 327 |
+
print("๐ค DEBUG: ํ ํฐ ๋์ฝ๋ฉ ์์")
|
| 328 |
+
# ์์ฑ๋ ํ ํฐ๋ค์ ๋์ฝ๋ฉ
|
| 329 |
+
generated_ids = torch.cat(generated_tokens, dim=0)
|
| 330 |
+
response = tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 331 |
+
print(f" ์๋ณธ ์๋ต: {response[:200]}...")
|
| 332 |
+
|
| 333 |
+
if full_message in response:
|
| 334 |
+
response = response.replace(full_message, "").strip()
|
| 335 |
+
print(f" ์ ๋ฆฌ๋ ์๋ต: {response[:200]}...")
|
| 336 |
+
|
| 337 |
+
print("โ
DEBUG: chat_with_model ์๋ฃ")
|
| 338 |
+
return response if response else "์ฃ์กํฉ๋๋ค. ์๋ต์ ์์ฑํ ์ ์์ต๋๋ค."
|
| 339 |
+
except Exception as e:
|
| 340 |
+
print(f"โ DEBUG: chat_with_model ์ ์ฒด ์ค๋ฅ: {e}")
|
| 341 |
+
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 342 |
+
import traceback
|
| 343 |
+
traceback.print_exc()
|
| 344 |
+
return f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 345 |
+
|
| 346 |
+
def solve_math_problem(problem, file=None):
|
| 347 |
+
global tokenizer, model
|
| 348 |
+
print(f"๐ DEBUG: solve_math_problem ์์")
|
| 349 |
+
print(f" ๋ฌธ์ : {problem}")
|
| 350 |
+
print(f" ํ์ผ: {file}")
|
| 351 |
+
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 352 |
+
|
| 353 |
+
if not MODEL_LOADED:
|
| 354 |
+
print("โ DEBUG: ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์")
|
| 355 |
+
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 356 |
+
|
| 357 |
+
try:
|
| 358 |
+
print("๐ DEBUG: ํ์ผ ์ฒ๋ฆฌ ์์")
|
| 359 |
+
# ํ์ผ ์ฒ๋ฆฌ
|
| 360 |
+
file_content = ""
|
| 361 |
+
image_file = None
|
| 362 |
+
if file is not None:
|
| 363 |
+
print(f" ํ์ผ๋ช
: {file.name}")
|
| 364 |
+
text_content, image_file = process_uploaded_file(file)
|
| 365 |
+
print(f" ํ
์คํธ ๋ด์ฉ: {text_content[:100] if text_content else 'None'}...")
|
| 366 |
+
print(f" ์ด๋ฏธ์ง ํ์ผ: {image_file}")
|
| 367 |
+
if text_content:
|
| 368 |
+
file_content = f"\n[์
๋ก๋๋ ํ์ผ ๋ด์ฉ]\n{text_content}\n"
|
| 369 |
+
|
| 370 |
+
# ๋ฉ์์ง์ ํ์ผ ๋ด์ฉ ์ถ๊ฐ
|
| 371 |
+
full_prompt = f"๋ค์ ์ํ ๋ฌธ์ ๋ฅผ ๋จ๊ณ๋ณ๋ก ํ์ด์ฃผ์ธ์: {problem}{file_content}"
|
| 372 |
+
print(f"๐ DEBUG: ์ ์ฒด ํ๋กฌํํธ: {full_prompt[:200]}...")
|
| 373 |
+
|
| 374 |
+
print("๐ค DEBUG: ํ ํฌ๋์ด์ ์ฒ๋ฆฌ ์์")
|
| 375 |
+
print(f" tokenizer ํ์
: {type(tokenizer)}")
|
| 376 |
+
print(f" tokenizer ๊ฐ: {tokenizer}")
|
| 377 |
+
|
| 378 |
+
# tokenizer๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธ
|
| 379 |
+
if not hasattr(tokenizer, 'encode') or tokenizer is None or isinstance(tokenizer, bool):
|
| 380 |
+
print("โ DEBUG: tokenizer๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์")
|
| 381 |
+
# tokenizer๋ฅผ ๋ค์ ๋ก๋
|
| 382 |
+
print("๐ DEBUG: tokenizer ์ฌ๋ก๋ ์๋")
|
| 383 |
+
try:
|
| 384 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 385 |
+
MODEL_NAME,
|
| 386 |
+
token=HF_TOKEN,
|
| 387 |
+
trust_remote_code=True,
|
| 388 |
+
use_fast=False
|
| 389 |
+
)
|
| 390 |
+
print("โ
DEBUG: tokenizer ์ฌ๋ก๋ ์ฑ๊ณต")
|
| 391 |
+
print(f" ์๋ก์ด tokenizer ํ์
: {type(tokenizer)}")
|
| 392 |
+
except Exception as reload_error:
|
| 393 |
+
print(f"โ DEBUG: tokenizer ์ฌ๋ก๋ ์คํจ: {reload_error}")
|
| 394 |
+
return f"ํ ํฌ๋์ด์ ์ค๋ฅ: {str(reload_error)}"
|
| 395 |
+
|
| 396 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
| 397 |
+
print(f" ์
๋ ฅ shape: {inputs['input_ids'].shape}")
|
| 398 |
+
print(f" attention_mask shape: {inputs['attention_mask'].shape}")
|
| 399 |
+
|
| 400 |
+
print("๐ค DEBUG: ๋ชจ๋ธ ์ถ๋ก ์์")
|
| 401 |
+
with torch.no_grad():
|
| 402 |
+
if image_file is not None:
|
| 403 |
+
print("๐ผ๏ธ DEBUG: ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 404 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฉํฐ๋ชจ๋ฌ ์์ฑ
|
| 405 |
+
import torchvision.transforms as transforms
|
| 406 |
+
|
| 407 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 408 |
+
transform = transforms.Compose([
|
| 409 |
+
transforms.Resize((224, 224)),
|
| 410 |
+
transforms.ToTensor(),
|
| 411 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 412 |
+
])
|
| 413 |
+
|
| 414 |
+
pil_image = Image.open(image_file).convert('RGB')
|
| 415 |
+
pixel_values = transform(pil_image).unsqueeze(0)
|
| 416 |
+
image_metas = {"vision_grid_thw": torch.tensor([[1, 14, 14]])} # ๊ธฐ๋ณธ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ
|
| 417 |
+
|
| 418 |
+
print(f" ์ด๋ฏธ์ง shape: {pixel_values.shape}")
|
| 419 |
+
print(f" ์ด๋ฏธ์ง ๋ฉํ: {image_metas}")
|
| 420 |
+
|
| 421 |
+
# ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ์ forward ๋ฉ์๋ ์ฌ์ฉ
|
| 422 |
+
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (๋ฉํฐ๋ชจ๋ฌ)")
|
| 423 |
+
try:
|
| 424 |
+
outputs = model(
|
| 425 |
+
input_ids=inputs["input_ids"],
|
| 426 |
+
attention_mask=inputs["attention_mask"],
|
| 427 |
+
pixel_values=[pixel_values],
|
| 428 |
+
image_metas=image_metas,
|
| 429 |
+
max_new_tokens=300,
|
| 430 |
+
temperature=0.3,
|
| 431 |
+
do_sample=True,
|
| 432 |
+
pad_token_id=tokenizer.eos_token_id
|
| 433 |
+
)
|
| 434 |
+
print("โ
DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 435 |
+
except Exception as model_error:
|
| 436 |
+
print(f"โ DEBUG: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 437 |
+
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 438 |
+
raise model_error
|
| 439 |
+
else:
|
| 440 |
+
print("๐ DEBUG: ํ
์คํธ๋ง ์ฒ๋ฆฌ ๋ชจ๋")
|
| 441 |
+
# ํ
์คํธ๋ง ์์ฑ
|
| 442 |
+
print("๐ DEBUG: ๋ชจ๋ธ ํธ์ถ (ํ
์คํธ๋ง)")
|
| 443 |
+
try:
|
| 444 |
+
outputs = model(
|
| 445 |
+
input_ids=inputs["input_ids"],
|
| 446 |
+
attention_mask=inputs["attention_mask"],
|
| 447 |
+
max_new_tokens=300,
|
| 448 |
+
temperature=0.3,
|
| 449 |
+
do_sample=True,
|
| 450 |
+
pad_token_id=tokenizer.eos_token_id
|
| 451 |
+
)
|
| 452 |
+
print("โ
DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 453 |
+
except Exception as model_error:
|
| 454 |
+
print(f"โ DEBUG: ํ
์คํธ ๋ชจ๋ธ ํธ์ถ ์คํจ: {model_error}")
|
| 455 |
+
print(f" ์ค๋ฅ ํ์
: {type(model_error).__name__}")
|
| 456 |
+
raise model_error
|
| 457 |
+
|
| 458 |
+
print("๐ DEBUG: ์ถ๋ ฅ ์ฒ๋ฆฌ ์์")
|
| 459 |
+
print(f" outputs ํ์
: {type(outputs)}")
|
| 460 |
+
print(f" outputs ๋ด์ฉ: {outputs}")
|
| 461 |
+
|
| 462 |
+
# outputs๊ฐ ํํ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์์ ์ฌ์ฉ
|
| 463 |
+
if isinstance(outputs, tuple):
|
| 464 |
+
print("๐ฆ DEBUG: outputs๊ฐ ํํ์")
|
| 465 |
+
logits = outputs[0]
|
| 466 |
+
print(f" logits shape: {logits.shape}")
|
| 467 |
+
else:
|
| 468 |
+
print("๐ฆ DEBUG: outputs๊ฐ ๊ฐ์ฒด์")
|
| 469 |
+
if hasattr(outputs, 'logits'):
|
| 470 |
+
logits = outputs.logits
|
| 471 |
+
print(f" logits shape: {logits.shape}")
|
| 472 |
+
else:
|
| 473 |
+
logits = outputs
|
| 474 |
+
print(f" outputs shape: {logits.shape}")
|
| 475 |
+
|
| 476 |
+
print("๐ฏ DEBUG: ํ ํฐ ์์ฑ ์์")
|
| 477 |
+
# ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ํ ํฐ ์ ํ
|
| 478 |
+
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 479 |
+
generated_tokens = [next_token]
|
| 480 |
+
print(f" ์ฒซ ๋ฒ์งธ ํ ํฐ: {next_token.item()}")
|
| 481 |
+
|
| 482 |
+
# ์ถ๊ฐ ํ ํฐ ์์ฑ
|
| 483 |
+
print("๐ DEBUG: ๋ฐ๋ณต ํ ํฐ ์์ฑ ์์")
|
| 484 |
+
for i in range(299): # max_new_tokens - 1
|
| 485 |
+
if i % 50 == 0:
|
| 486 |
+
print(f" ์งํ๋ฅ : {i}/299")
|
| 487 |
+
|
| 488 |
+
inputs["input_ids"] = torch.cat([inputs["input_ids"], next_token.unsqueeze(-1)], dim=-1)
|
| 489 |
+
inputs["attention_mask"] = torch.cat([inputs["attention_mask"], torch.ones_like(next_token.unsqueeze(-1))], dim=-1)
|
| 490 |
+
|
| 491 |
+
with torch.no_grad():
|
| 492 |
+
try:
|
| 493 |
+
outputs = model(**inputs)
|
| 494 |
+
if isinstance(outputs, tuple):
|
| 495 |
+
logits = outputs[0]
|
| 496 |
+
else:
|
| 497 |
+
logits = outputs.logits if hasattr(outputs, 'logits') else outputs
|
| 498 |
+
|
| 499 |
+
next_token = torch.argmax(logits[:, -1, :], dim=-1)
|
| 500 |
+
generated_tokens.append(next_token)
|
| 501 |
+
|
| 502 |
+
if next_token.item() == tokenizer.eos_token_id:
|
| 503 |
+
print(f" EOS ํ ํฐ ๋ฐ๊ฒฌ: {i}๋ฒ์งธ")
|
| 504 |
+
break
|
| 505 |
+
except Exception as loop_error:
|
| 506 |
+
print(f"โ DEBUG: ํ ํฐ ์์ฑ ๋ฃจํ ์ค๋ฅ (i={i}): {loop_error}")
|
| 507 |
+
raise loop_error
|
| 508 |
+
|
| 509 |
+
print("๐ค DEBUG: ํ ํฐ ๋์ฝ๋ฉ ์์")
|
| 510 |
+
# ์์ฑ๋ ํ ํฐ๋ค์ ๋์ฝ๋ฉ
|
| 511 |
+
generated_ids = torch.cat(generated_tokens, dim=0)
|
| 512 |
+
response = tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 513 |
+
print(f" ์๋ณธ ์๋ต: {response[:200]}...")
|
| 514 |
+
|
| 515 |
+
if full_prompt in response:
|
| 516 |
+
response = response.replace(full_prompt, "").strip()
|
| 517 |
+
print(f" ์ ๋ฆฌ๋ ์๋ต: {response[:200]}...")
|
| 518 |
+
|
| 519 |
+
print("โ
DEBUG: solve_math_problem ์๋ฃ")
|
| 520 |
+
return response if response else "์ฃ์กํฉ๋๋ค. ์ํ ๋ฌธ์ ๋ฅผ ํ ์ ์์ต๋๋ค."
|
| 521 |
+
except Exception as e:
|
| 522 |
+
print(f"โ DEBUG: solve_math_problem ์ ์ฒด ์ค๋ฅ: {e}")
|
| 523 |
+
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 524 |
+
import traceback
|
| 525 |
+
traceback.print_exc()
|
| 526 |
+
return f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 527 |
+
|
| 528 |
+
with gr.Blocks(title="Lily Math RAG System", theme=gr.themes.Soft()) as demo:
|
| 529 |
+
gr.Markdown("# ๐งฎ Lily Math RAG System")
|
| 530 |
+
gr.Markdown("์ํ ๋ฌธ์ ํด๊ฒฐ์ ์ํ AI ์์คํ
์
๋๋ค.")
|
| 531 |
+
with gr.Tabs():
|
| 532 |
+
with gr.Tab("๐ฌ ์ฑํ
"):
|
| 533 |
+
with gr.Row():
|
| 534 |
+
with gr.Column(scale=3):
|
| 535 |
+
chatbot = gr.Chatbot(height=400, type="messages")
|
| 536 |
+
msg = gr.Textbox(label="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์", placeholder="์๋
ํ์ธ์! ์ํ ๋ฌธ์ ๋ฅผ ๋์์ฃผ์ธ์.", lines=2)
|
| 537 |
+
clear = gr.Button("๋ํ ์ด๊ธฐํ")
|
| 538 |
+
with gr.Column(scale=1):
|
| 539 |
+
gr.Markdown("### ๐ ํ์ผ ์
๋ก๋")
|
| 540 |
+
file_input = gr.File(label="PDF/์ด๋ฏธ์ง ํ์ผ (์ ํ์ฌํญ)", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 541 |
+
gr.Markdown("PDF๋ ์ด๋ฏธ์ง ํ์ผ์ ์
๋ก๋ํ๋ฉด ๋ฌธ์๋ฅผ ํด์ํ์ฌ ๋ต๋ณํฉ๋๋ค.")
|
| 542 |
+
|
| 543 |
+
def respond(message, chat_history, file):
|
| 544 |
+
bot_message = chat_with_model(message, chat_history, file)
|
| 545 |
+
chat_history.append({"role": "user", "content": message})
|
| 546 |
+
chat_history.append({"role": "assistant", "content": bot_message})
|
| 547 |
+
return "", chat_history
|
| 548 |
+
msg.submit(respond, [msg, chatbot, file_input], [msg, chatbot])
|
| 549 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 550 |
+
|
| 551 |
+
with gr.Tab("๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ"):
|
| 552 |
+
with gr.Row():
|
| 553 |
+
with gr.Column(scale=2):
|
| 554 |
+
math_input = gr.Textbox(label="์ํ ๋ฌธ์ ", placeholder="์: 2x + 5 = 13", lines=3)
|
| 555 |
+
solve_btn = gr.Button("๋ฌธ์ ํ๊ธฐ", variant="primary")
|
| 556 |
+
with gr.Column(scale=1):
|
| 557 |
+
gr.Markdown("### ๐ ํ์ผ ์
๋ก๋")
|
| 558 |
+
math_file_input = gr.File(label="์ํ ๋ฌธ์ ํ์ผ (์ ํ์ฌํญ)", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 559 |
+
gr.Markdown("์ํ ๋ฌธ์ PDF๋ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ ์ ํํ ๋ต๋ณ์ ๋ฐ์ ์ ์์ต๋๋ค.")
|
| 560 |
+
with gr.Column(scale=2):
|
| 561 |
+
math_output = gr.Textbox(label="ํด๋ต", lines=8, interactive=False)
|
| 562 |
+
solve_btn.click(solve_math_problem, [math_input, math_file_input], math_output)
|
| 563 |
+
|
| 564 |
+
with gr.Tab("โ๏ธ ์ค์ "):
|
| 565 |
+
gr.Markdown("## ๏ฟฝ๏ฟฝ์คํ
์ ๋ณด")
|
| 566 |
+
gr.Markdown(f"**๋ชจ๋ธ**: {MODEL_NAME}")
|
| 567 |
+
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: {'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}")
|
| 568 |
+
gr.Markdown(f"**ํ ํฐ ์ํ**: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 569 |
+
gr.Markdown("**๋ฒ์ **: 3.0.0 (๋ฉํฐ๋ชจ๋ฌ)")
|
| 570 |
+
gr.Markdown("**๊ธฐ๋ฅ**: ํ
์คํธ + ์ด๋ฏธ์ง ๋ฉํฐ๋ชจ๋ฌ ๋ํ")
|
| 571 |
+
|
| 572 |
+
if __name__ == "__main__":
|
| 573 |
+
demo.launch()
|
| 574 |
+
|
app_local.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import traceback
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import torch
|
| 6 |
+
from typing import Optional
|
| 7 |
+
import fitz # PyMuPDF
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import io
|
| 10 |
+
|
| 11 |
+
# --- 1. ์ ์ญ ๋ณ์ ๋ฐ ํ๊ฒฝ ์ค์ ---
|
| 12 |
+
|
| 13 |
+
# ์ ์ญ ๋ณ์ ์ ์ธ
|
| 14 |
+
tokenizer = None
|
| 15 |
+
model = None
|
| 16 |
+
MODEL_LOADED = False
|
| 17 |
+
|
| 18 |
+
# ํ๊ฒฝ ๊ฐ์ง (๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ์ธ์ง, ์๋ฒ์ธ์ง ํ์ธ)
|
| 19 |
+
IS_LOCAL = os.path.exists('../.env') or 'LOCAL_TEST' in os.environ
|
| 20 |
+
print(f"๐ ํ๊ฒฝ: {'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}")
|
| 21 |
+
|
| 22 |
+
# .env ํ์ผ์์ ํ๊ฒฝ ๋ณ์ ๋ก๋ (์ฃผ๋ก ๋ก์ปฌ์์ ์ฌ์ฉ)
|
| 23 |
+
try:
|
| 24 |
+
from dotenv import load_dotenv
|
| 25 |
+
if IS_LOCAL:
|
| 26 |
+
load_dotenv(dotenv_path='../.env')
|
| 27 |
+
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 28 |
+
except ImportError:
|
| 29 |
+
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์")
|
| 30 |
+
|
| 31 |
+
# ํ๊ฒฝ ๋ณ์์์ ํ ํฐ ๋ฐ ๋ชจ๋ธ ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
|
| 32 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 33 |
+
MODEL_NAME_SERVER = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 34 |
+
MODEL_PATH_LOCAL = "../lily_llm_core/models/kanana-1.5-v-3b-instruct"
|
| 35 |
+
|
| 36 |
+
# ์ต์ข
๋ชจ๋ธ ๊ฒฝ๋ก ์ค์
|
| 37 |
+
MODEL_PATH = MODEL_PATH_LOCAL if IS_LOCAL else MODEL_NAME_SERVER
|
| 38 |
+
|
| 39 |
+
print(f"๐ ๋ชจ๋ธ ๊ฒฝ๋ก: {MODEL_PATH}")
|
| 40 |
+
print(f"๐ HF ํ ํฐ: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 41 |
+
|
| 42 |
+
# --- 2. ํต์ฌ ๋ก์ง: ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ---
|
| 43 |
+
|
| 44 |
+
def load_model_and_tokenizer():
|
| 45 |
+
"""ํ๊ฒฝ์ ๋ง๊ฒ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ๋ก๋ํ๋ ํตํฉ ํจ์"""
|
| 46 |
+
global tokenizer, model # ์ ์ญ ๋ณ์ ์ฌ์ฉ ์ ์ธ
|
| 47 |
+
|
| 48 |
+
print("๐ง ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ์์...")
|
| 49 |
+
|
| 50 |
+
# modeling.py๋ ์ปค์คํ
๋ชจ๋ธ์ ์ฌ์ฉํ ๋๋ง import
|
| 51 |
+
from modeling import KananaVForConditionalGeneration
|
| 52 |
+
|
| 53 |
+
if IS_LOCAL:
|
| 54 |
+
# ๋ก์ปฌ ํ์ผ ์์คํ
์์ ๋ชจ๋ธ ๋ก๋
|
| 55 |
+
if not os.path.exists(MODEL_PATH):
|
| 56 |
+
raise FileNotFoundError(f"๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: {MODEL_PATH}")
|
| 57 |
+
|
| 58 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 59 |
+
MODEL_PATH,
|
| 60 |
+
trust_remote_code=True,
|
| 61 |
+
local_files_only=True # ๋ก์ปฌ ํ์ผ๋ง ์ฌ์ฉ
|
| 62 |
+
)
|
| 63 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 64 |
+
MODEL_PATH,
|
| 65 |
+
torch_dtype=torch.float16,
|
| 66 |
+
trust_remote_code=True,
|
| 67 |
+
local_files_only=True
|
| 68 |
+
)
|
| 69 |
+
else:
|
| 70 |
+
# Hugging Face Hub์์ ๋ชจ๋ธ ๋ก๋
|
| 71 |
+
if not HF_TOKEN:
|
| 72 |
+
# ํ ํฐ์ด ์์ผ๋ฉด ๊ณต๊ฐ๋ ๋ชจ๋ธ๋ก ๋์ฒด (์ ํ์ )
|
| 73 |
+
print("โ ๏ธ HF ํ ํฐ์ด ์์ด ๊ณต๊ฐ ๋ชจ๋ธ(DialoGPT)๋ก ๋์ฒดํฉ๋๋ค.")
|
| 74 |
+
public_model = "microsoft/DialoGPT-medium"
|
| 75 |
+
tokenizer = AutoTokenizer.from_pretrained(public_model)
|
| 76 |
+
model = AutoModelForCausalLM.from_pretrained(public_model, torch_dtype=torch.float16)
|
| 77 |
+
return
|
| 78 |
+
|
| 79 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 80 |
+
MODEL_PATH,
|
| 81 |
+
token=HF_TOKEN,
|
| 82 |
+
trust_remote_code=True
|
| 83 |
+
)
|
| 84 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 85 |
+
MODEL_PATH,
|
| 86 |
+
token=HF_TOKEN,
|
| 87 |
+
torch_dtype=torch.float16,
|
| 88 |
+
trust_remote_code=True,
|
| 89 |
+
device_map="auto" # GPU ์๋ ํ ๋น
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
print("โ
๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ์ฑ๊ณต!")
|
| 93 |
+
|
| 94 |
+
# --- 3. ํ์ผ ์ฒ๋ฆฌ ์ ํธ๋ฆฌํฐ ---
|
| 95 |
+
|
| 96 |
+
def extract_text_from_pdf(pdf_file):
|
| 97 |
+
"""PDF์์ ํ
์คํธ ์ถ์ถ"""
|
| 98 |
+
try:
|
| 99 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 100 |
+
text = "".join(page.get_text() for page in doc)
|
| 101 |
+
doc.close()
|
| 102 |
+
return f"\n\n--- PDF ๋ด์ฉ ---\n{text}\n--- PDF ๋ด์ฉ ๋ ---"
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"PDF ์ฝ๊ธฐ ์ค๋ฅ: {e}"
|
| 105 |
+
|
| 106 |
+
def extract_text_from_image(image_file):
|
| 107 |
+
"""์ด๋ฏธ์ง์์ OCR๋ก ํ
์คํธ ์ถ์ถ (ํ์ฌ๋ ํ์ผ ์ ๋ณด๋ง ๋ฐํ)"""
|
| 108 |
+
try:
|
| 109 |
+
# ์ค์ OCR ๋ผ์ด๋ธ๋ฌ๋ฆฌ(์: Tesseract) ์ฐ๋ ํ์
|
| 110 |
+
image = Image.open(image_file)
|
| 111 |
+
return f"\n\n--- ์ด๋ฏธ์ง ํ์ผ ์ ๋ณด: {image.format}, {image.size[0]}x{image.size[1]} ---"
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return f"์ด๋ฏธ์ง ์ฝ๊ธฐ ์ค๋ฅ: {e}"
|
| 114 |
+
|
| 115 |
+
def process_uploaded_file(file):
|
| 116 |
+
"""์
๋ก๋๋ ํ์ผ์ ์ข
๋ฅ์ ๋ง๊ฒ ์ฒ๋ฆฌ"""
|
| 117 |
+
if file is None:
|
| 118 |
+
return ""
|
| 119 |
+
|
| 120 |
+
file_path = file.name
|
| 121 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
| 122 |
+
|
| 123 |
+
if file_extension == '.pdf':
|
| 124 |
+
return extract_text_from_pdf(file)
|
| 125 |
+
elif file_extension in ['.png', '.jpg', '.jpeg']:
|
| 126 |
+
# TODO: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ์ ์ํ ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ๋ก์ง ์ถ๊ฐ
|
| 127 |
+
return extract_text_from_image(file)
|
| 128 |
+
else:
|
| 129 |
+
return f"์ง์ํ์ง ์๋ ํ์ผ ํ์: {file_extension}"
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# --- 4. ํต์ฌ ๋ก์ง: ์๋ต ์์ฑ ํจ์ ---
|
| 133 |
+
|
| 134 |
+
def generate_response(prompt_template: str, message: str, file: Optional = None):
|
| 135 |
+
"""ํตํฉ๋ ์๋ต ์์ฑ ํจ์"""
|
| 136 |
+
if not MODEL_LOADED:
|
| 137 |
+
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค. ์ฑ์ ์ฌ์์ํด์ฃผ์ธ์."
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
print("โ๏ธ ์๋ต ์์ฑ ์์...")
|
| 141 |
+
|
| 142 |
+
# 1. ํ์ผ ๋ด์ฉ ์ฒ๋ฆฌ
|
| 143 |
+
file_content = process_uploaded_file(file)
|
| 144 |
+
|
| 145 |
+
# 2. ์ ์ฒด ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 146 |
+
full_message = message + file_content
|
| 147 |
+
full_prompt = prompt_template.format(message=full_message)
|
| 148 |
+
|
| 149 |
+
print(f"๐ ์ ์ฒด ํ๋กฌํํธ (์ผ๋ถ): {full_prompt[:200]}...")
|
| 150 |
+
|
| 151 |
+
# 3. ํ ํฌ๋์ด์ ๋ก ์
๋ ฅ ๋ณํ
|
| 152 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 153 |
+
|
| 154 |
+
# 4. ๋ชจ๋ธ์ ํตํด ์๋ต ์์ฑ (๋จ ํ ๋ฒ์ ํธ์ถ)
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
outputs = model.generate(
|
| 157 |
+
**inputs,
|
| 158 |
+
max_new_tokens=512,
|
| 159 |
+
temperature=0.7,
|
| 160 |
+
top_p=0.9,
|
| 161 |
+
do_sample=True,
|
| 162 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 163 |
+
eos_token_id=tokenizer.eos_token_id
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# 5. ์์ฑ๋ ํ ํฐ ID๋ฅผ ํ
์คํธ๋ก ๋์ฝ๋ฉ
|
| 167 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 168 |
+
|
| 169 |
+
# 6. ์
๋ ฅ ํ๋กฌํํธ๋ฅผ ์๋ต์์ ์ ๊ฑฐํ์ฌ ์์ํ ๋ต๋ณ๋ง ์ถ์ถ
|
| 170 |
+
assistant_response = response_text.split("<|im_start|>assistant\n")[-1].strip()
|
| 171 |
+
|
| 172 |
+
print(f"๐ฌ ์์ฑ๋ ์๋ต (์ผ๋ถ): {assistant_response[:200]}...")
|
| 173 |
+
print("โ
์๋ต ์์ฑ ์๋ฃ")
|
| 174 |
+
|
| 175 |
+
return assistant_response if assistant_response else "์ฃ์กํฉ๋๋ค. ๋ต๋ณ์ ์์ฑํ ์ ์์ต๋๋ค."
|
| 176 |
+
|
| 177 |
+
except Exception as e:
|
| 178 |
+
print(f"โ ์๋ต ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 179 |
+
traceback.print_exc()
|
| 180 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# --- 5. Gradio UI ์ค์ ---
|
| 184 |
+
|
| 185 |
+
with gr.Blocks(title="Lily Math RAG System", theme=gr.themes.Soft()) as demo:
|
| 186 |
+
gr.Markdown("# ๐งฎ Lily Math RAG System")
|
| 187 |
+
gr.Markdown("์ํ ๋ฌธ์ ํด๊ฒฐ ๋ฐ ์ผ๋ฐ ๋ํ๋ฅผ ์ํ AI ์์คํ
์
๋๋ค.")
|
| 188 |
+
|
| 189 |
+
with gr.Tabs():
|
| 190 |
+
with gr.Tab("๐ฌ ์ผ๋ฐ ์ฑํ
"):
|
| 191 |
+
# ์ฑํ
ํ๋กฌํํธ ํ
ํ๋ฆฟ
|
| 192 |
+
chat_prompt = "<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
with gr.Column(scale=3):
|
| 196 |
+
chatbot = gr.Chatbot(height=500, label="๋ํ์ฐฝ", type="messages")
|
| 197 |
+
msg = gr.Textbox(label="๋ฉ์์ง", placeholder="์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?", lines=3)
|
| 198 |
+
clear = gr.Button("์ ๋ํ ์์")
|
| 199 |
+
with gr.Column(scale=1):
|
| 200 |
+
gr.Markdown("### ๐ ํ์ผ ์
๋ก๋ (์ ํ)")
|
| 201 |
+
chat_file = gr.File(label="PDF/์ด๋ฏธ์ง ํ์ผ", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 202 |
+
|
| 203 |
+
def respond(message, chat_history, file):
|
| 204 |
+
bot_message = generate_response(chat_prompt, message, file)
|
| 205 |
+
chat_history.append({"role": "user", "content": message})
|
| 206 |
+
chat_history.append({"role": "assistant", "content": bot_message})
|
| 207 |
+
return "", chat_history
|
| 208 |
+
|
| 209 |
+
msg.submit(respond, [msg, chatbot, chat_file], [msg, chatbot])
|
| 210 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 211 |
+
|
| 212 |
+
with gr.Tab("๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ"):
|
| 213 |
+
# ์ํ ๋ฌธ์ ํ์ด์ฉ ํ๋กฌํํธ ํ
ํ๋ฆฟ
|
| 214 |
+
math_prompt = "๋ค์ ์ํ ๋ฌธ์ ๋ฅผ ๋จ๊ณ๋ณ๋ก ์์ธํ ํ์ด์ฃผ์ธ์:\n\n{message}\n\n<|im_start|>assistant\n"
|
| 215 |
+
|
| 216 |
+
with gr.Row():
|
| 217 |
+
with gr.Column(scale=2):
|
| 218 |
+
math_input = gr.Textbox(label="์ํ ๋ฌธ์ ", placeholder="์: 2x + 5 = 13", lines=5)
|
| 219 |
+
gr.Markdown("### ๐ ๋ฌธ์ ํ์ผ ์
๋ก๋ (์ ํ)")
|
| 220 |
+
math_file = gr.File(label="PDF/์ด๋ฏธ์ง ํ์์ ๋ฌธ์ ", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 221 |
+
solve_btn = gr.Button("๋ฌธ์ ํ๊ธฐ", variant="primary")
|
| 222 |
+
with gr.Column(scale=2):
|
| 223 |
+
math_output = gr.Textbox(label="ํ์ด ๊ณผ์ ๋ฐ ์ ๋ต", lines=10, interactive=False)
|
| 224 |
+
|
| 225 |
+
solve_btn.click(lambda msg, file: generate_response(math_prompt, msg, file), [math_input, math_file], math_output)
|
| 226 |
+
|
| 227 |
+
with gr.Tab("โ๏ธ ์์คํ
์ ๋ณด"):
|
| 228 |
+
gr.Markdown(f"**๋ชจ๋ธ ๊ฒฝ๋ก**: `{MODEL_PATH}`")
|
| 229 |
+
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: `{'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}`")
|
| 230 |
+
gr.Markdown(f"**์คํ ํ๊ฒฝ**: `{'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}`")
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
# --- 6. ์ ํ๋ฆฌ์ผ์ด์
์คํ ---
|
| 234 |
+
|
| 235 |
+
if __name__ == "__main__":
|
| 236 |
+
try:
|
| 237 |
+
load_model_and_tokenizer()
|
| 238 |
+
MODEL_LOADED = True
|
| 239 |
+
except Exception as e:
|
| 240 |
+
print(f"โ ์ต์ข
์คํ ์คํจ: ์ ํ๋ฆฌ์ผ์ด์
์์์ ํ์ํ ๋ชจ๋ธ์ ๋ก๋ํ์ง ๋ชปํ์ต๋๋ค.")
|
| 241 |
+
print(f"์ค๋ฅ: {e}")
|
| 242 |
+
traceback.print_exc()
|
| 243 |
+
MODEL_LOADED = False
|
| 244 |
+
|
| 245 |
+
demo.launch(server_name="localhost", server_port=8006)
|
app_local_250807_0427.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import traceback
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import torch
|
| 6 |
+
from typing import Optional
|
| 7 |
+
import fitz # PyMuPDF
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import io
|
| 10 |
+
|
| 11 |
+
# --- 1. ์ ์ญ ๋ณ์ ๋ฐ ํ๊ฒฝ ์ค์ ---
|
| 12 |
+
|
| 13 |
+
# ์ ์ญ ๋ณ์ ์ ์ธ
|
| 14 |
+
tokenizer = None
|
| 15 |
+
model = None
|
| 16 |
+
MODEL_LOADED = False
|
| 17 |
+
|
| 18 |
+
# ํ๊ฒฝ ๊ฐ์ง (๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ์ธ์ง, ์๋ฒ์ธ์ง ํ์ธ)
|
| 19 |
+
IS_LOCAL = os.path.exists('../.env') or 'LOCAL_TEST' in os.environ
|
| 20 |
+
print(f"๐ ํ๊ฒฝ: {'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}")
|
| 21 |
+
|
| 22 |
+
# .env ํ์ผ์์ ํ๊ฒฝ ๋ณ์ ๋ก๋ (์ฃผ๋ก ๋ก์ปฌ์์ ์ฌ์ฉ)
|
| 23 |
+
try:
|
| 24 |
+
from dotenv import load_dotenv
|
| 25 |
+
if IS_LOCAL:
|
| 26 |
+
load_dotenv(dotenv_path='../.env')
|
| 27 |
+
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 28 |
+
except ImportError:
|
| 29 |
+
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์")
|
| 30 |
+
|
| 31 |
+
# ํ๊ฒฝ ๋ณ์์์ ํ ํฐ ๋ฐ ๋ชจ๋ธ ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
|
| 32 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 33 |
+
MODEL_NAME_SERVER = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 34 |
+
MODEL_PATH_LOCAL = "../lily_llm_core/models/kanana-1.5-v-3b-instruct"
|
| 35 |
+
|
| 36 |
+
# ์ต์ข
๋ชจ๋ธ ๊ฒฝ๋ก ์ค์
|
| 37 |
+
MODEL_PATH = MODEL_PATH_LOCAL if IS_LOCAL else MODEL_NAME_SERVER
|
| 38 |
+
|
| 39 |
+
print(f"๐ ๋ชจ๋ธ ๊ฒฝ๋ก: {MODEL_PATH}")
|
| 40 |
+
print(f"๐ HF ํ ํฐ: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 41 |
+
|
| 42 |
+
# --- 2. ํต์ฌ ๋ก์ง: ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ---
|
| 43 |
+
|
| 44 |
+
def load_model_and_tokenizer():
|
| 45 |
+
"""ํ๊ฒฝ์ ๋ง๊ฒ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ๋ก๋ํ๋ ํตํฉ ํจ์"""
|
| 46 |
+
global tokenizer, model # ์ ์ญ ๋ณ์ ์ฌ์ฉ ์ ์ธ
|
| 47 |
+
|
| 48 |
+
print("๐ง ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ์์...")
|
| 49 |
+
|
| 50 |
+
# modeling.py๋ ์ปค์คํ
๋ชจ๋ธ์ ์ฌ์ฉํ ๋๋ง import
|
| 51 |
+
from modeling import KananaVForConditionalGeneration
|
| 52 |
+
|
| 53 |
+
if IS_LOCAL:
|
| 54 |
+
# ๋ก์ปฌ ํ์ผ ์์คํ
์์ ๋ชจ๋ธ ๋ก๋
|
| 55 |
+
if not os.path.exists(MODEL_PATH):
|
| 56 |
+
raise FileNotFoundError(f"๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: {MODEL_PATH}")
|
| 57 |
+
|
| 58 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 59 |
+
MODEL_PATH,
|
| 60 |
+
trust_remote_code=True,
|
| 61 |
+
local_files_only=True # ๋ก์ปฌ ํ์ผ๋ง ์ฌ์ฉ
|
| 62 |
+
)
|
| 63 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 64 |
+
MODEL_PATH,
|
| 65 |
+
torch_dtype=torch.float16,
|
| 66 |
+
trust_remote_code=True,
|
| 67 |
+
local_files_only=True
|
| 68 |
+
)
|
| 69 |
+
else:
|
| 70 |
+
# Hugging Face Hub์์ ๋ชจ๋ธ ๋ก๋
|
| 71 |
+
if not HF_TOKEN:
|
| 72 |
+
# ํ ํฐ์ด ์์ผ๋ฉด ๊ณต๊ฐ๋ ๋ชจ๋ธ๋ก ๋์ฒด (์ ํ์ )
|
| 73 |
+
print("โ ๏ธ HF ํ ํฐ์ด ์์ด ๊ณต๊ฐ ๋ชจ๋ธ(DialoGPT)๋ก ๋์ฒดํฉ๋๋ค.")
|
| 74 |
+
public_model = "microsoft/DialoGPT-medium"
|
| 75 |
+
tokenizer = AutoTokenizer.from_pretrained(public_model)
|
| 76 |
+
model = AutoModelForCausalLM.from_pretrained(public_model, torch_dtype=torch.float16)
|
| 77 |
+
return
|
| 78 |
+
|
| 79 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 80 |
+
MODEL_PATH,
|
| 81 |
+
token=HF_TOKEN,
|
| 82 |
+
trust_remote_code=True
|
| 83 |
+
)
|
| 84 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 85 |
+
MODEL_PATH,
|
| 86 |
+
token=HF_TOKEN,
|
| 87 |
+
torch_dtype=torch.float16,
|
| 88 |
+
trust_remote_code=True,
|
| 89 |
+
device_map="auto" # GPU ์๋ ํ ๋น
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
print("โ
๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋ฉ ์ฑ๊ณต!")
|
| 93 |
+
|
| 94 |
+
# --- 3. ํ์ผ ์ฒ๋ฆฌ ์ ํธ๋ฆฌํฐ ---
|
| 95 |
+
|
| 96 |
+
def extract_text_from_pdf(pdf_file):
|
| 97 |
+
"""PDF์์ ํ
์คํธ ์ถ์ถ"""
|
| 98 |
+
try:
|
| 99 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 100 |
+
text = "".join(page.get_text() for page in doc)
|
| 101 |
+
doc.close()
|
| 102 |
+
return f"\n\n--- PDF ๋ด์ฉ ---\n{text}\n--- PDF ๋ด์ฉ ๋ ---"
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"PDF ์ฝ๊ธฐ ์ค๋ฅ: {e}"
|
| 105 |
+
|
| 106 |
+
def extract_text_from_image(image_file):
|
| 107 |
+
"""์ด๋ฏธ์ง์์ OCR๋ก ํ
์คํธ ์ถ์ถ (ํ์ฌ๋ ํ์ผ ์ ๋ณด๋ง ๋ฐํ)"""
|
| 108 |
+
try:
|
| 109 |
+
# ์ค์ OCR ๋ผ์ด๋ธ๋ฌ๋ฆฌ(์: Tesseract) ์ฐ๋ ํ์
|
| 110 |
+
image = Image.open(image_file)
|
| 111 |
+
return f"\n\n--- ์ด๋ฏธ์ง ํ์ผ ์ ๋ณด: {image.format}, {image.size[0]}x{image.size[1]} ---"
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return f"์ด๋ฏธ์ง ์ฝ๊ธฐ ์ค๋ฅ: {e}"
|
| 114 |
+
|
| 115 |
+
def process_uploaded_file(file):
|
| 116 |
+
"""์
๋ก๋๋ ํ์ผ์ ์ข
๋ฅ์ ๋ง๊ฒ ์ฒ๋ฆฌ"""
|
| 117 |
+
if file is None:
|
| 118 |
+
return ""
|
| 119 |
+
|
| 120 |
+
file_path = file.name
|
| 121 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
| 122 |
+
|
| 123 |
+
if file_extension == '.pdf':
|
| 124 |
+
return extract_text_from_pdf(file)
|
| 125 |
+
elif file_extension in ['.png', '.jpg', '.jpeg']:
|
| 126 |
+
# TODO: ๋ฉํฐ๋ชจ๋ฌ ๋ชจ๋ธ์ ์ํ ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ๋ก์ง ์ถ๊ฐ
|
| 127 |
+
return extract_text_from_image(file)
|
| 128 |
+
else:
|
| 129 |
+
return f"์ง์ํ์ง ์๋ ํ์ผ ํ์: {file_extension}"
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# --- 4. ํต์ฌ ๋ก์ง: ์๋ต ์์ฑ ํจ์ ---
|
| 133 |
+
|
| 134 |
+
def generate_response(prompt_template: str, message: str, file: Optional = None):
|
| 135 |
+
"""ํตํฉ๋ ์๋ต ์์ฑ ํจ์"""
|
| 136 |
+
if not MODEL_LOADED:
|
| 137 |
+
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค. ์ฑ์ ์ฌ์์ํด์ฃผ์ธ์."
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
print("โ๏ธ ์๋ต ์์ฑ ์์...")
|
| 141 |
+
|
| 142 |
+
# 1. ํ์ผ ๋ด์ฉ ์ฒ๋ฆฌ
|
| 143 |
+
file_content = process_uploaded_file(file)
|
| 144 |
+
|
| 145 |
+
# 2. ์ ์ฒด ํ๋กฌํํธ ๊ตฌ์ฑ
|
| 146 |
+
full_message = message + file_content
|
| 147 |
+
full_prompt = prompt_template.format(message=full_message)
|
| 148 |
+
|
| 149 |
+
print(f"๐ ์ ์ฒด ํ๋กฌํํธ (์ผ๋ถ): {full_prompt[:200]}...")
|
| 150 |
+
|
| 151 |
+
# 3. ํ ํฌ๋์ด์ ๋ก ์
๋ ฅ ๋ณํ
|
| 152 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 153 |
+
|
| 154 |
+
# 4. ๋ชจ๋ธ์ ํตํด ์๋ต ์์ฑ (๋จ ํ ๋ฒ์ ํธ์ถ)
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
outputs = model.generate(
|
| 157 |
+
**inputs,
|
| 158 |
+
max_new_tokens=512,
|
| 159 |
+
temperature=0.7,
|
| 160 |
+
top_p=0.9,
|
| 161 |
+
do_sample=True,
|
| 162 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 163 |
+
eos_token_id=tokenizer.eos_token_id
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# 5. ์์ฑ๋ ํ ํฐ ID๋ฅผ ํ
์คํธ๋ก ๋์ฝ๋ฉ
|
| 167 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 168 |
+
|
| 169 |
+
# 6. ์
๋ ฅ ํ๋กฌํํธ๋ฅผ ์๋ต์์ ์ ๊ฑฐํ์ฌ ์์ํ ๋ต๋ณ๋ง ์ถ์ถ
|
| 170 |
+
assistant_response = response_text.split("<|im_start|>assistant\n")[-1].strip()
|
| 171 |
+
|
| 172 |
+
print(f"๐ฌ ์์ฑ๋ ์๋ต (์ผ๋ถ): {assistant_response[:200]}...")
|
| 173 |
+
print("โ
์๋ต ์์ฑ ์๋ฃ")
|
| 174 |
+
|
| 175 |
+
return assistant_response if assistant_response else "์ฃ์กํฉ๋๋ค. ๋ต๋ณ์ ์์ฑํ ์ ์์ต๋๋ค."
|
| 176 |
+
|
| 177 |
+
except Exception as e:
|
| 178 |
+
print(f"โ ์๋ต ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 179 |
+
traceback.print_exc()
|
| 180 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# --- 5. Gradio UI ์ค์ ---
|
| 184 |
+
|
| 185 |
+
with gr.Blocks(title="Lily Math RAG System", theme=gr.themes.Soft()) as demo:
|
| 186 |
+
gr.Markdown("# ๐งฎ Lily Math RAG System")
|
| 187 |
+
gr.Markdown("์ํ ๋ฌธ์ ํด๊ฒฐ ๋ฐ ์ผ๋ฐ ๋ํ๋ฅผ ์ํ AI ์์คํ
์
๋๋ค.")
|
| 188 |
+
|
| 189 |
+
with gr.Tabs():
|
| 190 |
+
with gr.Tab("๐ฌ ์ผ๋ฐ ์ฑํ
"):
|
| 191 |
+
# ์ฑํ
ํ๋กฌํํธ ํ
ํ๋ฆฟ
|
| 192 |
+
chat_prompt = "<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
with gr.Column(scale=3):
|
| 196 |
+
chatbot = gr.Chatbot(height=500, label="๋ํ์ฐฝ", type="messages")
|
| 197 |
+
msg = gr.Textbox(label="๋ฉ์์ง", placeholder="์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?", lines=3)
|
| 198 |
+
clear = gr.Button("์ ๋ํ ์์")
|
| 199 |
+
with gr.Column(scale=1):
|
| 200 |
+
gr.Markdown("### ๐ ํ์ผ ์
๋ก๋ (์ ํ)")
|
| 201 |
+
chat_file = gr.File(label="PDF/์ด๋ฏธ์ง ํ์ผ", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 202 |
+
|
| 203 |
+
def respond(message, chat_history, file):
|
| 204 |
+
bot_message = generate_response(chat_prompt, message, file)
|
| 205 |
+
chat_history.append({"role": "user", "content": message})
|
| 206 |
+
chat_history.append({"role": "assistant", "content": bot_message})
|
| 207 |
+
return "", chat_history
|
| 208 |
+
|
| 209 |
+
msg.submit(respond, [msg, chatbot, chat_file], [msg, chatbot])
|
| 210 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 211 |
+
|
| 212 |
+
with gr.Tab("๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ"):
|
| 213 |
+
# ์ํ ๋ฌธ์ ํ์ด์ฉ ํ๋กฌํํธ ํ
ํ๋ฆฟ
|
| 214 |
+
math_prompt = "๋ค์ ์ํ ๋ฌธ์ ๋ฅผ ๋จ๊ณ๋ณ๋ก ์์ธํ ํ์ด์ฃผ์ธ์:\n\n{message}\n\n<|im_start|>assistant\n"
|
| 215 |
+
|
| 216 |
+
with gr.Row():
|
| 217 |
+
with gr.Column(scale=2):
|
| 218 |
+
math_input = gr.Textbox(label="์ํ ๋ฌธ์ ", placeholder="์: 2x + 5 = 13", lines=5)
|
| 219 |
+
gr.Markdown("### ๐ ๋ฌธ์ ํ์ผ ์
๋ก๋ (์ ํ)")
|
| 220 |
+
math_file = gr.File(label="PDF/์ด๋ฏธ์ง ํ์์ ๋ฌธ์ ", file_types=[".pdf", ".png", ".jpg", ".jpeg"])
|
| 221 |
+
solve_btn = gr.Button("๋ฌธ์ ํ๊ธฐ", variant="primary")
|
| 222 |
+
with gr.Column(scale=2):
|
| 223 |
+
math_output = gr.Textbox(label="ํ์ด ๊ณผ์ ๋ฐ ์ ๋ต", lines=10, interactive=False)
|
| 224 |
+
|
| 225 |
+
solve_btn.click(lambda msg, file: generate_response(math_prompt, msg, file), [math_input, math_file], math_output)
|
| 226 |
+
|
| 227 |
+
with gr.Tab("โ๏ธ ์์คํ
์ ๋ณด"):
|
| 228 |
+
gr.Markdown(f"**๋ชจ๋ธ ๊ฒฝ๋ก**: `{MODEL_PATH}`")
|
| 229 |
+
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: `{'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}`")
|
| 230 |
+
gr.Markdown(f"**์คํ ํ๊ฒฝ**: `{'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}`")
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
# --- 6. ์ ํ๋ฆฌ์ผ์ด์
์คํ ---
|
| 234 |
+
|
| 235 |
+
if __name__ == "__main__":
|
| 236 |
+
try:
|
| 237 |
+
load_model_and_tokenizer()
|
| 238 |
+
MODEL_LOADED = True
|
| 239 |
+
except Exception as e:
|
| 240 |
+
print(f"โ ์ต์ข
์คํ ์คํจ: ์ ํ๋ฆฌ์ผ์ด์
์์์ ํ์ํ ๋ชจ๋ธ์ ๋ก๋ํ์ง ๋ชปํ์ต๋๋ค.")
|
| 241 |
+
print(f"์ค๋ฅ: {e}")
|
| 242 |
+
traceback.print_exc()
|
| 243 |
+
MODEL_LOADED = False
|
| 244 |
+
|
| 245 |
+
demo.launch(server_name="localhost", server_port=8006)
|
test_input.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from gradio_client import Client, file
|
| 3 |
+
|
| 4 |
+
# --- ์ค์ ---
|
| 5 |
+
# ๋ก์ปฌ Gradio ์๋ฒ ์ฃผ์ (app.py ์คํ ์ ํฐ๋ฏธ๋์ ํ์๋๋ ์ฃผ์)
|
| 6 |
+
SERVER_URL = "http://localhost:8006/"
|
| 7 |
+
|
| 8 |
+
def run_chat_test(client):
|
| 9 |
+
"""์ผ๋ฐ ์ฑํ
ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 10 |
+
print("\n--- ๐ฌ ์ผ๋ฐ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 11 |
+
|
| 12 |
+
test_message = "์๋
ํ์ธ์! ์ค๋ ๋ ์จ๋ ์ด๋ค๊ฐ์?"
|
| 13 |
+
chat_history = [] # ์ด๊ธฐ ๋ํ ๋ด์ญ์ ๋น์ด์์
|
| 14 |
+
|
| 15 |
+
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 16 |
+
|
| 17 |
+
# `respond` ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 0)
|
| 18 |
+
# ์
๋ ฅ: (๋ฉ์์ง, ์ฑํ
๋ด์ญ, ํ์ผ)
|
| 19 |
+
# ์ถ๋ ฅ: (๋น์์ง ํ
์คํธ ๋ฐ์ค, ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ)
|
| 20 |
+
result = client.predict(
|
| 21 |
+
test_message,
|
| 22 |
+
chat_history,
|
| 23 |
+
None, # ํ์ผ ์์
|
| 24 |
+
fn_index=0
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ์์ ๋ง์ง๋ง ์๋ต(๋ด ๋ฉ์์ง)์ ์ถ์ถ
|
| 28 |
+
updated_history = result[1]
|
| 29 |
+
bot_response = updated_history[-1]['content']
|
| 30 |
+
|
| 31 |
+
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 32 |
+
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 33 |
+
|
| 34 |
+
def run_math_test(client):
|
| 35 |
+
"""์ํ ๋ฌธ์ ํด๊ฒฐ ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 36 |
+
print("\n--- ๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ ํ
์คํธ ์์ ---")
|
| 37 |
+
|
| 38 |
+
test_problem = "๋ ๊ฐ์ ์ฐ์๋ ์ง์์ ํฉ์ด 34์ผ ๋, ๋ ์ง์๋ ๋ฌด์์ธ๊ฐ์?"
|
| 39 |
+
|
| 40 |
+
print(f"๋ณด๋ด๋ ๋ฌธ์ : '{test_problem}'")
|
| 41 |
+
|
| 42 |
+
# ์ํ ๋ฌธ์ ํด๊ฒฐ ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 1)
|
| 43 |
+
# ์
๋ ฅ: (์ํ ๋ฌธ์ , ํ์ผ)
|
| 44 |
+
# ์ถ๋ ฅ: (๊ฒฐ๊ณผ ํ
์คํธ)
|
| 45 |
+
result = client.predict(
|
| 46 |
+
test_problem,
|
| 47 |
+
None, # ํ์ผ ์์
|
| 48 |
+
fn_index=1
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 52 |
+
print(f"๐ค ๋ฐ์ ์๋ต (์ผ๋ถ): '{result[:200]}...'")
|
| 53 |
+
|
| 54 |
+
def run_file_test(client):
|
| 55 |
+
"""ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 56 |
+
print("\n--- ๐ ํ์ผ ์
๋ก๋ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 57 |
+
|
| 58 |
+
# ํ
์คํธ์ฉ ์์ ํ
์คํธ ํ์ผ ์์ฑ
|
| 59 |
+
temp_file_path = "test_document.txt"
|
| 60 |
+
with open(temp_file_path, "w", encoding="utf-8") as f:
|
| 61 |
+
f.write("์ด ํ์ผ์ ํ
์คํธ๋ฅผ ์ํด ์์ฑ๋์์ต๋๋ค.\n")
|
| 62 |
+
f.write("ํ์ผ์ ํต์ฌ ๋ด์ฉ์ '๋ํ๋ฏผ๊ตญ์ ์๋๋ ์์ธ์ด๋ค' ์
๋๋ค.")
|
| 63 |
+
|
| 64 |
+
print(f"์
๋ก๋ํ ํ์ผ: '{temp_file_path}'")
|
| 65 |
+
test_message = "์
๋ก๋ํ ํ์ผ์ ํต์ฌ ๋ด์ฉ์ด ๋ญ์ผ?"
|
| 66 |
+
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 67 |
+
|
| 68 |
+
# `file()` ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์๋ฒ์ ์
๋ก๋ ๊ฐ๋ฅํ ํํ๋ก ๋ณํ
|
| 69 |
+
result = client.predict(
|
| 70 |
+
test_message,
|
| 71 |
+
[], # ์ฑํ
๋ด์ญ ์์
|
| 72 |
+
file(temp_file_path),
|
| 73 |
+
fn_index=0
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# ์์ ํ์ผ ์ญ์
|
| 77 |
+
os.remove(temp_file_path)
|
| 78 |
+
|
| 79 |
+
bot_response = result[1][-1]['content']
|
| 80 |
+
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 81 |
+
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
print(f"Gradio ์๋ฒ({SERVER_URL})์ ์ฐ๊ฒฐ์ ์๋ํฉ๋๋ค...")
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
# ์๋ฒ์ ํด๋ผ์ด์ธํธ๋ก ์ฐ๊ฒฐ
|
| 89 |
+
client = Client(SERVER_URL, verbose=False)
|
| 90 |
+
print("โ
์๋ฒ ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
| 91 |
+
|
| 92 |
+
# ํ
์คํธ ์คํ
|
| 93 |
+
run_chat_test(client)
|
| 94 |
+
run_math_test(client)
|
| 95 |
+
# run_file_test(client) # ํ์ผ ํ
์คํธ๋ ํ์์ ์ฃผ์ ํด์ ํ์ฌ ์ฌ์ฉ
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"\nโ ํ
์คํธ ์คํจ: ์๋ฒ์ ์ฐ๊ฒฐํ ์ ์๊ฑฐ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.")
|
| 99 |
+
print("๋จผ์ ๋ค๋ฅธ ํฐ๋ฏธ๋์์ 'python app.py'๋ฅผ ์คํํ๋์ง ํ์ธํด์ฃผ์ธ์.")
|
| 100 |
+
print(f"์ค๋ฅ ์์ธ ์ ๋ณด: {e}")
|
test_text.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from gradio_client import Client, file
|
| 3 |
+
|
| 4 |
+
# --- ์ค์ ---
|
| 5 |
+
# ๋ก์ปฌ Gradio ์๋ฒ ์ฃผ์ (app.py ์คํ ์ ํฐ๋ฏธ๋์ ํ์๋๋ ์ฃผ์)
|
| 6 |
+
SERVER_URL = "http://localhost:8006/"
|
| 7 |
+
|
| 8 |
+
def run_chat_test(client):
|
| 9 |
+
"""์ผ๋ฐ ์ฑํ
ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 10 |
+
print("\n--- ๐ฌ ์ผ๋ฐ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 11 |
+
|
| 12 |
+
test_message = "์๋
ํ์ธ์! ์ค๋ ๋ ์จ๋ ์ด๋ค๊ฐ์?"
|
| 13 |
+
chat_history = [] # ์ด๊ธฐ ๋ํ ๋ด์ญ์ ๋น์ด์์
|
| 14 |
+
|
| 15 |
+
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 16 |
+
|
| 17 |
+
# `respond` ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 0)
|
| 18 |
+
# ์
๋ ฅ: (๋ฉ์์ง, ์ฑํ
๋ด์ญ, ํ์ผ)
|
| 19 |
+
# ์ถ๋ ฅ: (๋น์์ง ํ
์คํธ ๋ฐ์ค, ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ)
|
| 20 |
+
result = client.predict(
|
| 21 |
+
test_message,
|
| 22 |
+
chat_history,
|
| 23 |
+
None, # ํ์ผ ์์
|
| 24 |
+
fn_index=0
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# ๊ฐฑ์ ๋ ์ฑํ
๋ด์ญ์์ ๋ง์ง๋ง ์๋ต(๋ด ๋ฉ์์ง)์ ์ถ์ถ
|
| 28 |
+
updated_history = result[1]
|
| 29 |
+
bot_response = updated_history[-1]['content']
|
| 30 |
+
|
| 31 |
+
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 32 |
+
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 33 |
+
|
| 34 |
+
def run_math_test(client):
|
| 35 |
+
"""์ํ ๋ฌธ์ ํด๊ฒฐ ํญ์ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 36 |
+
print("\n--- ๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ ํ
์คํธ ์์ ---")
|
| 37 |
+
|
| 38 |
+
test_problem = "๋ ๊ฐ์ ์ฐ์๋ ์ง์์ ํฉ์ด 34์ผ ๋, ๋ ์ง์๋ ๋ฌด์์ธ๊ฐ์?"
|
| 39 |
+
|
| 40 |
+
print(f"๋ณด๋ด๋ ๋ฌธ์ : '{test_problem}'")
|
| 41 |
+
|
| 42 |
+
# ์ํ ๋ฌธ์ ํด๊ฒฐ ํจ์ ํธ์ถ (API ์๋ํฌ์ธํธ ์ธ๋ฑ์ค: 1)
|
| 43 |
+
# ์
๋ ฅ: (์ํ ๋ฌธ์ , ํ์ผ)
|
| 44 |
+
# ์ถ๋ ฅ: (๊ฒฐ๊ณผ ํ
์คํธ)
|
| 45 |
+
result = client.predict(
|
| 46 |
+
test_problem,
|
| 47 |
+
None, # ํ์ผ ์์
|
| 48 |
+
fn_index=1
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 52 |
+
print(f"๐ค ๋ฐ์ ์๋ต (์ผ๋ถ): '{result[:200]}...'")
|
| 53 |
+
|
| 54 |
+
def run_file_test(client):
|
| 55 |
+
"""ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ์ ํ
์คํธํฉ๋๋ค."""
|
| 56 |
+
print("\n--- ๐ ํ์ผ ์
๋ก๋ ์ฑํ
ํ
์คํธ ์์ ---")
|
| 57 |
+
|
| 58 |
+
# ํ
์คํธ์ฉ ์์ ํ
์คํธ ํ์ผ ์์ฑ
|
| 59 |
+
temp_file_path = "test_document.txt"
|
| 60 |
+
with open(temp_file_path, "w", encoding="utf-8") as f:
|
| 61 |
+
f.write("์ด ํ์ผ์ ํ
์คํธ๋ฅผ ์ํด ์์ฑ๋์์ต๋๋ค.\n")
|
| 62 |
+
f.write("ํ์ผ์ ํต์ฌ ๋ด์ฉ์ '๋ํ๋ฏผ๊ตญ์ ์๋๋ ์์ธ์ด๋ค' ์
๋๋ค.")
|
| 63 |
+
|
| 64 |
+
print(f"์
๋ก๋ํ ํ์ผ: '{temp_file_path}'")
|
| 65 |
+
test_message = "์
๋ก๋ํ ํ์ผ์ ํต์ฌ ๋ด์ฉ์ด ๋ญ์ผ?"
|
| 66 |
+
print(f"๋ณด๋ด๋ ๋ฉ์์ง: '{test_message}'")
|
| 67 |
+
|
| 68 |
+
# `file()` ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์๋ฒ์ ์
๋ก๋ ๊ฐ๋ฅํ ํํ๋ก ๋ณํ
|
| 69 |
+
result = client.predict(
|
| 70 |
+
test_message,
|
| 71 |
+
[], # ์ฑํ
๋ด์ญ ์์
|
| 72 |
+
file(temp_file_path),
|
| 73 |
+
fn_index=0
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# ์์ ํ์ผ ์ญ์
|
| 77 |
+
os.remove(temp_file_path)
|
| 78 |
+
|
| 79 |
+
bot_response = result[1][-1]['content']
|
| 80 |
+
print("โ
ํ
์คํธ ์ฑ๊ณต!")
|
| 81 |
+
print(f"๐ค ๋ฐ์ ์๋ต: '{bot_response}'")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
print(f"Gradio ์๋ฒ({SERVER_URL})์ ์ฐ๊ฒฐ์ ์๋ํฉ๋๋ค...")
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
# ์๋ฒ์ ํด๋ผ์ด์ธํธ๋ก ์ฐ๊ฒฐ
|
| 89 |
+
client = Client(SERVER_URL, verbose=False)
|
| 90 |
+
print("โ
์๋ฒ ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
| 91 |
+
|
| 92 |
+
# ํ
์คํธ ์คํ
|
| 93 |
+
run_chat_test(client)
|
| 94 |
+
run_math_test(client)
|
| 95 |
+
# run_file_test(client) # ํ์ผ ํ
์คํธ๋ ํ์์ ์ฃผ์ ํด์ ํ์ฌ ์ฌ์ฉ
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"\nโ ํ
์คํธ ์คํจ: ์๋ฒ์ ์ฐ๊ฒฐํ ์ ์๊ฑฐ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.")
|
| 99 |
+
print("๋จผ์ ๋ค๋ฅธ ํฐ๋ฏธ๋์์ 'python app.py'๋ฅผ ์คํํ๋์ง ํ์ธํด์ฃผ์ธ์.")
|
| 100 |
+
print(f"์ค๋ฅ ์์ธ ์ ๋ณด: {e}")
|
test_tokenizer.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import traceback
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
| 8 |
+
try:
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
load_dotenv()
|
| 11 |
+
print("โ
.env ํ์ผ ๋ก๋๋จ")
|
| 12 |
+
except ImportError:
|
| 13 |
+
print("โ ๏ธ python-dotenv๊ฐ ์ค์น๋์ง ์์")
|
| 14 |
+
|
| 15 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
+
|
| 17 |
+
# ํ๊ฒฝ ๊ฐ์ง
|
| 18 |
+
IS_LOCAL = os.path.exists('../.env') or 'LOCAL_TEST' in os.environ
|
| 19 |
+
print(f"๐ ํ๊ฒฝ: {'๋ก์ปฌ' if IS_LOCAL else '์๋ฒ'}")
|
| 20 |
+
|
| 21 |
+
# ํ๊ฒฝ์ ๋ฐ๋ฅธ ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์
|
| 22 |
+
if IS_LOCAL:
|
| 23 |
+
# ๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก (hearth_llm_model ํด๋ ์ฌ์ฉ)
|
| 24 |
+
MODEL_PATH = "../lily_llm_core/models/kanana-1.5-v-3b-instruct"
|
| 25 |
+
print(f"๐ ๋ก์ปฌ ๋ชจ๋ธ ๊ฒฝ๋ก: {MODEL_PATH}")
|
| 26 |
+
print(f"๐ ๊ฒฝ๋ก ์กด์ฌ: {os.path.exists(MODEL_PATH)}")
|
| 27 |
+
else:
|
| 28 |
+
# ์๋ฒ์์๋ Hugging Face ๋ชจ๋ธ ์ฌ์ฉ
|
| 29 |
+
MODEL_PATH = os.getenv("MODEL_NAME", "gbrabbit/lily-math-model")
|
| 30 |
+
print(f"๐ ์๋ฒ ๋ชจ๋ธ: {MODEL_PATH}")
|
| 31 |
+
|
| 32 |
+
print(f"๐ ํ ํฐ: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 33 |
+
|
| 34 |
+
# ํ ํฌ๋์ด์ ํ
์คํธ
|
| 35 |
+
print("\n๐ง ํ ํฌ๋์ด์ ํ
์คํธ ์์...")
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
print("๐ค ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 39 |
+
print(f" MODEL_PATH: {MODEL_PATH}")
|
| 40 |
+
print(f" IS_LOCAL: {IS_LOCAL}")
|
| 41 |
+
print(f" trust_remote_code: True")
|
| 42 |
+
print(f" use_fast: False")
|
| 43 |
+
|
| 44 |
+
if IS_LOCAL:
|
| 45 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 46 |
+
MODEL_PATH,
|
| 47 |
+
trust_remote_code=True,
|
| 48 |
+
)
|
| 49 |
+
else:
|
| 50 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 51 |
+
MODEL_PATH,
|
| 52 |
+
token=HF_TOKEN,
|
| 53 |
+
trust_remote_code=True,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
print(f"โ
ํ ํฌ๋์ด์ ๋ก๋ฉ ์๋ฃ")
|
| 57 |
+
print(f" ํ์
: {type(tokenizer)}")
|
| 58 |
+
print(f" ๊ฐ: {tokenizer}")
|
| 59 |
+
print(f" hasattr('encode'): {hasattr(tokenizer, 'encode')}")
|
| 60 |
+
print(f" hasattr('__call__'): {hasattr(tokenizer, '__call__')}")
|
| 61 |
+
|
| 62 |
+
# ํ ํฌ๋์ด์ ํ
์คํธ
|
| 63 |
+
test_input = "์๋
ํ์ธ์"
|
| 64 |
+
print(f"\n๐ค ํ ํฌ๋์ด์ ํ
์คํธ: '{test_input}'")
|
| 65 |
+
|
| 66 |
+
test_tokens = tokenizer(test_input, return_tensors="pt")
|
| 67 |
+
print(f" โ
ํ ํฌ๋์ด์ ํธ์ถ ์ฑ๊ณต")
|
| 68 |
+
print(f" input_ids shape: {test_tokens['input_ids'].shape}")
|
| 69 |
+
print(f" attention_mask shape: {test_tokens['attention_mask'].shape}")
|
| 70 |
+
|
| 71 |
+
# ๋์ฝ๋ฉ ํ
์คํธ
|
| 72 |
+
decoded = tokenizer.decode(test_tokens['input_ids'][0], skip_special_tokens=True)
|
| 73 |
+
print(f" ๋์ฝ๋ฉ ๊ฒฐ๊ณผ: '{decoded}'")
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"โ ํ ํฌ๋์ด์ ํ
์คํธ ์คํจ: {e}")
|
| 77 |
+
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 78 |
+
traceback.print_exc()
|
| 79 |
+
|
| 80 |
+
# ๋ชจ๋ธ ํ
์คํธ
|
| 81 |
+
print("\n๐ง ๋ชจ๋ธ ํ
์คํธ ์์...")
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
print("๐ค ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 85 |
+
from modeling import KananaVForConditionalGeneration
|
| 86 |
+
|
| 87 |
+
if IS_LOCAL:
|
| 88 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 89 |
+
MODEL_PATH,
|
| 90 |
+
torch_dtype=torch.float16,
|
| 91 |
+
trust_remote_code=True,
|
| 92 |
+
device_map=None,
|
| 93 |
+
low_cpu_mem_usage=True
|
| 94 |
+
)
|
| 95 |
+
else:
|
| 96 |
+
model = KananaVForConditionalGeneration.from_pretrained(
|
| 97 |
+
MODEL_PATH,
|
| 98 |
+
token=HF_TOKEN,
|
| 99 |
+
torch_dtype=torch.float16,
|
| 100 |
+
trust_remote_code=True,
|
| 101 |
+
device_map=None,
|
| 102 |
+
low_cpu_mem_usage=True
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
print(f"โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
| 106 |
+
# print(f" ํ์
: {type(model)}")
|
| 107 |
+
# print(f" ๋๋ฐ์ด์ค: {next(model.parameters()).device}")
|
| 108 |
+
|
| 109 |
+
# ๋ชจ๋ธ ํ
์คํธ
|
| 110 |
+
test_input = "์๋
ํ์ธ์"
|
| 111 |
+
formatted_prompt = f"<|im_start|>user\n{test_input}<|im_end|>\n<|im_start|>assistant\n"
|
| 112 |
+
max_length: Optional[int] = None
|
| 113 |
+
|
| 114 |
+
inputs = tokenizer(
|
| 115 |
+
formatted_prompt,
|
| 116 |
+
return_tensors="pt",
|
| 117 |
+
padding=True,
|
| 118 |
+
truncation=True,
|
| 119 |
+
max_length=512
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
print(f"\n๐ค ๋ชจ๋ธ ์ถ๋ก ํ
์คํธ: '{test_input}'")
|
| 123 |
+
|
| 124 |
+
# Kanana์ฉ ์์ฑ ์ค์
|
| 125 |
+
max_new_tokens = max_length or 100
|
| 126 |
+
|
| 127 |
+
with torch.no_grad():
|
| 128 |
+
outputs = model.generate(
|
| 129 |
+
input_ids=inputs["input_ids"],
|
| 130 |
+
attention_mask=inputs["attention_mask"],
|
| 131 |
+
max_new_tokens=max_new_tokens,
|
| 132 |
+
repetition_penalty=1.1,
|
| 133 |
+
no_repeat_ngram_size=2,
|
| 134 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 135 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 136 |
+
use_cache=True
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
print(f" โ
๋ชจ๋ธ ํธ์ถ ์ฑ๊ณต")
|
| 140 |
+
print(f" outputs ํ์
: {type(outputs)}")
|
| 141 |
+
print(f" outputs shape: {outputs.shape}")
|
| 142 |
+
|
| 143 |
+
# ๋์ฝ๋ฉ ํ
์คํธ
|
| 144 |
+
# model.generate()์ ์ถ๋ ฅ์ ์ ์ฒด ์ํ์ค์ด๋ฏ๋ก ๋ฐ๋ก ๋์ฝ๋ฉํฉ๋๋ค.
|
| 145 |
+
# outputs[0]์ ๋ฐฐ์น ์ค ์ฒซ ๋ฒ์งธ ๊ฒฐ๊ณผ๋ฅผ ์๋ฏธํฉ๋๋ค.
|
| 146 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 147 |
+
|
| 148 |
+
# ์
๋ ฅ ํ๋กฌํํธ๋ฅผ ์๋ต์์ ์ ๊ฑฐ (์ ํ์ฌํญ)
|
| 149 |
+
assistant_response = response.split("<|im_start|>assistant\n")[-1]
|
| 150 |
+
|
| 151 |
+
print(f" ์์ฑ๋ ์ ์ฒด ํ
์คํธ: '{response}'")
|
| 152 |
+
print(f" ์ด์์คํดํธ ์๋ต: '{assistant_response.strip()}'")
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"โ ๋ชจ๋ธ ํ
์คํธ ์คํจ: {e}")
|
| 156 |
+
print(f" ์ค๋ฅ ํ์
: {type(e).__name__}")
|
| 157 |
+
traceback.print_exc()
|
| 158 |
+
|
| 159 |
+
print("\nโ
ํ
์คํธ ์๋ฃ!")
|