gbrabbit commited on
Commit
0f0528f
ยท
1 Parent(s): 1bcd400

Auto commit at 07-2025-08 0:20:03

Browse files
Files changed (2) hide show
  1. app.py +110 -30
  2. 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
- # ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ์˜ generate ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ (์ด๋ฏธ์ง€ ์—†์ด ํ…์ŠคํŠธ๋งŒ)
98
- outputs = model.generate(
99
- input_ids=inputs["input_ids"],
100
- attention_mask=inputs["attention_mask"],
101
- max_new_tokens=200,
102
- temperature=0.7,
103
- do_sample=True,
104
- pad_token_id=tokenizer.eos_token_id
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
- # ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ ๋ชจ๋ธ์˜ generate ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ (์ด๋ฏธ์ง€ ์—†์ด ํ…์ŠคํŠธ๋งŒ)
121
- outputs = model.generate(
122
- input_ids=inputs["input_ids"],
123
- attention_mask=inputs["attention_mask"],
124
- max_new_tokens=300,
125
- temperature=0.3,
126
- do_sample=True,
127
- pad_token_id=tokenizer.eos_token_id
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
- chatbot = gr.Chatbot(height=400, type="messages")
142
- msg = gr.Textbox(label="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”", placeholder="์•ˆ๋…•ํ•˜์„ธ์š”! ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ๋„์™€์ฃผ์„ธ์š”.", lines=2)
143
- clear = gr.Button("๋Œ€ํ™” ์ดˆ๊ธฐํ™”")
144
- def respond(message, chat_history):
145
- bot_message = chat_with_model(message, chat_history)
 
 
 
 
 
 
 
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