Spaces:
Sleeping
Sleeping
Auto commit at 07-2025-08 0:20:03
Browse files- app.py +110 -30
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -88,21 +88,54 @@ print(f"\n3. ์ต์ข
์ํ:")
|
|
| 88 |
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 89 |
print(f" ์ต์ข
๋ชจ๋ธ๋ช
: {MODEL_NAME}")
|
| 90 |
|
| 91 |
-
def chat_with_model(message, history):
|
| 92 |
if not MODEL_LOADED:
|
| 93 |
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 94 |
try:
|
| 95 |
inputs = tokenizer(message, return_tensors="pt")
|
|
|
|
| 96 |
with torch.no_grad():
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 107 |
if message in response:
|
| 108 |
response = response.replace(message, "").strip()
|
|
@@ -110,22 +143,55 @@ def chat_with_model(message, history):
|
|
| 110 |
except Exception as e:
|
| 111 |
return f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 112 |
|
| 113 |
-
def solve_math_problem(problem):
|
| 114 |
if not MODEL_LOADED:
|
| 115 |
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 116 |
try:
|
| 117 |
prompt = f"๋ค์ ์ํ ๋ฌธ์ ๋ฅผ ๋จ๊ณ๋ณ๋ก ํ์ด์ฃผ์ธ์: {problem}"
|
| 118 |
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
| 119 |
with torch.no_grad():
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 130 |
if prompt in response:
|
| 131 |
response = response.replace(prompt, "").strip()
|
|
@@ -138,30 +204,44 @@ with gr.Blocks(title="Lily Math RAG System", theme=gr.themes.Soft()) as demo:
|
|
| 138 |
gr.Markdown("์ํ ๋ฌธ์ ํด๊ฒฐ์ ์ํ AI ์์คํ
์
๋๋ค.")
|
| 139 |
with gr.Tabs():
|
| 140 |
with gr.Tab("๐ฌ ์ฑํ
"):
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
chat_history.append({"role": "user", "content": message})
|
| 147 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 148 |
return "", chat_history
|
| 149 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 150 |
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
| 151 |
with gr.Tab("๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ"):
|
| 152 |
with gr.Row():
|
| 153 |
-
with gr.Column():
|
| 154 |
math_input = gr.Textbox(label="์ํ ๋ฌธ์ ", placeholder="์: 2x + 5 = 13", lines=3)
|
| 155 |
solve_btn = gr.Button("๋ฌธ์ ํ๊ธฐ", variant="primary")
|
| 156 |
-
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
math_output = gr.Textbox(label="ํด๋ต", lines=8, interactive=False)
|
| 158 |
-
solve_btn.click(solve_math_problem, math_input, math_output)
|
|
|
|
| 159 |
with gr.Tab("โ๏ธ ์ค์ "):
|
| 160 |
gr.Markdown("## ์์คํ
์ ๋ณด")
|
| 161 |
gr.Markdown(f"**๋ชจ๋ธ**: {MODEL_NAME}")
|
| 162 |
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: {'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}")
|
| 163 |
gr.Markdown(f"**ํ ํฐ ์ํ**: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 164 |
-
gr.Markdown("**๋ฒ์ **: 3.0.0 (
|
|
|
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
| 167 |
demo.launch()
|
|
|
|
| 88 |
print(f" MODEL_LOADED: {MODEL_LOADED}")
|
| 89 |
print(f" ์ต์ข
๋ชจ๋ธ๋ช
: {MODEL_NAME}")
|
| 90 |
|
| 91 |
+
def chat_with_model(message, history, image=None):
|
| 92 |
if not MODEL_LOADED:
|
| 93 |
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 94 |
try:
|
| 95 |
inputs = tokenizer(message, return_tensors="pt")
|
| 96 |
+
|
| 97 |
with torch.no_grad():
|
| 98 |
+
if image is not None:
|
| 99 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฉํฐ๋ชจ๋ฌ ์์ฑ
|
| 100 |
+
from PIL import Image
|
| 101 |
+
import torchvision.transforms as transforms
|
| 102 |
+
|
| 103 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 104 |
+
transform = transforms.Compose([
|
| 105 |
+
transforms.Resize((224, 224)),
|
| 106 |
+
transforms.ToTensor(),
|
| 107 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 108 |
+
])
|
| 109 |
+
|
| 110 |
+
if isinstance(image, str):
|
| 111 |
+
pil_image = Image.open(image).convert('RGB')
|
| 112 |
+
else:
|
| 113 |
+
pil_image = image.convert('RGB')
|
| 114 |
+
|
| 115 |
+
pixel_values = transform(pil_image).unsqueeze(0)
|
| 116 |
+
image_metas = {"vision_grid_thw": torch.tensor([[1, 14, 14]])} # ๊ธฐ๋ณธ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ
|
| 117 |
+
|
| 118 |
+
outputs = model.generate(
|
| 119 |
+
input_ids=inputs["input_ids"],
|
| 120 |
+
attention_mask=inputs["attention_mask"],
|
| 121 |
+
pixel_values=[pixel_values],
|
| 122 |
+
image_metas=image_metas,
|
| 123 |
+
max_new_tokens=200,
|
| 124 |
+
temperature=0.7,
|
| 125 |
+
do_sample=True,
|
| 126 |
+
pad_token_id=tokenizer.eos_token_id
|
| 127 |
+
)
|
| 128 |
+
else:
|
| 129 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ํ
์คํธ๋ง ์์ฑ
|
| 130 |
+
outputs = model.generate(
|
| 131 |
+
input_ids=inputs["input_ids"],
|
| 132 |
+
attention_mask=inputs["attention_mask"],
|
| 133 |
+
max_new_tokens=200,
|
| 134 |
+
temperature=0.7,
|
| 135 |
+
do_sample=True,
|
| 136 |
+
pad_token_id=tokenizer.eos_token_id
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 140 |
if message in response:
|
| 141 |
response = response.replace(message, "").strip()
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
return f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 145 |
|
| 146 |
+
def solve_math_problem(problem, image=None):
|
| 147 |
if not MODEL_LOADED:
|
| 148 |
return "โ ๋ชจ๋ธ์ด ๋ก๋๋์ง ์์์ต๋๋ค."
|
| 149 |
try:
|
| 150 |
prompt = f"๋ค์ ์ํ ๋ฌธ์ ๋ฅผ ๋จ๊ณ๋ณ๋ก ํ์ด์ฃผ์ธ์: {problem}"
|
| 151 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 152 |
+
|
| 153 |
with torch.no_grad():
|
| 154 |
+
if image is not None:
|
| 155 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฉํฐ๋ชจ๋ฌ ์์ฑ
|
| 156 |
+
from PIL import Image
|
| 157 |
+
import torchvision.transforms as transforms
|
| 158 |
+
|
| 159 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 160 |
+
transform = transforms.Compose([
|
| 161 |
+
transforms.Resize((224, 224)),
|
| 162 |
+
transforms.ToTensor(),
|
| 163 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 164 |
+
])
|
| 165 |
+
|
| 166 |
+
if isinstance(image, str):
|
| 167 |
+
pil_image = Image.open(image).convert('RGB')
|
| 168 |
+
else:
|
| 169 |
+
pil_image = image.convert('RGB')
|
| 170 |
+
|
| 171 |
+
pixel_values = transform(pil_image).unsqueeze(0)
|
| 172 |
+
image_metas = {"vision_grid_thw": torch.tensor([[1, 14, 14]])} # ๊ธฐ๋ณธ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ
|
| 173 |
+
|
| 174 |
+
outputs = model.generate(
|
| 175 |
+
input_ids=inputs["input_ids"],
|
| 176 |
+
attention_mask=inputs["attention_mask"],
|
| 177 |
+
pixel_values=[pixel_values],
|
| 178 |
+
image_metas=image_metas,
|
| 179 |
+
max_new_tokens=300,
|
| 180 |
+
temperature=0.3,
|
| 181 |
+
do_sample=True,
|
| 182 |
+
pad_token_id=tokenizer.eos_token_id
|
| 183 |
+
)
|
| 184 |
+
else:
|
| 185 |
+
# ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ํ
์คํธ๋ง ์์ฑ
|
| 186 |
+
outputs = model.generate(
|
| 187 |
+
input_ids=inputs["input_ids"],
|
| 188 |
+
attention_mask=inputs["attention_mask"],
|
| 189 |
+
max_new_tokens=300,
|
| 190 |
+
temperature=0.3,
|
| 191 |
+
do_sample=True,
|
| 192 |
+
pad_token_id=tokenizer.eos_token_id
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 196 |
if prompt in response:
|
| 197 |
response = response.replace(prompt, "").strip()
|
|
|
|
| 204 |
gr.Markdown("์ํ ๋ฌธ์ ํด๊ฒฐ์ ์ํ AI ์์คํ
์
๋๋ค.")
|
| 205 |
with gr.Tabs():
|
| 206 |
with gr.Tab("๐ฌ ์ฑํ
"):
|
| 207 |
+
with gr.Row():
|
| 208 |
+
with gr.Column(scale=3):
|
| 209 |
+
chatbot = gr.Chatbot(height=400, type="messages")
|
| 210 |
+
msg = gr.Textbox(label="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์", placeholder="์๋
ํ์ธ์! ์ํ ๋ฌธ์ ๋ฅผ ๋์์ฃผ์ธ์.", lines=2)
|
| 211 |
+
clear = gr.Button("๋ํ ์ด๊ธฐํ")
|
| 212 |
+
with gr.Column(scale=1):
|
| 213 |
+
gr.Markdown("### ๐ท ์ด๋ฏธ์ง ์
๋ก๋")
|
| 214 |
+
image_input = gr.Image(label="์ด๋ฏธ์ง (์ ํ์ฌํญ)", type="pil")
|
| 215 |
+
gr.Markdown("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฉํฐ๋ชจ๋ฌ ๋ํ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.")
|
| 216 |
+
|
| 217 |
+
def respond(message, chat_history, image):
|
| 218 |
+
bot_message = chat_with_model(message, chat_history, image)
|
| 219 |
chat_history.append({"role": "user", "content": message})
|
| 220 |
chat_history.append({"role": "assistant", "content": bot_message})
|
| 221 |
return "", chat_history
|
| 222 |
+
msg.submit(respond, [msg, chatbot, image_input], [msg, chatbot])
|
| 223 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 224 |
+
|
| 225 |
with gr.Tab("๐งฎ ์ํ ๋ฌธ์ ํด๊ฒฐ"):
|
| 226 |
with gr.Row():
|
| 227 |
+
with gr.Column(scale=2):
|
| 228 |
math_input = gr.Textbox(label="์ํ ๋ฌธ์ ", placeholder="์: 2x + 5 = 13", lines=3)
|
| 229 |
solve_btn = gr.Button("๋ฌธ์ ํ๊ธฐ", variant="primary")
|
| 230 |
+
with gr.Column(scale=1):
|
| 231 |
+
gr.Markdown("### ๐ท ์ด๋ฏธ์ง ์
๋ก๋")
|
| 232 |
+
math_image_input = gr.Image(label="์ํ ๋ฌธ์ ์ด๋ฏธ์ง (์ ํ์ฌํญ)", type="pil")
|
| 233 |
+
gr.Markdown("์ํ ๋ฌธ์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ ์ ํํ ๋ต๋ณ์ ๋ฐ์ ์ ์์ต๋๋ค.")
|
| 234 |
+
with gr.Column(scale=2):
|
| 235 |
math_output = gr.Textbox(label="ํด๋ต", lines=8, interactive=False)
|
| 236 |
+
solve_btn.click(solve_math_problem, [math_input, math_image_input], math_output)
|
| 237 |
+
|
| 238 |
with gr.Tab("โ๏ธ ์ค์ "):
|
| 239 |
gr.Markdown("## ์์คํ
์ ๋ณด")
|
| 240 |
gr.Markdown(f"**๋ชจ๋ธ**: {MODEL_NAME}")
|
| 241 |
gr.Markdown(f"**๋ชจ๋ธ ์ํ**: {'โ
๋ก๋๋จ' if MODEL_LOADED else 'โ ๋ก๋ ์คํจ'}")
|
| 242 |
gr.Markdown(f"**ํ ํฐ ์ํ**: {'โ
์ค์ ๋จ' if HF_TOKEN else 'โ ์ค์ ๋์ง ์์'}")
|
| 243 |
+
gr.Markdown("**๋ฒ์ **: 3.0.0 (๋ฉํฐ๋ชจ๋ฌ)")
|
| 244 |
+
gr.Markdown("**๊ธฐ๋ฅ**: ํ
์คํธ + ์ด๋ฏธ์ง ๋ฉํฐ๋ชจ๋ฌ ๋ํ")
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -5,4 +5,6 @@ torch>=2.0.0
|
|
| 5 |
einops>=0.6.0
|
| 6 |
timm>=0.9.0
|
| 7 |
python-dotenv>=1.0.0
|
|
|
|
|
|
|
| 8 |
accelerate==1.9.0
|
|
|
|
| 5 |
einops>=0.6.0
|
| 6 |
timm>=0.9.0
|
| 7 |
python-dotenv>=1.0.0
|
| 8 |
+
Pillow>=9.0.0
|
| 9 |
+
torchvision>=0.15.0
|
| 10 |
accelerate==1.9.0
|