File size: 8,697 Bytes
4b20767
c3da58c
3fe28a6
92773d2
c3da58c
 
 
 
4b20767
3fe28a6
92773d2
70405cd
 
92773d2
 
 
 
 
c3da58c
92773d2
 
 
 
c3da58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92773d2
 
c3da58c
92773d2
c3da58c
3fe28a6
92773d2
 
c3da58c
92773d2
 
 
 
 
4b20767
5d83c2a
3fe28a6
92773d2
3fe28a6
c4cd467
92773d2
 
 
50c1962
92773d2
c4cd467
 
5d83c2a
c4cd467
330b1d3
3fe28a6
 
330b1d3
5d83c2a
c4cd467
92773d2
c4cd467
c3da58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92773d2
 
fd4506a
c3da58c
 
 
 
4b38efb
c4cd467
 
92773d2
 
c3da58c
 
 
 
92773d2
4b20767
5406190
 
c3da58c
 
14d1f54
 
c3da58c
c4cd467
b4c97ca
c3da58c
 
 
 
 
 
 
 
 
 
 
 
5d83c2a
c3da58c
 
5406190
c3da58c
fd4506a
 
c3da58c
 
 
 
 
 
 
c4cd467
5d83c2a
c3da58c
 
5d83c2a
c3da58c
 
 
 
 
 
5d83c2a
c3da58c
 
fd4506a
5d83c2a
c3da58c
330b1d3
3fe28a6
 
 
 
 
 
4b38efb
c3da58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b20767
 
c3da58c
 
 
 
 
 
 
 
5406190
 
c3da58c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
from typing import List, Dict, Any, Tuple
import torch
import warnings

# Подавляем ненужные предупреждения
warnings.filterwarnings("ignore", message=".*low_cpu_mem_usage.*")

# CPU-модели (только одна маленькая модель для экономии памяти)
MODELS = {
    "Qwen2.5-0.5B": "Qwen/Qwen2.5-0.5B-Instruct",
    "Qwen2.5-1.5B": "Qwen/Qwen2.5-1.5B-Instruct",
}

def load_model(model_key: str):
    model_id = MODELS[model_key]
    print(f"🚀 Загрузка {model_id}...")
    
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
    
    # Сначала загружаем модель отдельно с оптимизацией памяти
    try:
        model = AutoModelForCausalLM.from_pretrained(
            model_id,
            torch_dtype=torch.float32,
            device_map=None,  # Используем CPU
            low_cpu_mem_usage=True,
            trust_remote_code=True
        )
    except Exception as e:
        print(f"⚠️ Не удалось загрузить с low_cpu_mem_usage: {e}")
        print("🔄 Пробуем без low_cpu_mem_usage...")
        model = AutoModelForCausalLM.from_pretrained(
            model_id,
            torch_dtype=torch.float32,
            trust_remote_code=True
        )
    
    # Затем создаем pipeline
    pipe = pipeline(
        "text-generation",
        model=model,
        tokenizer=tokenizer,
        device=-1,  # Явно указываем CPU (-1 означает CPU)
        max_new_tokens=128,  # Ещё меньше токенов для экономии памяти
        do_sample=True,
        temperature=0.7,
        pad_token_id=tokenizer.eos_token_id
    )
    print(f"✅ {model_id} загружена!")
    return pipe

model_cache = {}

def respond(message: str, 
            history: List[Dict[str, str]], 
            model_key: str, 
            system_prompt: str) -> Tuple[List[Dict[str, str]], str, Dict[str, Any]]:
    try:
        if model_key not in model_cache:
            model_cache[model_key] = load_model(model_key)
        pipe = model_cache[model_key]
        
        print(f"🚀 Генерация: {model_key}, Msg='{message[:30]}...'")
        
        messages = []
        if system_prompt.strip():
            messages.append({"role": "system", "content": system_prompt})
        
        for msg in history:
            messages.append({"role": msg["role"], "content": msg["content"]})
        
        messages.append({"role": "user", "content": message})
        
        tokenizer = pipe.tokenizer
        
        # Используем чат-шаблон для Qwen моделей
        try:
            prompt = tokenizer.apply_chat_template(
                messages, 
                tokenize=False, 
                add_generation_prompt=True
            )
        except Exception as e:
            print(f"⚠️ Ошибка применения чат-шаблона: {e}")
            # Альтернативный способ форматирования
            prompt = ""
            for msg in messages:
                if msg["role"] == "system":
                    prompt += f"System: {msg['content']}\n\n"
                elif msg["role"] == "user":
                    prompt += f"User: {msg['content']}\n\n"
                elif msg["role"] == "assistant":
                    prompt += f"Assistant: {msg['content']}\n\n"
            prompt += "Assistant:"
        
        outputs = pipe(
            prompt, 
            max_new_tokens=256,  # Уменьшил для экономии памяти
            do_sample=True, 
            temperature=0.7,
            repetition_penalty=1.1
        )
        
        # Извлекаем ответ
        generated_text = outputs[0]["generated_text"]
        if generated_text.startswith(prompt):
            bot_reply = generated_text[len(prompt):].strip()
        else:
            bot_reply = generated_text.strip()
        
        print(f"✅ Ответ: {bot_reply[:50]}...")
        
        new_history = history + [
            {"role": "user", "content": message}, 
            {"role": "assistant", "content": bot_reply}
        ]
        return new_history, "", gr.update(value="")
    
    except Exception as e:
        error_msg = f"❌ {model_key}: {str(e)}"
        print(f"💥 {error_msg}")
        new_history = history + [
            {"role": "user", "content": message}, 
            {"role": "assistant", "content": error_msg}
        ]
        return new_history, error_msg, gr.update(value="")

