rashmiprajapati commited on
Commit
6031ee9
·
1 Parent(s): 9147118

Initial commit of Healthbot files

Browse files
cbt-tinyllama/cbt-tinyllama-merged/chat_template.jinja ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% for message in messages %}
2
+ {% if message['role'] == 'user' %}
3
+ {{ '<|user|>
4
+ ' + message['content'] + eos_token }}
5
+ {% elif message['role'] == 'system' %}
6
+ {{ '<|system|>
7
+ ' + message['content'] + eos_token }}
8
+ {% elif message['role'] == 'assistant' %}
9
+ {{ '<|assistant|>
10
+ ' + message['content'] + eos_token }}
11
+ {% endif %}
12
+ {% if loop.last and add_generation_prompt %}
13
+ {{ '<|assistant|>' }}
14
+ {% endif %}
15
+ {% endfor %}
cbt-tinyllama/cbt-tinyllama-merged/config.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "LlamaForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 1,
8
+ "eos_token_id": 2,
9
+ "head_dim": 64,
10
+ "hidden_act": "silu",
11
+ "hidden_size": 2048,
12
+ "initializer_range": 0.02,
13
+ "intermediate_size": 5632,
14
+ "max_position_embeddings": 2048,
15
+ "mlp_bias": false,
16
+ "model_type": "llama",
17
+ "num_attention_heads": 32,
18
+ "num_hidden_layers": 22,
19
+ "num_key_value_heads": 4,
20
+ "pretraining_tp": 1,
21
+ "rms_norm_eps": 1e-05,
22
+ "rope_scaling": null,
23
+ "rope_theta": 10000.0,
24
+ "tie_word_embeddings": false,
25
+ "torch_dtype": "float32",
26
+ "transformers_version": "4.52.2",
27
+ "use_cache": true,
28
+ "vocab_size": 32000
29
+ }
cbt-tinyllama/cbt-tinyllama-merged/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b847be0c992dbe7bc7eb870b0d3b67bc1e02cee034771329d6a53da6f580496c
3
+ size 4400276670
cbt-tinyllama/cbt-tinyllama-merged/special_tokens_map.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": "</s>",
17
+ "unk_token": {
18
+ "content": "<unk>",
19
+ "lstrip": false,
20
+ "normalized": false,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ }
24
+ }
cbt-tinyllama/cbt-tinyllama-merged/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
cbt-tinyllama/cbt-tinyllama-merged/tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347
3
+ size 499723
cbt-tinyllama/cbt-tinyllama-merged/tokenizer_config.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": null,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<unk>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<s>",
16
+ "lstrip": false,
17
+ "normalized": false,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "2": {
23
+ "content": "</s>",
24
+ "lstrip": false,
25
+ "normalized": false,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": true
29
+ }
30
+ },
31
+ "bos_token": "<s>",
32
+ "clean_up_tokenization_spaces": false,
33
+ "eos_token": "</s>",
34
+ "extra_special_tokens": {},
35
+ "legacy": false,
36
+ "model_max_length": 2048,
37
+ "pad_token": "</s>",
38
+ "padding_side": "right",
39
+ "sp_model_kwargs": {},
40
+ "tokenizer_class": "LlamaTokenizer",
41
+ "unk_token": "<unk>",
42
+ "use_default_system_prompt": false
43
+ }
chatbot.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # --- Streamlit page config (must be first) ---
6
+ st.set_page_config(page_title="TinyLLaMA Chatbot", layout="centered")
7
+
8
+ # Device: CPU only
9
+ device = torch.device("cpu")
10
+
11
+ # --- Load the model and tokenizer ---
12
+ @st.cache_resource
13
+ def load_model():
14
+ model_path = r"C:\Users\HP\OneDrive\Desktop\HealthBot\cbt-tinyllama-merged\cbt-tinyllama-merged" # adjust if needed
15
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
16
+
17
+ # Set pad token if missing
18
+ if tokenizer.pad_token is None:
19
+ tokenizer.pad_token = tokenizer.eos_token
20
+
21
+ model = AutoModelForCausalLM.from_pretrained(model_path)
22
+ model.to(device)
23
+ model.eval()
24
+ return tokenizer, model
25
+
26
+ tokenizer, model = load_model()
27
+
28
+ # --- Custom styling for chat bubbles ---
29
+ st.markdown("""
30
+ <style>
31
+ .user-bubble {
32
+ background-color: #DCF8C6;
33
+ padding: 10px;
34
+ border-radius: 20px;
35
+ margin-bottom: 10px;
36
+ width: fit-content;
37
+ max-width: 80%;
38
+ align-self: flex-end;
39
+ }
40
+ .bot-bubble {
41
+ background-color: #F1F0F0;
42
+ padding: 10px;
43
+ border-radius: 20px;
44
+ margin-bottom: 10px;
45
+ width: fit-content;
46
+ max-width: 80%;
47
+ align-self: flex-start;
48
+ }
49
+ .chat-container {
50
+ display: flex;
51
+ flex-direction: column;
52
+ }
53
+ </style>
54
+ """, unsafe_allow_html=True)
55
+
56
+ # --- Title ---
57
+ st.title("🤖 TinyLLaMA Chatbot")
58
+ st.markdown("A conversational assistant powered by your fine-tuned TinyLLaMA model.")
59
+
60
+ # --- Initialize chat history ---
61
+ if "messages" not in st.session_state:
62
+ st.session_state.messages = []
63
+
64
+ # --- User input ---
65
+ user_input = st.chat_input("Type your message...")
66
+
67
+ # --- Generate response function ---
68
+ def generate_response(prompt):
69
+ input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
70
+ attention_mask = (input_ids != tokenizer.pad_token_id).long().to(device)
71
+
72
+ # Trim input to max length
73
+ max_length = model.config.max_position_embeddings
74
+ if input_ids.size(1) > max_length:
75
+ input_ids = input_ids[:, -max_length:]
76
+ attention_mask = attention_mask[:, -max_length:]
77
+
78
+ with torch.no_grad():
79
+ output_ids = model.generate(
80
+ input_ids,
81
+ attention_mask=attention_mask,
82
+ max_new_tokens=100,
83
+ do_sample=True,
84
+ top_k=50,
85
+ top_p=0.95,
86
+ temperature=0.8,
87
+ pad_token_id=tokenizer.eos_token_id
88
+ )
89
+
90
+ decoded = tokenizer.decode(output_ids[0], skip_special_tokens=True)
91
+ # Remove prompt from output and stop at next user prompt if exists
92
+ response = decoded[len(prompt):].split("User:")[0].strip()
93
+ return response
94
+
95
+ # --- Process user input ---
96
+ if user_input:
97
+ st.session_state.messages.append({"role": "user", "content": user_input})
98
+
99
+ # Build full prompt from history
100
+ prompt = ""
101
+ for msg in st.session_state.messages:
102
+ role = "User" if msg["role"] == "user" else "Assistant"
103
+ prompt += f"{role}: {msg['content']}\n"
104
+ prompt += "Assistant:"
105
+
106
+ bot_reply = generate_response(prompt)
107
+ st.session_state.messages.append({"role": "assistant", "content": bot_reply})
108
+
109
+ # --- Display chat ---
110
+ for msg in st.session_state.messages:
111
+ if msg["role"] == "user":
112
+ st.markdown(f'<div class="chat-container"><div class="user-bubble"><b>You:</b><br>{msg["content"]}</div></div>', unsafe_allow_html=True)
113
+ else:
114
+ st.markdown(f'<div class="chat-container"><div class="bot-bubble"><b>Bot:</b><br>{msg["content"]}</div></div>', unsafe_allow_html=True)
115
+
116
+