ginipick commited on
Commit
41910ca
·
verified ·
1 Parent(s): 6900566

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -439
app.py DELETED
@@ -1,439 +0,0 @@
1
- import spaces
2
- import json
3
- import subprocess
4
- import os
5
- from llama_cpp import Llama
6
- from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
7
- from llama_cpp_agent.providers import LlamaCppPythonProvider
8
- from llama_cpp_agent.chat_history import BasicChatHistory
9
- from llama_cpp_agent.chat_history.messages import Roles
10
- import gradio as gr
11
- from huggingface_hub import hf_hub_download
12
-
13
- llm = None
14
- llm_model = None
15
-
16
- # 모델 이름과 경로를 정의
17
- MISTRAL_MODEL_NAME = "Private-BitSix-Mistral-Small-3.1-24B-Instruct-2503.gguf"
18
-
19
- # 모델 다운로드
20
- model_path = hf_hub_download(
21
- repo_id="ginigen/Private-BitSix-Mistral-Small-3.1-24B-Instruct-2503",
22
- filename=MISTRAL_MODEL_NAME,
23
- local_dir="./models"
24
- )
25
-
26
- print(f"Downloaded model path: {model_path}")
27
-
28
- # 파스텔톤 색상 정의
29
- PASTEL_COLORS = {
30
- "background": "#f9f7ff",
31
- "primary": "#d8b4fe",
32
- "secondary": "#e9d5ff",
33
- "accent": "#c084fc",
34
- "text": "#4c1d95",
35
- "border": "#ddd6fe",
36
- "dark_bg": "#1e1a2e",
37
- "dark_primary": "#9d74d9",
38
- "dark_secondary": "#7c58b9",
39
- "dark_accent": "#8b5cf6",
40
- "dark_text": "#e9d5ff",
41
- "dark_border": "#5b21b6"
42
- }
43
-
44
- css = """
45
- .bubble-wrap {
46
- padding-top: calc(var(--spacing-xl) * 3) !important;
47
- }
48
- .message-row {
49
- justify-content: space-evenly !important;
50
- width: 100% !important;
51
- max-width: 100% !important;
52
- margin: calc(var(--spacing-xl)) 0 !important;
53
- padding: 0 calc(var(--spacing-xl) * 3) !important;
54
- }
55
- .flex-wrap.user {
56
- border-bottom-right-radius: var(--radius-lg) !important;
57
- background: #f3e8ff !important;
58
- border: 1px solid #ddd6fe !important;
59
- }
60
- .flex-wrap.bot {
61
- border-bottom-left-radius: var(--radius-lg) !important;
62
- background: #ede9fe !important;
63
- border: 1px solid #ddd6fe !important;
64
- }
65
- .message.user{
66
- padding: 10px;
67
- color: #5b21b6;
68
- }
69
- .message.bot{
70
- text-align: right;
71
- width: 100%;
72
- padding: 10px;
73
- border-radius: 10px;
74
- color: #6d28d9;
75
- }
76
- .message-bubble-border {
77
- border-radius: 12px !important;
78
- box-shadow: 0 2px 10px rgba(186, 162, 255, 0.2) !important;
79
- }
80
- .message-buttons {
81
- justify-content: flex-end !important;
82
- }
83
- .message-buttons-left {
84
- align-self: end !important;
85
- }
86
- .message-buttons-bot, .message-buttons-user {
87
- right: 10px !important;
88
- left: auto !important;
89
- bottom: 2px !important;
90
- }
91
- .dark.message-bubble-border {
92
- border-color: #5b21b6 !important;
93
- box-shadow: 0 2px 10px rgba(139, 92, 246, 0.3) !important;
94
- }
95
- .dark.user {
96
- background: #2e1065 !important;
97
- border: 1px solid #5b21b6 !important;
98
- }
99
- .dark.assistant.dark, .dark.pending.dark {
100
- background: #27104a !important;
101
- border: 1px solid #5b21b6 !important;
102
- }
103
-
104
- /* 커스텀 스타일 추가 */
105
- .gradio-container {
106
- background-color: #f5f3ff !important;
107
- }
108
- .dark .gradio-container {
109
- background-color: #1e1a2e !important;
110
- }
111
- .gr-button-primary {
112
- background-color: #a78bfa !important;
113
- border: 1px solid #8b5cf6 !important;
114
- }
115
- .dark .gr-button-primary {
116
- background-color: #7c3aed !important;
117
- border: 1px solid #6d28d9 !important;
118
- }
119
- .gr-input, .gr-textarea {
120
- border: 1px solid #ddd6fe !important;
121
- background-color: #faf5ff !important;
122
- }
123
- .dark .gr-input, .dark .gr-textarea {
124
- border: 1px solid #5b21b6 !important;
125
- background-color: #27104a !important;
126
- color: #e9d5ff !important;
127
- }
128
- .gr-padded {
129
- border-radius: 12px !important;
130
- }
131
- .gr-box {
132
- border-radius: 12px !important;
133
- border: 1px solid #ddd6fe !important;
134
- box-shadow: 0 4px 12px rgba(186, 162, 255, 0.15) !important;
135
- }
136
- .dark .gr-box {
137
- border: 1px solid #5b21b6 !important;
138
- box-shadow: 0 4px 12px rgba(139, 92, 246, 0.2) !important;
139
- }
140
-
141
- /* 모달 스타일 */
142
- .modal-overlay {
143
- position: fixed;
144
- top: 0;
145
- left: 0;
146
- width: 100%;
147
- height: 100%;
148
- background-color: rgba(0, 0, 0, 0.5);
149
- display: flex;
150
- justify-content: center;
151
- align-items: center;
152
- z-index: 1000;
153
- opacity: 0;
154
- transition: opacity 0.3s ease;
155
- }
156
- .modal-content {
157
- background-color: #f5f3ff;
158
- border-radius: 16px;
159
- padding: 20px;
160
- box-shadow: 0 10px 25px rgba(139, 92, 246, 0.3);
161
- text-align: center;
162
- max-width: 400px;
163
- width: 100%;
164
- transform: scale(0.8);
165
- transition: transform 0.3s ease;
166
- }
167
- .dark .modal-content {
168
- background-color: #1e1a2e;
169
- box-shadow: 0 10px 25px rgba(109, 40, 217, 0.4);
170
- }
171
- .modal-header {
172
- margin-bottom: 15px;
173
- color: #6d28d9;
174
- font-size: 20px;
175
- font-weight: bold;
176
- }
177
- .dark .modal-header {
178
- color: #a78bfa;
179
- }
180
- .modal-img {
181
- width: 200px;
182
- height: 200px;
183
- object-fit: contain;
184
- border-radius: 8px;
185
- margin-bottom: 15px;
186
- border: 3px solid #c4b5fd;
187
- }
188
- .dark .modal-img {
189
- border-color: #7c3aed;
190
- }
191
- .modal-close {
192
- background-color: #a78bfa;
193
- color: white;
194
- border: none;
195
- padding: 8px 15px;
196
- border-radius: 8px;
197
- cursor: pointer;
198
- font-weight: bold;
199
- margin-top: 10px;
200
- transition: background-color 0.2s;
201
- }
202
- .modal-close:hover {
203
- background-color: #8b5cf6;
204
- }
205
- .dark .modal-close {
206
- background-color: #7c3aed;
207
- }
208
- .dark .modal-close:hover {
209
- background-color: #6d28d9;
210
- }
211
- .show-modal {
212
- opacity: 1;
213
- }
214
- .show-modal .modal-content {
215
- transform: scale(1);
216
- }
217
- """
218
-
219
- # HTML 코드 추가 - 이미지 팝업 모달
220
- welcome_modal_html = """
221
- <div id="welcomeModal" class="modal-overlay">
222
- <div class="modal-content">
223
- <div class="modal-header">Private-BitSix AI에 오신 것을 환영합니다</div>
224
- <img src="file/llama.jpg" alt="Llama Logo" class="modal-img">
225
- <p style="margin-bottom: 15px; color: #6d28d9;">6BIT 양자화로 모델 크기는 줄이고 성능은 유지하는 프라이버시 중심 AI 솔루션.</p>
226
- <button id="closeModal" class="modal-close">시작하기</button>
227
- </div>
228
- </div>
229
-
230
- <script>
231
- // 페이지 로드 후 모달 표시
232
- document.addEventListener('DOMContentLoaded', function() {
233
- setTimeout(function() {
234
- var modal = document.getElementById('welcomeModal');
235
- modal.classList.add('show-modal');
236
-
237
- // 닫기 버튼 클릭 시 모달 닫기
238
- document.getElementById('closeModal').addEventListener('click', function() {
239
- modal.classList.remove('show-modal');
240
- setTimeout(function() {
241
- modal.style.display = 'none';
242
- }, 300);
243
- });
244
- }, 500);
245
- });
246
- </script>
247
- """
248
-
249
- def get_messages_formatter_type(model_name):
250
- if "Mistral" in model_name or "BitSix" in model_name:
251
- return MessagesFormatterType.CHATML # Mistral 계열 모델은 ChatML 형식 사용
252
- else:
253
- raise ValueError(f"Unsupported model: {model_name}")
254
-
255
- @spaces.GPU(duration=120)
256
- def respond(
257
- message,
258
- history: list[tuple[str, str]],
259
- system_message,
260
- max_tokens,
261
- temperature,
262
- top_p,
263
- top_k,
264
- repeat_penalty,
265
- ):
266
- global llm
267
- global llm_model
268
-
269
- chat_template = get_messages_formatter_type(MISTRAL_MODEL_NAME)
270
-
271
- # 모델 파일 경로 확인
272
- model_path = os.path.join("./models", MISTRAL_MODEL_NAME)
273
-
274
- print(f"Model path: {model_path}")
275
-
276
- if not os.path.exists(model_path):
277
- print(f"Warning: Model file not found at {model_path}")
278
- print(f"Available files in ./models: {os.listdir('./models')}")
279
-
280
- if llm is None or llm_model != MISTRAL_MODEL_NAME:
281
- llm = Llama(
282
- model_path=model_path,
283
- flash_attn=True,
284
- n_gpu_layers=81,
285
- n_batch=1024,
286
- n_ctx=8192,
287
- )
288
- llm_model = MISTRAL_MODEL_NAME
289
-
290
- provider = LlamaCppPythonProvider(llm)
291
-
292
- agent = LlamaCppAgent(
293
- provider,
294
- system_prompt=f"{system_message}",
295
- predefined_messages_formatter_type=chat_template,
296
- debug_output=True
297
- )
298
-
299
- settings = provider.get_provider_default_settings()
300
- settings.temperature = temperature
301
- settings.top_k = top_k
302
- settings.top_p = top_p
303
- settings.max_tokens = max_tokens
304
- settings.repeat_penalty = repeat_penalty
305
- settings.stream = True
306
-
307
- messages = BasicChatHistory()
308
-
309
- for msn in history:
310
- user = {
311
- 'role': Roles.user,
312
- 'content': msn[0]
313
- }
314
- assistant = {
315
- 'role': Roles.assistant,
316
- 'content': msn[1]
317
- }
318
- messages.add_message(user)
319
- messages.add_message(assistant)
320
-
321
- stream = agent.get_chat_response(
322
- message,
323
- llm_sampling_settings=settings,
324
- chat_history=messages,
325
- returns_streaming_generator=True,
326
- print_output=False
327
- )
328
-
329
- outputs = ""
330
- for output in stream:
331
- outputs += output
332
- yield outputs
333
-
334
- # 파스텔톤 테마로 업데이트된 플레이스홀더
335
- PLACEHOLDER = """
336
- <div class="message-bubble-border" style="display:flex; max-width: 600px; border-radius: 16px; border-width: 1px; border-color: #ddd6fe; box-shadow: 0 4px 12px rgba(186, 162, 255, 0.2); backdrop-filter: blur(10px);">
337
- <figure style="margin: 0;max-width: 200px;min-height: 300px;">
338
- <img src="file/llama.jpg" alt="Logo" style="width: 100%; height: 100%; border-radius: 12px; border: 3px solid #c4b5fd;">
339
- </figure>
340
- <div style="padding: 1rem 1.5rem;display: flex;flex-direction: column;justify-content: space-evenly;">
341
- <h2 style="text-align: left; font-size: 1.8rem; font-weight: 700; margin-bottom: 0.8rem; color: #6d28d9;">Ginigen Private-BitSix</h2>
342
- <p style="text-align: left; font-size: 16px; line-height: 1.6; margin-bottom: 15px; color: #7c3aed;">The Ginigen Private-BitSix framework simplifies interactions with Large Language Models (LLMs), providing an interface for chatting, executing function calls, generating structured output, performing retrieval augmented generation, and processing text using agentic chains with tools.</p>
343
- <div style="display: flex; justify-content: space-between; align-items: center;">
344
- <div style="display: flex; flex-flow: column; justify-content: space-between;">
345
- <span style="display: inline-flex; align-items: center; border-radius: 0.5rem; background-color: rgba(192, 132, 252, 0.2); padding: 0.2rem 0.75rem; font-size: 0.8rem; font-weight: 500; color: #8b5cf6; margin-bottom: 5px; border: 1px solid #c4b5fd;">
346
- Private BitSix Mistral Small 3.1 24B Instruct
347
- </span>
348
- </div>
349
- <div style="display: flex; justify-content: flex-end; align-items: center;">
350
- <a href="https://discord.gg/openfreeai" target="_blank" rel="noreferrer" style="padding: .5rem; color: #6d28d9;">
351
- <svg width="24" height="24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 5 30.67 23.25">
352
- <title>Discord</title>
353
- <path d="M26.0015 6.9529C24.0021 6.03845 21.8787 5.37198 19.6623 5C19.3833 5.48048 19.0733 6.13144 18.8563 6.64292C16.4989 6.30193 14.1585 6.30193 11.8336 6.64292C11.6166 6.13144 11.2911 5.48048 11.0276 5C8.79575 5.37198 6.67235 6.03845 4.6869 6.9529C0.672601 12.8736 -0.41235 18.6548 0.130124 24.3585C2.79599 26.2959 5.36889 27.4739 7.89682 28.2489C8.51679 27.4119 9.07477 26.5129 9.55525 25.5675C8.64079 25.2265 7.77283 24.808 6.93587 24.312C7.15286 24.1571 7.36986 23.9866 7.57135 23.8161C12.6241 26.1255 18.0969 26.1255 23.0876 23.8161C23.3046 23.9866 23.5061 24.1571 23.7231 24.312C22.8861 24.808 22.0182 25.2265 21.1037 25.5675C21.5842 26.5129 22.1422 27.4119 22.7621 28.2489C25.2885 27.4739 27.8769 26.2959 30.5288 24.3585C31.1952 17.7559 29.4733 12.0212 26.0015 6.9529ZM10.2527 20.8402C8.73376 20.8402 7.49382 19.4608 7.49382 17.7714C7.49382 16.082 8.70276 14.7025 10.2527 14.7025C11.7871 14.7025 13.0425 16.082 13.0115 17.7714C13.0115 19.4608 11.7871 20.8402 10.2527 20.8402ZM20.4373 20.8402C18.9183 20.8402 17.6768 19.4608 17.6768 17.7714C17.6768 16.082 18.8873 14.7025 20.4373 14.7025C21.9717 14.7025 23.2271 16.082 23.1961 17.7714C23.1961 19.4608 21.9872 20.8402 20.4373 20.8402Z"></path>
354
- </svg>
355
- </a>
356
- </div>
357
- </div>
358
- </div>
359
- </div>
360
- """
361
-
362
- # 파스텔톤 테마 설정
363
- pastel_theme = gr.themes.Soft(
364
- primary_hue=gr.themes.colors.purple,
365
- secondary_hue=gr.themes.colors.violet,
366
- neutral_hue=gr.themes.colors.gray,
367
- font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
368
- ).set(
369
- body_background_fill="#f5f3ff",
370
- body_background_fill_dark="#1e1a2e",
371
- block_background_fill="#f8f4ff",
372
- block_background_fill_dark="#27104a",
373
- block_border_width="1px",
374
- block_border_color="#ddd6fe",
375
- block_border_color_dark="#5b21b6",
376
- block_title_background_fill="#f3e8ff",
377
- block_title_background_fill_dark="#2e1065",
378
- block_title_text_color="#6d28d9",
379
- block_title_text_color_dark="#c4b5fd",
380
- block_label_background_fill="#faf5ff",
381
- block_label_background_fill_dark="#1e1a2e",
382
- block_label_text_color="#7c3aed",
383
- block_label_text_color_dark="#a78bfa",
384
- input_background_fill="#faf5ff",
385
- input_background_fill_dark="#27104a",
386
- input_border_color="#ddd6fe",
387
- input_border_color_dark="#5b21b6",
388
- input_border_width="1px",
389
- button_primary_background_fill="#a78bfa",
390
- button_primary_background_fill_dark="#7c3aed",
391
- button_primary_text_color="white",
392
- button_primary_text_color_dark="white",
393
- button_secondary_background_fill="#f3e8ff",
394
- button_secondary_background_fill_dark="#2e1065",
395
- button_secondary_text_color="#6d28d9",
396
- button_secondary_text_color_dark="#a78bfa",
397
- border_color_accent="#c4b5fd",
398
- border_color_accent_dark="#7c3aed",
399
- border_color_primary="#ddd6fe",
400
- border_color_primary_dark="#5b21b6",
401
- background_fill_secondary="#f3e8ff",
402
- background_fill_secondary_dark="#1e1a2e",
403
- color_accent_soft="#ede9fe",
404
- color_accent_soft_dark="#3b0764",
405
- code_background_fill="#f3e8ff",
406
- code_background_fill_dark="#2e1065",
407
- )
408
-
409
- with gr.Blocks(theme=pastel_theme, css=css) as demo:
410
- # 웰컴 모달 추가
411
- gr.HTML(welcome_modal_html)
412
-
413
- # 챗봇 인터페이스
414
- gr.ChatInterface(
415
- fn=respond,
416
- title="Ginigen Private AI",
417
- description="6BIT 양자화로 모델 크기는 줄이고 성능은 유지하는 프라이버시 중심 AI 솔루션.",
418
- examples=[
419
- ["안녕하세요, 저는 AI에 관심이 많습니다. 양자화란 무엇인가요?"],
420
- ["미스트랄 모델의 특징은 무엇인가요?"],
421
- ["긴 컨텍스트(context)를 처리하는 방법을 설명해 주세요."]
422
- ],
423
- additional_inputs=[
424
- gr.Textbox(
425
- value="You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem.",
426
- label="시스템 메시지",
427
- lines=5
428
- ),
429
- gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="최대 토큰 수"),
430
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
431
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
432
- gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k"),
433
- gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty"),
434
- ],
435
- chatbot=gr.Chatbot(placeholder=PLACEHOLDER, type="messages")
436
- )
437
-
438
- if __name__ == "__main__":
439
- demo.launch()