import gradio as gr from llama_cpp import Llama from huggingface_hub import hf_hub_download # Скачиваем квантованную модель (4-bit Q4_K_M) # Это облегченная версия, которая имеет шансы запуститься на CPU model_path = hf_hub_download( repo_id="bartowski/phi-4-GGUF", filename="phi-4-Q4_K_M.gguf" ) # Инициализация модели llm = Llama( model_path=model_path, n_ctx=2048, # Контекст n_threads=4 # Количество ядер процессора ) def chat(message, history): messages = [{"role": "system", "content": "You are a helpful assistant."}] for user_msg, bot_msg in history: messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": bot_msg}) messages.append({"role": "user", "content": message}) # Генерация ответа response = "" stream = llm.create_chat_completion(messages=messages, stream=True) for chunk in stream: if "content" in chunk["choices"][0]["delta"]: response += chunk["choices"][0]["delta"]["content"] yield response demo = gr.ChatInterface( fn=chat, title="Phi-4 GGUF (CPU Basic)", description="Модель Phi-4 запущена на CPU. Работает медленно!" ) if __name__ == "__main__": demo.launch()