# Исправлено: убран параметр theme для совместимости со старой версией Gradio
with gr.Blocks(title="🚀 Локальный HF Чат (на слабом CPU!)") as demo:
    gr.Markdown("""
    # 🚀 Локальный Inference (без API!)
   
    ⚠️ **Внимание**: Модели загружаются при первом выборе и могут занять несколько минут! Работают на слабом CPU - запаситесь терпением.
    """)
    
    with gr.Row():
        model_dropdown = gr.Dropdown(
            choices=list(MODELS.keys()), 
            value="Qwen2.5-0.5B", 
            label="🧠 Модель",
            info="Выберите модель (загрузка при первшем использовании)"
        )
        system_prompt = gr.Textbox(
            label="📝 System Prompt", 
            placeholder="Ты весёлый и полезный ИИ-ассистент.", 
            lines=2,
            value="Ты весёлый и полезный ИИ-ассистент."
        )
    
    chatbot = gr.Chatbot(
        height=400, 
        label="Чат"
    )
    
    with gr.Row():
        msg_input = gr.Textbox(
            placeholder="Напишите сообщение... (Enter для отправки)", 
            show_label=False, 
            lines=2,
            scale=4
        )
        send_btn = gr.Button("📤 Отправить", variant="primary", scale=1)
    
    with gr.Row():
        clear_btn = gr.Button("🗑️ Очистить историю", variant="secondary")
        retry_btn = gr.Button("🔄 Повторить последнее", variant="secondary")
    
    status = gr.Textbox(
        label="Статус", 
        interactive=False, 
        lines=3,
        placeholder="Здесь будут отображаться логи работы..."
    )
    
    # Обработчики событий
    def clear_chat():
        return [], "", gr.update(value="")
    
    def retry_last(history: List[Dict[str, str]]):
        if history:
            last_user_msg = None
            for msg in reversed(history):
                if msg["role"] == "user":
                    last_user_msg = msg["content"]
                    break
            return last_user_msg if last_user_msg else ""
        return ""
    
    # Привязка событий
    send_btn.click(
        fn=respond, 
        inputs=[msg_input, chatbot, model_dropdown, system_prompt], 
        outputs=[chatbot, status, msg_input]
    )
    
    msg_input.submit(
        fn=respond, 
        inputs=[msg_input, chatbot, model_dropdown, system_prompt], 
        outputs=[chatbot, status, msg_input]
    )
    
    clear_btn.click(
        fn=clear_chat, 
        outputs=[chatbot, status, msg_input]
    )
    
    retry_btn.click(
        fn=retry_last, 
        inputs=[chatbot], 
        outputs=[msg_input]
    )
    
    # Информация о состоянии
    gr.Markdown("""
    ### 💡 Советы:
    1. Первая загрузка модели может занять 1-5 минут
    2. Ответы генерируются на CPU, будьте терпеливы
    3. Для более быстрых ответов используйте Qwen2.5-0.5B
    4. Очищайте историю, если чат становится медленным
    """)

if __name__ == "__main__":
    print("=" * 50)
    print("🚀 Запуск локального чат-бота на CPU")
    print("=" * 50)
    
    demo.queue(max_size=5).launch(
        debug=False, 
        show_error=True,
        server_name="0.0.0.0",
        server_port=7860,
        share=False  # Отключаем share для локального использования
    )