File size: 3,451 Bytes
1156ff4
 
 
 
 
80e0063
1156ff4
 
3da3e98
 
1156ff4
 
80e0063
e7c2321
1156ff4
 
 
 
 
 
 
 
 
 
 
 
 
0798f86
 
1156ff4
0798f86
e7c2321
 
 
0798f86
 
e7c2321
 
 
 
 
 
 
 
0798f86
 
 
 
1156ff4
0798f86
 
 
 
80e0063
1156ff4
0798f86
1156ff4
 
0798f86
1156ff4
e7c2321
 
1156ff4
 
 
 
 
80e0063
1156ff4
 
 
e7c2321
1156ff4
 
 
 
 
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
import os
import gradio as gr
from huggingface_hub import hf_hub_download
from llama_cpp import Llama

# 1. Завантажуємо наш моноліт із безлімітного Model Registry
print("Downloading base Llama 3.2 GGUF model...")
base_model_path = hf_hub_download(
    repo_id="nadtoka/llama-3.2-devops-notes-model",
    filename="custom_devops_llama_q4.gguf"
)

# 2. Ініціалізуємо рушій llama.cpp
print("Initializing LlamaContext...")
llm = Llama(
    model_path=base_model_path,
    n_ctx=2048,
    n_threads=2 # Оптимально для 2 vCPU на безкоштовному тарифі HF
)

# Жорсткий системний промпт Senior DevOps
SYSTEM_PROMPT = """You are an expert Senior DevOps Engineer, Cloud Architect, and SysAdmin. 
Your primary focus is Kubernetes, Docker, AWS, Linux, Terraform, and CI/CD pipelines. 
Always provide concise, highly technical answers. Use clear terminal commands and code blocks. 
If a user asks about topics outside of IT infrastructure, hardware, or software engineering, gently decline and steer the conversation back to tech."""

def respond(message, chat_history):
    # 1. Формуємо нативний список повідомлень (ідентично до train_lora.py)
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    
    # Наш універсальний парсер історії Gradio 6
    for item in chat_history:
        if isinstance(item, (list, tuple)) and len(item) == 2:
            user_msg, bot_msg = item
            if user_msg: messages.append({"role": "user", "content": user_msg})
            if bot_msg:  messages.append({"role": "assistant", "content": bot_msg})
        else:
            if isinstance(item, dict):
                role = item.get("role")
                content = item.get("content")
            else:
                role = getattr(item, "role", None)
                content = getattr(item, "content", None)
            if role and content:
                messages.append({"role": role, "content": content})
                
    # Додаємо поточний запит користувача
    messages.append({"role": "user", "content": message})
    
    # 2. Викликаємо нативний Chat Completion рушія llama.cpp
    # Він автоматично візьме вбудований чат-шаблон Llama 3.2 із твого GGUF моноліту!
    response = llm.create_chat_completion(
        messages=messages,
        max_tokens=1024,
        temperature=0.2,
        top_p=0.9
    )
    
    return response["choices"][0]["message"]["content"].strip()

# Створення вебінтерфейсу Gradio
with gr.Blocks() as demo:
    gr.Markdown("# 🦙 Llama 3.2 — Senior DevOps Knowledge Base (LoRA)")
    gr.Markdown("This SLM model has been locally fine-tuned via LoRA on custom DevOps engineering notes and infrastructure documentation.")
    
    chatbot = gr.ChatInterface(
        fn=respond,
        cache_examples=False, # 🔥 ОЦЕЙ РЯДОК РЯТУЄ ВІД SegFault (Exit code 139)!
        examples=[
            "How to clear a read-only lock on a Micron 9200 MAX SSD?",
            "Deploy Nginx as a reverse proxy for PHP-FPM inside Docker Swarm.",
            "What is the core difference between count and for_each in Terraform?"
        ]
    )

if __name__ == "__main__":
    demo.launch()