File size: 2,691 Bytes
07113c4
 
 
 
ca1f085
138b42d
ca1f085
07113c4
 
 
138b42d
 
07113c4
 
ca1f085
138b42d
ca1f085
 
07113c4
 
138b42d
 
07113c4
 
 
 
 
 
 
 
 
 
 
138b42d
ca1f085
11e6ed2
07113c4
 
 
138b42d
 
 
 
 
07113c4
 
b28cab5
 
 
11e6ed2
 
 
 
138b42d
 
 
 
11e6ed2
 
138b42d
11e6ed2
 
138b42d
11e6ed2
07113c4
138b42d
11e6ed2
138b42d
07113c4
 
138b42d
 
 
 
 
11e6ed2
138b42d
ca1f085
 
138b42d
 
ca1f085
138b42d
 
07113c4
138b42d
b28cab5
138b42d
11e6ed2
b28cab5
07113c4
 
 
138b42d
11e6ed2
07113c4
 
138b42d
 
 
b28cab5
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import torch
import gradio as gr
from transformers import pipeline

# -------------------------------
# Load Model (CPU-safe)
# -------------------------------
pipe = pipeline(
    "text-generation",
    model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
    torch_dtype=torch.float32,
    device_map=None,
)

# -------------------------------
# Chat Function (messages format)
# -------------------------------
def chat(user_message, history, system_prompt, temperature, max_tokens):
    messages = [{"role": "system", "content": system_prompt}]

    if history:
        messages.extend(history)

    messages.append({"role": "user", "content": user_message})

    prompt = pipe.tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True,
    )

    output = pipe(
        prompt,
        max_new_tokens=int(max_tokens),
        temperature=temperature,
        top_p=0.9,
        do_sample=True,
    )

    assistant_reply = output[0]["generated_text"].split("<|assistant|>")[-1].strip()

    history.append({"role": "user", "content": user_message})
    history.append({"role": "assistant", "content": assistant_reply})

    return history

# -------------------------------
# Gradio UI
# -------------------------------
with gr.Blocks(title="TinyLLaMA Chatbot") as demo:
    gr.Markdown("## πŸ¦™ TinyLLaMA Chatbot")

    preset_prompts = {
        "Pirate πŸ΄β€β˜ οΈ": "You are a pirate chatbot. Speak like a pirate.",
        "Teacher πŸ‘¨β€πŸ«": "You are a patient teacher.",
        "Coder πŸ‘¨β€πŸ’»": "You are a programming assistant.",
        "Friendly πŸ€–": "You are a friendly assistant."
    }

    personality = gr.Dropdown(
        choices=list(preset_prompts.keys()),
        value="Pirate πŸ΄β€β˜ οΈ",
        label="Choose Personality"
    )

    system_prompt = gr.Textbox(
        value=preset_prompts["Pirate πŸ΄β€β˜ οΈ"],
        label="System Prompt"
    )

    personality.change(
        lambda x: preset_prompts[x],
        inputs=personality,
        outputs=system_prompt
    )

    chatbot = gr.Chatbot(type="messages", height=400)
    user_input = gr.Textbox(label="Your Message")

    temperature = gr.Slider(0.1, 1.2, value=0.85)
    max_tokens = gr.Slider(32, 128, value=96, step=16)

    send = gr.Button("Send πŸš€")
    clear = gr.Button("Clear 🧹")

    send.click(
        chat,
        inputs=[user_input, chatbot, system_prompt, temperature, max_tokens],
        outputs=chatbot
    )

    user_input.submit(
        chat,
        inputs=[user_input, chatbot, system_prompt, temperature, max_tokens],
        outputs=chatbot
    )

    clear.click(lambda: [], outputs=chatbot)

demo.launch()