Spaces:
Running
on
Zero
Running
on
Zero
cutechicken
commited on
Commit
โข
ba10abd
1
Parent(s):
0223744
Update app.py
Browse files
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 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
"""
|
259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
with gr.Blocks(css=CSS) as demo:
|
261 |
with gr.Row():
|
262 |
with gr.Column(scale=2):
|
263 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
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 |
-
|
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()
|