| import gradio as gr |
| from llama_cpp import Llama |
| from huggingface_hub import hf_hub_download |
|
|
| |
| |
| 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() |