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

# =========================
# LOAD MODEL FROM HF REPO
# =========================
model_path = hf_hub_download(
    repo_id="Mikecode123/ALX",
    filename="qwen2-1_5b-instruct-q4_0.gguf"
)

llm = Llama(
    model_path=model_path,
    n_ctx=1024,
    n_threads=2
)

# =========================
# CHAT FUNCTION
# =========================
def chat(prompt, history):
    messages = []

    # convert gradio history to chat format
    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": prompt})

    output = llm.create_chat_completion(
        messages=messages,
        max_tokens=500,
        temperature=0.7
    )

    response = output["choices"][0]["message"]["content"]

    history.append((prompt, response))
    return "", history

# =========================
# GRADIO UI
# =========================
with gr.Blocks() as demo:
    gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Build)")

    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Ask your AI")
    clear = gr.Button("Clear")

    msg.submit(chat, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: None, None, chatbot)

# =========================
# LAUNCH
# =========================
demo.launch()