cutechicken commited on
Commit
ba10abd
โ€ข
1 Parent(s): 0223744

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -111
app.py CHANGED
@@ -192,60 +192,7 @@ def read_uploaded_file(file):
192
  except Exception as e:
193
  return f"โŒ ํŒŒ์ผ ์ฝ๊ธฐ ์˜ค๋ฅ˜: {str(e)}", "error"
194
 
195
- @spaces.GPU
196
- def stream_chat(message: str, history: list, uploaded_file, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
197
- print(f'message is - {message}')
198
- print(f'history is - {history}')
199
-
200
- # ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ
201
- file_context = ""
202
- if uploaded_file:
203
- content, file_type = read_uploaded_file(uploaded_file)
204
- if content:
205
- file_context = f"\n\n์—…๋กœ๋“œ๋œ ํŒŒ์ผ ๋‚ด์šฉ:\n```\n{content}\n```"
206
-
207
- # ๊ด€๋ จ ์ปจํ…์ŠคํŠธ ์ฐพ๊ธฐ
208
- relevant_contexts = find_relevant_context(message)
209
- wiki_context = "\n\n๊ด€๋ จ ์œ„ํ‚คํ”ผ๋””์•„ ์ •๋ณด:\n"
210
- for ctx in relevant_contexts:
211
- wiki_context += f"Q: {ctx['question']}\nA: {ctx['answer']}\n์œ ์‚ฌ๋„: {ctx['similarity']:.3f}\n\n"
212
-
213
- # ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ ๊ตฌ์„ฑ
214
- conversation = []
215
- for prompt, answer in history:
216
- conversation.extend([
217
- {"role": "user", "content": prompt},
218
- {"role": "assistant", "content": answer}
219
- ])
220
-
221
- # ์ตœ์ข… ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ
222
- final_message = file_context + wiki_context + "\nํ˜„์žฌ ์งˆ๋ฌธ: " + message
223
- conversation.append({"role": "user", "content": final_message})
224
-
225
- input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
226
- inputs = tokenizer(input_ids, return_tensors="pt").to(0)
227
-
228
- streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
229
-
230
- generate_kwargs = dict(
231
- inputs,
232
- streamer=streamer,
233
- top_k=top_k,
234
- top_p=top_p,
235
- repetition_penalty=penalty,
236
- max_new_tokens=max_new_tokens,
237
- do_sample=True,
238
- temperature=temperature,
239
- eos_token_id=[255001],
240
- )
241
-
242
- thread = Thread(target=model.generate, kwargs=generate_kwargs)
243
- thread.start()
244
 
245
- buffer = ""
246
- for new_text in streamer:
247
- buffer += new_text
248
- yield buffer
249
 
250
  CSS = """
251
  /* ์ „์ฒด ํŽ˜์ด์ง€ ์Šคํƒ€์ผ๋ง */
@@ -254,13 +201,152 @@ body {
254
  min-height: 100vh;
255
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
256
  }
257
- /* ... (์ด์ „์˜ CSS ์Šคํƒ€์ผ ์œ ์ง€) ... */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  """
259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  with gr.Blocks(css=CSS) as demo:
261
  with gr.Row():
262
  with gr.Column(scale=2):
263
- chatbot = gr.Chatbot(height=500)
 
 
 
 
 
264
 
265
  msg = gr.Textbox(
266
  label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ",
@@ -282,60 +368,11 @@ with gr.Blocks(css=CSS) as demo:
282
  )
283
 
284
  with gr.Accordion("๊ณ ๊ธ‰ ์„ค์ • โš™๏ธ", open=False):
285
- temperature = gr.Slider(
286
- minimum=0,
287
- maximum=1,
288
- step=0.1,
289
- value=0.8,
290
- label="์˜จ๋„",
291
- )
292
- max_new_tokens = gr.Slider(
293
- minimum=128,
294
- maximum=8000,
295
- step=1,
296
- value=4000,
297
- label="์ตœ๋Œ€ ํ† ํฐ ์ˆ˜",
298
- )
299
- top_p = gr.Slider(
300
- minimum=0.0,
301
- maximum=1.0,
302
- step=0.1,
303
- value=0.8,
304
- label="์ƒ์œ„ ํ™•๋ฅ ",
305
- )
306
- top_k = gr.Slider(
307
- minimum=1,
308
- maximum=20,
309
- step=1,
310
- value=20,
311
- label="์ƒ์œ„ K",
312
- )
313
- penalty = gr.Slider(
314
- minimum=0.0,
315
- maximum=2.0,
316
- step=0.1,
317
- value=1.0,
318
- label="๋ฐ˜๋ณต ํŒจ๋„ํ‹ฐ",
319
- )
320
-
321
- # ์˜ˆ์‹œ ์งˆ๋ฌธ
322
- gr.Examples(
323
- examples=[
324
- ["ํ•œ๊ตญ์˜ ์ „ํ†ต ์ ˆ๊ธฐ์™€ 24์ ˆ๊ธฐ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."],
325
- ["์šฐ๋ฆฌ๋‚˜๋ผ ์ „ํ†ต ์Œ์‹ ์ค‘ ๊ฑด๊ฐ•์— ์ข‹์€ ๋ฐœํšจ์Œ์‹ 5๊ฐ€์ง€๋ฅผ ์ถ”์ฒœํ•˜๊ณ  ๊ทธ ํšจ๋Šฅ์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."],
326
- ["ํ•œ๊ตญ์˜ ๋Œ€ํ‘œ์ ์ธ ์‚ฐ๋“ค์„ ์†Œ๊ฐœํ•˜๊ณ , ๊ฐ ์‚ฐ์˜ ํŠน์ง•๊ณผ ๋“ฑ์‚ฐ ์ฝ”์Šค๋ฅผ ์ถ”์ฒœํ•ด์ฃผ์„ธ์š”."],
327
- ["์‚ฌ๋ฌผ๋†€์ด์˜ ์•…๊ธฐ ๊ตฌ์„ฑ๊ณผ ์žฅ๋‹จ์— ๋Œ€ํ•ด ์ดˆ๋ณด์ž๋„ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."],
328
- ["ํ•œ๊ตญ์˜ ์ „ํ†ต ๊ฑด์ถ•๋ฌผ์— ๋‹ด๊ธด ๊ณผํ•™์  ์›๋ฆฌ๋ฅผ ํ˜„๋Œ€์  ๊ด€์ ์—์„œ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”."],
329
- ["์กฐ์„ ์‹œ๋Œ€ ๊ณผ๊ฑฐ ์‹œํ—˜ ์ œ๋„๋ฅผ ํ˜„๋Œ€์˜ ์ž…์‹œ ์ œ๋„์™€ ๋น„๊ตํ•˜์—ฌ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."],
330
- ["ํ•œ๊ตญ์˜ 4๋Œ€ ๊ถ๊ถ์„ ๋น„๊ตํ•˜์—ฌ ๊ฐ๊ฐ์˜ ํŠน์ง•๊ณผ ์—ญ์‚ฌ์  ์˜๋ฏธ๋ฅผ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."],
331
- ["ํ•œ๊ตญ์˜ ์ „ํ†ต ๋†€์ด๋ฅผ ํ˜„๋Œ€์ ์œผ๋กœ ์žฌํ•ด์„ํ•˜์—ฌ ์‹ค๋‚ด์—์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ•ด์ฃผ์„ธ์š”."],
332
- ["ํ•œ๊ธ€ ์ฐฝ์ œ ๊ณผ์ •๊ณผ ํ›ˆ๋ฏผ์ •์Œ์˜ ๊ณผํ•™์  ์›๋ฆฌ๋ฅผ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."],
333
- ["ํ•œ๊ตญ์˜ ์ „ํ†ต ์ฐจ ๋ฌธํ™”์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ , ๊ณ„์ ˆ๋ณ„๋กœ ์–ด์šธ๋ฆฌ๋Š” ์ „ํ†ต์ฐจ๋ฅผ ์ถ”์ฒœํ•ด์ฃผ์„ธ์š”."],
334
- ["ํ•œ๊ตญ์˜ ์ „ํ†ต ์˜๋ณต์ธ ํ•œ๋ณต์˜ ๊ตฌ์กฐ์™€ ํŠน์ง•์„ ๊ณผํ•™์ , ๋ฏธํ•™์  ๊ด€์ ์—์„œ ๋ถ„์„ํ•ด์ฃผ์„ธ์š”."],
335
- ["ํ•œ๊ตญ์˜ ์ „ํ†ต ๊ฐ€์˜ฅ ๊ตฌ์กฐ๋ฅผ ๊ธฐํ›„์™€ ํ™˜๊ฒฝ ๊ด€์ ์—์„œ ๋ถ„์„ํ•˜๊ณ , ํ˜„๋Œ€ ๊ฑด์ถ•์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์š”์†Œ๋ฅผ ์ œ์•ˆํ•ด์ฃผ์„ธ์š”."]
336
- ],
337
- inputs=msg,
338
- )
339
 
340
  # ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
341
  msg.submit(
@@ -350,9 +387,12 @@ with gr.Blocks(css=CSS) as demo:
350
  outputs=[msg, chatbot]
351
  )
352
 
 
 
 
353
  # ํŒŒ์ผ ์—…๋กœ๋“œ์‹œ ์ž๋™ ๋ถ„์„
354
  file_upload.change(
355
- lambda: "ํŒŒ์ผ ๋ถ„์„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค...",
356
  outputs=msg
357
  ).then(
358
  stream_chat,
@@ -361,4 +401,4 @@ with gr.Blocks(css=CSS) as demo:
361
  )
362
 
363
  if __name__ == "__main__":
364
- demo.launch()
 
192
  except Exception as e:
193
  return f"โŒ ํŒŒ์ผ ์ฝ๊ธฐ ์˜ค๋ฅ˜: {str(e)}", "error"
194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
 
 
 
 
196
 
197
  CSS = """
198
  /* ์ „์ฒด ํŽ˜์ด์ง€ ์Šคํƒ€์ผ๋ง */
 
201
  min-height: 100vh;
202
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
203
  }
204
+ /* ๋ฉ”์ธ ์ปจํ…Œ์ด๋„ˆ */
205
+ .container {
206
+ max-width: 1200px;
207
+ margin: 0 auto;
208
+ padding: 2rem;
209
+ background: rgba(255, 255, 255, 0.95);
210
+ border-radius: 20px;
211
+ box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
212
+ backdrop-filter: blur(10px);
213
+ transform: perspective(1000px) translateZ(0);
214
+ transition: all 0.3s ease;
215
+ }
216
+ /* ์ œ๋ชฉ ์Šคํƒ€์ผ๋ง */
217
+ h1 {
218
+ color: #2d3436;
219
+ font-size: 2.5rem;
220
+ text-align: center;
221
+ margin-bottom: 2rem;
222
+ text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
223
+ transform: perspective(1000px) translateZ(20px);
224
+ }
225
+ h3 {
226
+ text-align: center;
227
+ color: #2d3436;
228
+ font-size: 1.5rem;
229
+ margin: 1rem 0;
230
+ }
231
+ /* ์ฑ„ํŒ…๋ฐ•์Šค ์Šคํƒ€์ผ๋ง */
232
+ .chatbox {
233
+ background: white;
234
+ border-radius: 15px;
235
+ box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
236
+ backdrop-filter: blur(4px);
237
+ border: 1px solid rgba(255, 255, 255, 0.18);
238
+ padding: 1rem;
239
+ margin: 1rem 0;
240
+ transform: translateZ(0);
241
+ transition: all 0.3s ease;
242
+ }
243
+ /* ๋ฉ”์‹œ์ง€ ์Šคํƒ€์ผ๋ง */
244
+ .chatbox .messages .message.user {
245
+ background: linear-gradient(145deg, #e1f5fe, #bbdefb);
246
+ border-radius: 15px;
247
+ padding: 1rem;
248
+ margin: 0.5rem;
249
+ box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.05);
250
+ transform: translateZ(10px);
251
+ animation: messageIn 0.3s ease-out;
252
+ }
253
+ .chatbox .messages .message.bot {
254
+ background: linear-gradient(145deg, #f5f5f5, #eeeeee);
255
+ border-radius: 15px;
256
+ padding: 1rem;
257
+ margin: 0.5rem;
258
+ box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.05);
259
+ transform: translateZ(10px);
260
+ animation: messageIn 0.3s ease-out;
261
+ }
262
+ /* ๋ฒ„ํŠผ ์Šคํƒ€์ผ๋ง */
263
+ .duplicate-button {
264
+ background: linear-gradient(145deg, #24292e, #1a1e22) !important;
265
+ color: white !important;
266
+ border-radius: 100vh !important;
267
+ padding: 0.8rem 1.5rem !important;
268
+ box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2) !important;
269
+ transition: all 0.3s ease !important;
270
+ border: none !important;
271
+ cursor: pointer !important;
272
+ }
273
+ .duplicate-button:hover {
274
+ transform: translateY(-2px) !important;
275
+ box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3) !important;
276
+ }
277
+ /* ์ž…๋ ฅ ํ•„๋“œ ์Šคํƒ€์ผ๋ง */
278
  """
279
 
280
+ @spaces.GPU
281
+ def stream_chat(message: str, history: list, uploaded_file, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
282
+ try:
283
+ print(f'message is - {message}')
284
+ print(f'history is - {history}')
285
+
286
+ # ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ
287
+ file_context = ""
288
+ if uploaded_file:
289
+ content, file_type = read_uploaded_file(uploaded_file)
290
+ if content:
291
+ file_context = f"\n\n์—…๋กœ๋“œ๋œ ํŒŒ์ผ ๋‚ด์šฉ:\n```\n{content}\n```"
292
+
293
+ # ๊ด€๋ จ ์ปจํ…์ŠคํŠธ ์ฐพ๏ฟฝ๏ฟฝ๏ฟฝ
294
+ relevant_contexts = find_relevant_context(message)
295
+ wiki_context = "\n\n๊ด€๋ จ ์œ„ํ‚คํ”ผ๋””์•„ ์ •๋ณด:\n"
296
+ for ctx in relevant_contexts:
297
+ wiki_context += f"Q: {ctx['question']}\nA: {ctx['answer']}\n์œ ์‚ฌ๋„: {ctx['similarity']:.3f}\n\n"
298
+
299
+ # ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ ๊ตฌ์„ฑ
300
+ conversation = []
301
+ for prompt, answer in history:
302
+ conversation.extend([
303
+ {"role": "user", "content": prompt},
304
+ {"role": "assistant", "content": answer}
305
+ ])
306
+
307
+ # ์ตœ์ข… ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ
308
+ final_message = file_context + wiki_context + "\nํ˜„์žฌ ์งˆ๋ฌธ: " + message
309
+ conversation.append({"role": "user", "content": final_message})
310
+
311
+ input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
312
+ inputs = tokenizer(input_ids, return_tensors="pt").to(0)
313
+
314
+ streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
315
+
316
+ generate_kwargs = dict(
317
+ inputs,
318
+ streamer=streamer,
319
+ top_k=top_k,
320
+ top_p=top_p,
321
+ repetition_penalty=penalty,
322
+ max_new_tokens=max_new_tokens,
323
+ do_sample=True,
324
+ temperature=temperature,
325
+ eos_token_id=[255001],
326
+ )
327
+
328
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
329
+ thread.start()
330
+
331
+ buffer = ""
332
+ for new_text in streamer:
333
+ buffer += new_text
334
+ yield "", history + [[message, buffer]]
335
+
336
+ except Exception as e:
337
+ error_message = f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
338
+ yield "", history + [[message, error_message]]
339
+
340
+ # UI ๋ถ€๋ถ„ ์ˆ˜์ •
341
  with gr.Blocks(css=CSS) as demo:
342
  with gr.Row():
343
  with gr.Column(scale=2):
344
+ chatbot = gr.Chatbot(
345
+ value=[],
346
+ height=500,
347
+ label="๋Œ€ํ™”์ฐฝ",
348
+ show_label=True
349
+ )
350
 
351
  msg = gr.Textbox(
352
  label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ",
 
368
  )
369
 
370
  with gr.Accordion("๊ณ ๊ธ‰ ์„ค์ • โš™๏ธ", open=False):
371
+ temperature = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.8, label="์˜จ๋„")
372
+ max_new_tokens = gr.Slider(minimum=128, maximum=8000, step=1, value=4000, label="์ตœ๋Œ€ ํ† ํฐ ์ˆ˜")
373
+ top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0.8, label="์ƒ์œ„ ํ™•๋ฅ ")
374
+ top_k = gr.Slider(minimum=1, maximum=20, step=1, value=20, label="์ƒ์œ„ K")
375
+ penalty = gr.Slider(minimum=0.0, maximum=2.0, step=0.1, value=1.0, label="๋ฐ˜๋ณต ํŒจ๋„ํ‹ฐ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
 
377
  # ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
378
  msg.submit(
 
387
  outputs=[msg, chatbot]
388
  )
389
 
390
+ def init_msg():
391
+ return "ํŒŒ์ผ ๋ถ„์„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค..."
392
+
393
  # ํŒŒ์ผ ์—…๋กœ๋“œ์‹œ ์ž๋™ ๋ถ„์„
394
  file_upload.change(
395
+ init_msg,
396
  outputs=msg
397
  ).then(
398
  stream_chat,
 
401
  )
402
 
403
  if __name__ == "__main__":
404
+ demo.launch()