File size: 1,257 Bytes
4922fab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import hf_hub_download
from llama_cpp import Llama

MODEL_REPO = "bartowski/Phi-3-mini-4k-instruct-GGUF"
MODEL_FILE = "Phi-3-mini-4k-instruct-Q4_K_M.gguf"

print("Downloading model...")

model_path = hf_hub_download(
    repo_id=MODEL_REPO,
    filename=MODEL_FILE
)

print("Loading model...")

llm = Llama(
    model_path=model_path,
    n_ctx=2048,
    n_threads=4,
    verbose=False
)

SYSTEM_PROMPT = """
You are a senior software engineer.

You specialize in:

- Python
- JavaScript
- TypeScript
- PHP
- Pawn/OpenMP
- SQL
- C++
- Rust

Always:
- Explain clearly
- Generate complete code
- Follow best practices
"""

def chat(message, history):

    conversation = ""

    for user, assistant in history:
        conversation += f"User: {user}\n"
        conversation += f"Assistant: {assistant}\n"

    prompt = f"""
{SYSTEM_PROMPT}

{conversation}

User: {message}
Assistant:
"""

    output = llm(
        prompt,
        max_tokens=256,
        temperature=0.2,
        top_p=0.95,
        stop=["User:"]
    )

    return output["choices"][0]["text"].strip()

demo = gr.ChatInterface(
    fn=chat,
    title="AI Coding Assistant",
    description="Running locally on Hugging Face CPU Basic"
)

demo.launch()