Spaces:
Sleeping
Sleeping
Oscar Dilley
commited on
Commit
Β·
8aec0fe
1
Parent(s):
f6ffde9
version1
Browse files- .gitignore +4 -0
- app.py +68 -2
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
env/
|
| 3 |
+
.venv/
|
| 4 |
+
.env/
|
app.py
CHANGED
|
@@ -1,4 +1,70 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
+
# Streamlit setup
|
| 6 |
+
st.title("Telco Chat Bot")
|
| 7 |
+
st.subheader("Smart Internet Lab")
|
| 8 |
+
st.page_link("https://github.com/Ali-maatouk/Tele-LLMs", label="Tele-LLMs backend", icon="π±")
|
| 9 |
+
# Add text giving credit
|
| 10 |
+
col1, col2 = st.columns(2)
|
| 11 |
+
if 'conversation' not in st.session_state:
|
| 12 |
+
st.session_state.conversation = []
|
| 13 |
+
user_input = st.text_input("You:", "") # user input
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Model functions:
|
| 17 |
+
@st.cache_resource(show_spinner=False)
|
| 18 |
+
def load_model():
|
| 19 |
+
""" Load model from Hugging face."""
|
| 20 |
+
success_placeholder = st.empty()
|
| 21 |
+
with st.spinner("Loading model... please wait"):
|
| 22 |
+
model_name = "AliMaatouk/TinyLlama-1.1B-Tele" # Replace with the correct model name
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, torch_dtype="auto")
|
| 24 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 25 |
+
success_placeholder.success("Model loaded successfully!", icon="π₯")
|
| 26 |
+
time.sleep(2)
|
| 27 |
+
success_placeholder.empty()
|
| 28 |
+
return model, tokenizer
|
| 29 |
+
|
| 30 |
+
def generate_response(user_input):
|
| 31 |
+
""" Query the model. """
|
| 32 |
+
success_placeholder = st.empty()
|
| 33 |
+
with st.spinner("Thinking..."):
|
| 34 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 35 |
+
#outputs = model.generate(**inputs, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 36 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 37 |
+
generated_tokens = outputs[0, len(inputs['input_ids'][0]):]
|
| 38 |
+
success_placeholder.success("Response generated!", icon="β
")
|
| 39 |
+
time.sleep(2)
|
| 40 |
+
success_placeholder.empty()
|
| 41 |
+
return tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 42 |
+
|
| 43 |
+
# RUNTIME EVENTS:
|
| 44 |
+
|
| 45 |
+
# Load model and tokenizer
|
| 46 |
+
model, tokenizer = load_model()
|
| 47 |
+
|
| 48 |
+
# Submit button to send the query
|
| 49 |
+
with col1:
|
| 50 |
+
if st.button("send"):
|
| 51 |
+
if user_input:
|
| 52 |
+
st.session_state.conversation.append({"role": "user", "content": user_input})
|
| 53 |
+
# Querying model
|
| 54 |
+
# Add a loading spinner during model loading
|
| 55 |
+
response = generate_response(user_input)
|
| 56 |
+
# Display bot response
|
| 57 |
+
st.session_state.conversation.append({"role": "bot", "content": response})
|
| 58 |
+
|
| 59 |
+
# Clear button to reset
|
| 60 |
+
with col2:
|
| 61 |
+
if st.button("clear chat"):
|
| 62 |
+
if user_input:
|
| 63 |
+
st.session_state.conversation = []
|
| 64 |
+
|
| 65 |
+
# Display conversation history
|
| 66 |
+
for chat in st.session_state.conversation:
|
| 67 |
+
if chat['role'] == 'user':
|
| 68 |
+
st.write(f"You: {chat['content']}")
|
| 69 |
+
else:
|
| 70 |
+
st.write(f"Bot: {chat['content']}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|