Spaces:
Sleeping
Sleeping
Commit ·
7234470
0
Parent(s):
Initial Model with the Dexy Assistant
Browse files- .gitignore +1 -0
- localmodel.py +43 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
smollm
|
localmodel.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pip install transformers
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import streamlit as st
|
| 4 |
+
checkpoint = "HuggingFaceTB/SmolLM-135M-Instruct"
|
| 5 |
+
|
| 6 |
+
device = "cpu" # for GPU use "gpu" usage or "cpu" for CPU usage
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 8 |
+
# for multiple GPUs install accelerate and do `model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto")`
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
| 10 |
+
|
| 11 |
+
st.title("Dexy Chat Assistant")
|
| 12 |
+
|
| 13 |
+
# Initialize session state for chat history
|
| 14 |
+
if 'messages' not in st.session_state:
|
| 15 |
+
st.session_state.messages = []
|
| 16 |
+
|
| 17 |
+
# Text input for user
|
| 18 |
+
user_name = st.text_input("Your name please?: ", key="user_name")
|
| 19 |
+
user_input = st.text_input("Enter your message:", key="user_input")
|
| 20 |
+
|
| 21 |
+
if st.button("Send"):
|
| 22 |
+
if user_input:
|
| 23 |
+
# Add user message to history
|
| 24 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 25 |
+
|
| 26 |
+
# Process with model
|
| 27 |
+
input_text = tokenizer.apply_chat_template(st.session_state.messages, tokenize=False)
|
| 28 |
+
encoded = tokenizer(input_text, return_tensors="pt", padding=True)
|
| 29 |
+
inputs = encoded.input_ids.to(device)
|
| 30 |
+
attention_mask = encoded.attention_mask.to(device)
|
| 31 |
+
outputs = model.generate(inputs, attention_mask=attention_mask, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
|
| 32 |
+
response = tokenizer.decode(outputs[0])
|
| 33 |
+
|
| 34 |
+
# Add assistant's response to history
|
| 35 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 36 |
+
|
| 37 |
+
# Display full chat history
|
| 38 |
+
for msg in st.session_state.messages:
|
| 39 |
+
if msg["role"] == "user":
|
| 40 |
+
st.write(f"{user_name}: {msg['content']}")
|
| 41 |
+
else:
|
| 42 |
+
# st.write(f"Dexy: {msg['content']}")
|
| 43 |
+
st.write(f"Dexy: {msg['content'].split('<|im_start|>assistant')[-1].split('<|im_end|>')[0]}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
streamlit
|