├── ass.png
├── man.png
├── lamini77.jpg
├── logo248m.png
├── logo77m.png
├── assistant.png
├── Streamlit-intrface.png
├── encoder-decoder-comparisons.png
├── model77M
└── yourmodelhere.txt
├── README.md
└── st-laminiChat.py
/ass.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/ass.png
--------------------------------------------------------------------------------
/man.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/man.png
--------------------------------------------------------------------------------
/lamini77.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/lamini77.jpg
--------------------------------------------------------------------------------
/logo248m.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/logo248m.png
--------------------------------------------------------------------------------
/logo77m.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/logo77m.png
--------------------------------------------------------------------------------
/assistant.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/assistant.png
--------------------------------------------------------------------------------
/Streamlit-intrface.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/Streamlit-intrface.png
--------------------------------------------------------------------------------
/encoder-decoder-comparisons.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fabiomatricardi/77M-chatbot-is-reality/main/encoder-decoder-comparisons.png
--------------------------------------------------------------------------------
/model77M/yourmodelhere.txt:
--------------------------------------------------------------------------------
1 | download all the files from
2 |
3 | https://huggingface.co/MBZUAI/LaMini-Flan-T5-77M/tree/main
4 | ```
5 | config.json
6 | generation_config.json
7 | pytorch_model.bin
8 | README.md
9 | special_tokens_map.json
10 | tokenizer.json
11 | tokenizer_config.json
12 | training_args.bin
13 | ```
14 | into this directory
15 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 77M-chatbot-is-reality
2 | Run encoder-decoder LaMini-Flan-T5 with streamlit
3 |
4 | test the powers of encoder-decoder models
5 |
6 |
7 |
8 | ### How to use it
9 | works with Python 3.11+
10 | tested on Windows 11 machine
11 | - Create a new folder for the project (mine is `LaminiST`)
12 | - Create a virtual environment and activate it
13 |
14 | ```
15 | python -m venv venv
16 | venv\Scripts\activate
17 | pip install streamlit==1.36.0 transformers torch langchain langchain-community tiktoken accelerate
18 | ```
19 |
20 | #### Clone the repo into your project directory
21 | ```
22 | git clone https://github.com/fabiomatricardi/77M-chatbot-is-reality.git .
23 | ```
24 |
25 | - From the terminal, with the venv activated run
26 | ```
27 | streamlit run st-laminiChat.py
28 | ```
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/st-laminiChat.py:
--------------------------------------------------------------------------------
1 | ########################################################################
2 | # https://huggingface.co/nicholasKluge/Aira-2-355M #
3 | # https://huggingface.co/Felladrin/gguf-Aira-2-355M/tree/main #
4 | ########################################################################
5 | import streamlit as st
6 | import datetime
7 | from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
8 | import torch
9 | import datetime
10 | from rich.markdown import Markdown
11 | import warnings
12 | warnings.filterwarnings(action='ignore')
13 | import datetime
14 | from rich.console import Console
15 | console = Console(width=90)
16 | import tiktoken
17 | import random
18 | import string
19 | from time import sleep
20 |
21 | encoding = tiktoken.get_encoding("r50k_base") #context_count = len(encoding.encode(yourtext))
22 |
23 | #AVATARS 👷🐦 🥶🌀
24 | av_us = 'man.png' #"🦖" #A single emoji, e.g. "🧑💻", "🤖", "🦖". Shortcodes are not supported.
25 | av_ass = 'ass.png'
26 |
27 | # Set the webpage title
28 | st.set_page_config(
29 | page_title="Your LocalGPT with LaMini-Flan-T5-77M",
30 | page_icon="🦙",
31 | layout="wide")
32 |
33 | convHistory = ''
34 | #modelfile = "MBZUAI/LaMini-Flan-T5-248M"
35 | repetitionpenalty = 1.3
36 | contextlength=512
37 | logfile = 'LaMini77M_logs.txt'
38 |
39 |
40 | @st.cache_resource
41 | def create_chat():
42 | LaMini = './model77M/'
43 | tokenizer = AutoTokenizer.from_pretrained(LaMini)
44 | model = AutoModelForSeq2SeqLM.from_pretrained(LaMini,
45 | device_map='cpu',
46 | torch_dtype=torch.float32)
47 | llm = pipeline('text2text-generation',
48 | model = model,
49 | tokenizer = tokenizer,
50 | max_length = 512,
51 | do_sample=True,
52 | temperature=0.35,
53 | top_p=0.8,
54 | repetition_penalty = 1.3,
55 | top_k = 4,
56 | penalty_alpha = 0.6
57 | )
58 | return llm
59 |
60 | def writehistory(filename,text):
61 | with open(filename, 'a', encoding='utf-8') as f:
62 | f.write(text)
63 | f.write('\n')
64 | f.close()
65 |
66 | def genRANstring(n):
67 | """
68 | n = int number of char to randomize
69 | """
70 | N = n
71 | res = ''.join(random.choices(string.ascii_uppercase +
72 | string.digits, k=N))
73 | return res
74 |
75 |
76 | # Create a header element
77 | st.image('logo77m.png')
78 |
79 | # create THE SESSIoN STATES
80 | if "logfilename" not in st.session_state:
81 | ## Logger file
82 | logfile = f'{genRANstring(5)}_log.txt'
83 | st.session_state.logfilename = logfile
84 | #Write in the history the first 2 sessions
85 | writehistory(st.session_state.logfilename,f'{str(datetime.datetime.now())}\n\nYour own LocalGPT with 🦙 LaMini-77M\n---\n🧠🫡: You are a helpful assistant.')
86 | writehistory(st.session_state.logfilename,f'🌀: How may I help you today?')
87 |
88 | if "limiter" not in st.session_state:
89 | st.session_state.limiter = 0
90 |
91 | if "numoftokens" not in st.session_state:
92 | st.session_state.numoftokens = 0
93 |
94 | if "bufstatus" not in st.session_state:
95 | st.session_state.bufstatus = "**:green[Good]**"
96 |
97 | if "prompt" not in st.session_state:
98 | st.session_state.prompt = ''
99 |
100 | if "maxlength" not in st.session_state:
101 | st.session_state.maxlength = 350
102 |
103 | # Point to the local server
104 | llm = create_chat()
105 |
106 | # CREATE THE SIDEBAR
107 | with st.sidebar:
108 | st.image('assistant.png', width=200)
109 | st.session_state.maxlength = st.slider('Max prompt:', min_value=100, max_value=400, value=350, step=10)
110 | n_tokens = st.markdown(f"Prompt Tokens: {st.session_state.numoftokens}")
111 | st.markdown(f"Buffer status: {st.session_state.bufstatus}")
112 | st.markdown(f"**Logfile**: {st.session_state.logfilename}")
113 | btnClear = st.button("Clear History",type="primary", use_container_width=True)
114 |
115 | # We store the conversation in the session state.
116 | # This will be used to render the chat conversation.
117 | # We initialize it with the first message we want to be greeted with.
118 | if "messages" not in st.session_state:
119 | st.session_state.messages = [
120 | {"role": "system", "content": "You are LaMini-Flan-T5, a helpful assistant. You reply only to the user questions. You always reply in the language of the instructions.",},
121 | {"role": "user", "content": "Hi, I am Fabio."},
122 | {"role": "assistant", "content": "Hi there Fabio, I am LaMini-Flan-T5: with my 77M parameters I can be useful to you. how may I help you today?"}
123 | ]
124 |
125 | def clearHistory():
126 | st.session_state.messages = [
127 | {"role": "system", "content": "You are LaMini-Flan-T5, a helpful assistant. You reply only to the user questions. You always reply in the language of the instructions.",},
128 | {"role": "user", "content": "Hi, I am Fabio."},
129 | {"role": "assistant", "content": "Hi there Fabio, I am LaMini-Flan-T5: with my 77M parameters I can be useful to you. how may I help you today?"}
130 | ]
131 | st.session_state.len_context = len(st.session_state.messages)
132 | if btnClear:
133 | clearHistory()
134 | st.session_state.len_context = len(st.session_state.messages)
135 |
136 | # We loop through each message in the session state and render it as
137 | # a chat message.
138 | for message in st.session_state.messages[1:]:
139 | if message["role"] == "user":
140 | with st.chat_message(message["role"],avatar=av_us):
141 | st.markdown(message["content"])
142 | else:
143 | with st.chat_message(message["role"],avatar=av_ass):
144 | st.markdown(message["content"])
145 |
146 |
147 | def countTokens():
148 | encoding = tiktoken.get_encoding("r50k_base") #context_count = len(encoding.encode(yourtext))
149 | st.session_state.numoftokens = len(encoding.encode(st.session_state.prompt))
150 | print(st.session_state.numoftokens)
151 | if st.session_state.numoftokens > st.session_state.maxlength:
152 | n_tokens.markdown(f"**⚠️ Prompt Tokens: {st.session_state.numoftokens}**")
153 | return False
154 | else:
155 | n_tokens.markdown(f"**✅ Prompt Tokens: {st.session_state.numoftokens}**")
156 | return True
157 |
158 | # We take questions/instructions from the chat input to pass to the LLM
159 | if user_prompt := st.chat_input("Your message here. Shift+Enter to add a new line", key="user_input"):
160 | st.session_state.prompt = user_prompt
161 | if countTokens():
162 | # Add our input to the session state
163 | st.session_state.messages.append(
164 | {"role": "user", "content": user_prompt}
165 | )
166 |
167 | # Add our input to the chat window
168 | with st.chat_message("user", avatar=av_us):
169 | st.markdown(user_prompt)
170 | writehistory(st.session_state.logfilename,f'👷: {user_prompt}')
171 |
172 |
173 | with st.chat_message("assistant",avatar=av_ass):
174 | message_placeholder = st.empty()
175 | with st.spinner("Thinking..."):
176 | response = ''
177 | conv_messages = []
178 | st.session_state.len_context = len(st.session_state.messages)
179 | st.session_state.bufstatus = "**:green[Good]**"
180 | full_response = ""
181 | completion = llm(user_prompt)[0]['generated_text']
182 | for chunk in completion:
183 | full_response += chunk
184 | sleep(0.012)
185 | message_placeholder.markdown(full_response + "🌟")
186 | message_placeholder.markdown(full_response)
187 | writehistory(st.session_state.logfilename,f'🌟: {full_response}\n\n---\n\n')
188 | else:
189 | # Add our input to the session state
190 | st.session_state.messages.append(
191 | {"role": "user", "content": user_prompt}
192 | )
193 |
194 | # Add our input to the chat window
195 | with st.chat_message("user", avatar=av_us):
196 | st.markdown(user_prompt)
197 | writehistory(st.session_state.logfilename,f'👷: {user_prompt}')
198 | with st.chat_message("assistant",avatar=av_ass):
199 | message_placeholder = st.empty()
200 | st.session_state.len_context = len(st.session_state.messages)
201 | st.session_state.bufstatus = "**:red[BAD]**"
202 | full_response = ""
203 | completion = "⚠️ Your prompt is too long for me. Shorten it, please."
204 | for chunk in completion:
205 | full_response += chunk
206 | sleep(0.012)
207 | message_placeholder.markdown(full_response + "🌟")
208 | message_placeholder.markdown(full_response)
209 | writehistory(st.session_state.logfilename,f'🌟: {full_response}\n\n---\n\n')
210 |
211 | # Add the response to the session state
212 | st.session_state.messages.append(
213 | {"role": "assistant", "content": full_response}
214 | )
215 | st.session_state.len_context = len(st.session_state.messages)
--------------------------------------------------------------------------------