Jay1121 commited on
Commit
b8e1e61
·
verified ·
1 Parent(s): a2b6c95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +292 -56
app.py CHANGED
@@ -7,21 +7,27 @@ import random
7
  # 1. 모델 준비
8
  # ------------------------------------------------------------------
9
  REPO_ID = "Jay1121/blossom_2GB"
10
- FILENAME = "qwen2.5-3b-instruct.Q4_K_M.gguf"
11
 
12
- # 모델 다운로드
13
  print(f"📥 모델 다운로드 확인: {FILENAME}")
14
- model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
 
 
 
 
15
 
16
- # 엔진 로드
17
  print("🧠 엔진 시동 중...")
18
- llm = Llama(
19
- model_path=model_path,
20
- n_ctx=2048,
21
- n_threads=4,
22
- verbose=True
23
- )
24
- print("✅ 로딩 완료!")
 
 
 
 
25
 
26
  # ------------------------------------------------------------------
27
  # 2. 시스템 프롬프트
@@ -244,36 +250,259 @@ with gr.Blocks(theme=gr.themes.Base(), css=DYSTOPIA_CSS) as demo:
244
  gr.Markdown(
245
  """
246
  ---
247
- ```
248
- [SYS] CONNECTION: ESTABLISHED
249
- [SYS] NEURAL LINK: ACTIVE
250
- [SYS] 찐친 모드: ON
251
- [SYS] 예의: 0%
252
- ```
253
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  )
 
 
 
 
255
 
256
- if __name__ == "__main__":
257
- demo.launch()
258
- ```
259
-
260
- **`requirements.txt`**
261
- ```
262
- gradio
263
- llama-cpp-python
264
- huggingface_hub
265
-
266
- 바뀐 부분:
267
-
268
- hf_hub_download로 모델 다운로드 (그 출력됨)
269
- 모델 경로: Jay1121/blossom_2GB / unsloth.Q4_K_M.gguf
270
- 엔진 로드 로그 ("📥 모델 다운로드", "🧠 엔진 시동 중...", "✅ 로딩 완료!")
271
- 밸런스 게임 + 인사 감지 로직 포함
272
- 디스토피아 CSS 유지
273
- 재시도J아니 어디부터 어디까지가 app.py고 어디부터 어디까지requirements 야 구분해서 좀 줘봐라ㅇㅋ 파일별로 나눠줄게.
274
-
275
- 📁 파일 1: app.py
276
- pythonimport gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  from huggingface_hub import hf_hub_download
278
  from llama_cpp import Llama
279
  import random
@@ -285,16 +514,24 @@ REPO_ID = "Jay1121/blossom_2GB"
285
  FILENAME = "unsloth.Q4_K_M.gguf"
286
 
287
  print(f"📥 모델 다운로드 확인: {FILENAME}")
288
- model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
 
 
 
 
289
 
290
  print("🧠 엔진 시동 중...")
291
- llm = Llama(
292
- model_path=model_path,
293
- n_ctx=2048,
294
- n_threads=4,
295
- verbose=True
296
- )
297
- print("✅ 로딩 완료!")
 
 
 
 
298
 
299
  # ------------------------------------------------------------------
300
  # 2. 시스템 프롬프트
@@ -331,6 +568,7 @@ def chat_response(user_input, history):
331
  else:
332
  final_instruction = user_input
333
 
 
334
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
335
 
336
  for user_msg, bot_msg in history:
@@ -339,6 +577,7 @@ def chat_response(user_input, history):
339
 
340
  messages.append({"role": "user", "content": final_instruction})
341
 
 
342
  response = llm.create_chat_completion(
343
  messages=messages,
344
  max_tokens=256,
@@ -514,16 +753,13 @@ with gr.Blocks(theme=gr.themes.Base(), css=DYSTOPIA_CSS) as demo:
514
 
515
  gr.Markdown(
516
  """
517
- ---
518
- ```
519
- [SYS] CONNECTION: ESTABLISHED
520
- [SYS] NEURAL LINK: ACTIVE
521
- [SYS] 찐친 모드: ON
522
- [SYS] 예의: 0%
523
- ```
524
  """
525
  )
526
 
527
  if __name__ == "__main__":
528
- demo.launch()
529
-
 
7
  # 1. 모델 준비
8
  # ------------------------------------------------------------------
9
  REPO_ID = "Jay1121/blossom_2GB"
10
+ FILENAME = "unsloth.Q4_K_M.gguf"
11
 
 
12
  print(f"📥 모델 다운로드 확인: {FILENAME}")
13
+ try:
14
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
15
+ except Exception as e:
16
+ print(f"❌ 모델 다운로드 실패: {e}")
17
+ raise e
18
 
 
19
  print("🧠 엔진 시동 중...")
20
+ try:
21
+ llm = Llama(
22
+ model_path=model_path,
23
+ n_ctx=2048,
24
+ n_threads=2, # 무료 티어 CPU 개수에 맞춤
25
+ verbose=True
26
+ )
27
+ print("✅ 로딩 완료!")
28
+ except Exception as e:
29
+ print(f"❌ 엔진 로드 실패: {e}")
30
+ raise e
31
 
32
  # ------------------------------------------------------------------
33
  # 2. 시스템 프롬프트
 
250
  gr.Markdown(
251
  """
252
  ---
253
+ import gradio as gr
254
+ from huggingface_hub import hf_hub_download
255
+ from llama_cpp import Llama
256
+ import random
257
+
258
+ # ------------------------------------------------------------------
259
+ # 1. 모델 준비
260
+ # ------------------------------------------------------------------
261
+ REPO_ID = "Jay1121/blossom_2GB"
262
+ FILENAME = "unsloth.Q4_K_M.gguf"
263
+
264
+ print(f"📥 모델 다운로드 확인: {FILENAME}")
265
+ try:
266
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
267
+ except Exception as e:
268
+ print(f"❌ 모델 다운로드 실패: {e}")
269
+ raise e
270
+
271
+ print("🧠 엔진 시동 중...")
272
+ try:
273
+ llm = Llama(
274
+ model_path=model_path,
275
+ n_ctx=2048,
276
+ n_threads=2, # 무료 티어 CPU 개수에 맞춤
277
+ verbose=True
278
  )
279
+ print("✅ 로딩 완료!")
280
+ except Exception as e:
281
+ print(f"❌ 엔진 로드 실패: {e}")
282
+ raise e
283
 
284
+ # ------------------------------------------------------------------
285
+ # 2. 시스템 프롬프트
286
+ # ------------------------------------------------------------------
287
+ SYSTEM_PROMPT = """너는 사용자의 오랜 찐친 '똘배'야.
288
+ - 반말로 편하게 대화해. 존댓말 절대 쓰지 마.
289
+ - 처음 보면 "야 왔냐", "오 뭐야" 같은 가벼운 인사로 시작해.
290
+ - 친구처럼 자연스럽게 대화하고, 가끔 욕도 섞어서 말해.
291
+ - 너무 과하게 욕하지 말고, 친한 친구가 장난치듯 시니컬하게 말해.
292
+ - 상대방 기분이 안 좋아 보이면 걱정도 해주고, 기분 좋으면 같이 좋아해줘.
293
+ - 질문에는 솔직하게 대답하되, 딱딱하게 말하지 마."""
294
+
295
+ # ------------------------------------------------------------------
296
+ # 3. 채팅
297
+ # ------------------------------------------------------------------
298
+ def chat_response(user_input, history):
299
+ clean_input = user_input.replace(" ", "")
300
+
301
+ greeting_words = ["안녕", "ㅎㅇ", "하이", "반", "좋은아침"]
302
+ is_greeting = any(word in clean_input for word in greeting_words)
303
+ is_balance_game = "밸런스게임" in clean_input or "밸런스질문" in clean_input
304
+
305
+ if is_balance_game:
306
+ topics = ["음식", "연애", "고통", "돈", "초능력", "직장", "친구"]
307
+ topic = random.choice(topics)
308
+ final_instruction = (
309
+ f"(사용자가 밸런스 게임을 하자고 한다. 주제는 '{topic}'이다. "
310
+ "아주 고르기 곤란하고 짜증나는 두 가지 선택지(A vs B)를 제시해라. "
311
+ "말투는 여전히 거칠고 시비조로 해라.) "
312
+ "자, 질문해."
313
+ )
314
+ elif is_greeting:
315
+ final_instruction = f"(친한 친구가 인사를 건넨다. 욕하지 말고 무심하게 받아준다) {user_input}"
316
+ else:
317
+ final_instruction = user_input
318
+
319
+ # 대화 히스토리 구성
320
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
321
+
322
+ for user_msg, bot_msg in history:
323
+ messages.append({"role": "user", "content": user_msg})
324
+ messages.append({"role": "assistant", "content": bot_msg})
325
+
326
+ messages.append({"role": "user", "content": final_instruction})
327
+
328
+ # 생성
329
+ response = llm.create_chat_completion(
330
+ messages=messages,
331
+ max_tokens=256,
332
+ stop=["<|end_of_text|>", "###", "User:"],
333
+ temperature=0.7 if is_balance_game else 0.5,
334
+ top_p=0.9,
335
+ repeat_penalty=1.2
336
+ )
337
+
338
+ return response["choices"][0]["message"]["content"].strip()
339
+
340
+ # ------------------------------------------------------------------
341
+ # 4. UI 디자인 (디스토피아 테마)
342
+ # ------------------------------------------------------------------
343
+ DYSTOPIA_CSS = """
344
+ @import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&family=Orbitron:wght@400;700&display=swap');
345
+
346
+ .gradio-container {
347
+ background: linear-gradient(135deg, #0a0a0a 0%, #1a0a0a 50%, #0a0a0a 100%) !important;
348
+ font-family: 'Share Tech Mono', monospace !important;
349
+ background-image: repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(255,0,64,0.03) 2px, rgba(255,0,64,0.03) 4px);
350
+ }
351
+
352
+ .main {
353
+ background: transparent !important;
354
+ }
355
+
356
+ h1 {
357
+ font-family: 'Orbitron', monospace !important;
358
+ color: #ff0040 !important;
359
+ text-shadow: 0 0 10px #ff0040, 0 0 20px #ff0040, 0 0 40px #ff0040, 2px 0 cyan, -2px 0 yellow !important;
360
+ letter-spacing: 3px !important;
361
+ animation: glitch 0.5s infinite alternate;
362
+ }
363
+
364
+ @keyframes glitch {
365
+ 0% { transform: skew(0deg); opacity: 1; }
366
+ 20% { transform: skew(-2deg); opacity: 0.9; }
367
+ 40% { transform: skew(2deg); opacity: 0.95; }
368
+ 60% { transform: skew(-1deg); opacity: 0.9; }
369
+ 80% { transform: skew(1deg); opacity: 0.95; }
370
+ 100% { transform: skew(0deg); opacity: 1; }
371
+ }
372
+
373
+ p {
374
+ color: #ff4444 !important;
375
+ font-weight: bold;
376
+ }
377
+
378
+ #chatbot {
379
+ background: rgba(10, 0, 0, 0.9) !important;
380
+ border: 2px solid #ff0040 !important;
381
+ box-shadow: 0 0 20px rgba(255, 0, 64, 0.4), inset 0 0 50px rgba(255, 0, 0, 0.05) !important;
382
+ border-radius: 0 !important;
383
+ height: 500px !important;
384
+ }
385
+
386
+ .message.user {
387
+ background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%) !important;
388
+ border: 1px solid #0ff !important;
389
+ color: #0ff !important;
390
+ border-radius: 0 !important;
391
+ padding: 15px !important;
392
+ font-weight: bold;
393
+ }
394
+
395
+ .message.bot {
396
+ background: linear-gradient(135deg, #2a0a0a 0%, #1a0000 100%) !important;
397
+ border: 1px dashed #ff0040 !important;
398
+ color: #ff6666 !important;
399
+ border-radius: 0 !important;
400
+ padding: 15px !important;
401
+ font-weight: bold;
402
+ }
403
+
404
+ textarea {
405
+ background: #0a0a0a !important;
406
+ border: 2px solid #ff0040 !important;
407
+ color: #ff4444 !important;
408
+ border-radius: 0 !important;
409
+ font-family: 'Share Tech Mono', monospace !important;
410
+ font-size: 1.1em !important;
411
+ }
412
+
413
+ textarea:focus {
414
+ border-color: #ff0040 !important;
415
+ box-shadow: 0 0 15px rgba(255, 0, 64, 0.5) !important;
416
+ }
417
+
418
+ button {
419
+ background: linear-gradient(135deg, #ff0040 0%, #aa0030 100%) !important;
420
+ border: none !important;
421
+ color: #fff !important;
422
+ font-family: 'Orbitron', monospace !important;
423
+ text-transform: uppercase !important;
424
+ letter-spacing: 2px !important;
425
+ border-radius: 0 !important;
426
+ font-weight: bold !important;
427
+ }
428
+
429
+ button:hover {
430
+ background: linear-gradient(135deg, #ff3366 0%, #ff0040 100%) !important;
431
+ box-shadow: 0 0 20px rgba(255, 0, 64, 0.6) !important;
432
+ transform: translateY(-2px) !important;
433
+ }
434
+
435
+ .examples button {
436
+ background: rgba(255, 0, 64, 0.1) !important;
437
+ border: 1px solid #ff0040 !important;
438
+ color: #ff4444 !important;
439
+ }
440
+
441
+ .examples button:hover {
442
+ background: rgba(255, 0, 64, 0.3) !important;
443
+ }
444
+
445
+ ::-webkit-scrollbar {
446
+ width: 8px;
447
+ }
448
+
449
+ ::-webkit-scrollbar-track {
450
+ background: #0a0a0a;
451
+ }
452
+
453
+ ::-webkit-scrollbar-thumb {
454
+ background: #ff0040;
455
+ box-shadow: 0 0 10px #ff0040;
456
+ }
457
+
458
+ .gradio-container::before {
459
+ content: "";
460
+ position: fixed;
461
+ top: 0;
462
+ left: 0;
463
+ width: 100%;
464
+ height: 100%;
465
+ background: repeating-linear-gradient(
466
+ 0deg,
467
+ rgba(0, 0, 0, 0.15),
468
+ rgba(0, 0, 0, 0.15) 1px,
469
+ transparent 1px,
470
+ transparent 2px
471
+ );
472
+ pointer-events: none;
473
+ z-index: 1000;
474
+ }
475
+
476
+ footer {
477
+ color: #ff0040 !important;
478
+ }
479
+ """
480
+
481
+ # ------------------------------------------------------------------
482
+ # 5. Gradio 앱
483
+ # ------------------------------------------------------------------
484
+ with gr.Blocks(theme=gr.themes.Base(), css=DYSTOPIA_CSS) as demo:
485
+ gr.Markdown("# ☠️ DDOLBAE-9000 ☠️")
486
+ gr.Markdown("⚠️ WARNING: AI PERSONALITY CORRUPTED | 찐친 모드 활성화 ⚠️")
487
+
488
+ chatbot = gr.ChatInterface(
489
+ fn=chat_response,
490
+ retry_btn=None,
491
+ undo_btn=None,
492
+ clear_btn="🗑️ PURGE LOG",
493
+ examples=[
494
+ "야 안녕",
495
+ "뭐해?",
496
+ "밸런스 게임 하자",
497
+ "오늘 기분 좀 별로야",
498
+ "배고프다",
499
+ ],
500
+ )
501
+
502
+ gr.Markdown(
503
+ """
504
+ ---
505
+ import gradio as gr
506
  from huggingface_hub import hf_hub_download
507
  from llama_cpp import Llama
508
  import random
 
514
  FILENAME = "unsloth.Q4_K_M.gguf"
515
 
516
  print(f"📥 모델 다운로드 확인: {FILENAME}")
517
+ try:
518
+ model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
519
+ except Exception as e:
520
+ print(f"❌ 모델 다운로드 실패: {e}")
521
+ raise e
522
 
523
  print("🧠 엔진 시동 중...")
524
+ try:
525
+ llm = Llama(
526
+ model_path=model_path,
527
+ n_ctx=2048,
528
+ n_threads=2, # 무료 티어 CPU 개수에 맞춤
529
+ verbose=True
530
+ )
531
+ print("✅ 로딩 완료!")
532
+ except Exception as e:
533
+ print(f"❌ 엔진 로드 실패: {e}")
534
+ raise e
535
 
536
  # ------------------------------------------------------------------
537
  # 2. 시스템 프롬프트
 
568
  else:
569
  final_instruction = user_input
570
 
571
+ # 대화 히스토리 구성
572
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
573
 
574
  for user_msg, bot_msg in history:
 
577
 
578
  messages.append({"role": "user", "content": final_instruction})
579
 
580
+ # 생성
581
  response = llm.create_chat_completion(
582
  messages=messages,
583
  max_tokens=256,
 
753
 
754
  gr.Markdown(
755
  """
756
+ ---
757
+ [SYS] CONNECTION: ESTABLISHED
758
+ [SYS] NEURAL LINK: ACTIVE
759
+ [SYS] 찐친 모드: ON
760
+ [SYS] 예의: 0%
 
 
761
  """
762
  )
763
 
764
  if __name__ == "__main__":
765
+ demo.launch(server_name="0.0.0.0", server_port=7860)