Spaces:
Build error
Build error
Update app1.py
Browse files
app1.py
CHANGED
|
@@ -1,63 +1,99 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
| 3 |
-
from nltk.tokenize import sent_tokenize
|
| 4 |
import nltk
|
| 5 |
|
|
|
|
| 6 |
nltk.download('punkt')
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
output = tokenizer.decode(response_ids[0], skip_special_tokens=True)
|
| 17 |
return output
|
| 18 |
|
| 19 |
-
|
| 20 |
def format_messages_for_display(messages):
|
|
|
|
| 21 |
formatted_text = []
|
| 22 |
for message in messages:
|
| 23 |
if message["role"] == "assistant":
|
| 24 |
-
formatted_text.append(f"Assistant: {message['content']}")
|
| 25 |
else:
|
| 26 |
-
formatted_text.append(f"User: {message['content']}")
|
| 27 |
-
return "\n".join(formatted_text)
|
| 28 |
-
|
| 29 |
|
|
|
|
| 30 |
def main():
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
with st.form(key='input_form'):
|
| 37 |
-
user_input = st.text_area("Enter your prompt:")
|
| 38 |
-
submitted = st.form_submit_button(label="Submit")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
{
|
| 43 |
-
"role": "user",
|
| 44 |
-
"content": user_input
|
| 45 |
-
}
|
| 46 |
-
]
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == '__main__':
|
| 63 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
|
| 3 |
import nltk
|
| 4 |
|
| 5 |
+
# Download the necessary NLTK data
|
| 6 |
nltk.download('punkt')
|
| 7 |
|
| 8 |
+
# Constants
|
| 9 |
+
MODEL_NAME = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
| 10 |
+
MAX_LENGTH = 512
|
| 11 |
+
RESPONSE_MAX_LENGTH = 50
|
| 12 |
+
RESPONSE_MIN_LENGTH = 20
|
| 13 |
+
LENGTH_PENALTY = 1.0
|
| 14 |
+
NUM_BEAMS = 2
|
| 15 |
+
NO_REPEAT_NGRAM_SIZE = 2
|
| 16 |
+
TEMPERATURE = 0.9
|
| 17 |
+
TOP_K = 30
|
| 18 |
+
TOP_P = 0.85
|
| 19 |
+
|
| 20 |
+
# Load Pre-Trained Model and Tokenizer
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_model():
|
| 23 |
+
"""Load the pre-trained model and tokenizer"""
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 25 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 26 |
+
return tokenizer, model
|
| 27 |
+
|
| 28 |
+
# Function to generate a response using the model
|
| 29 |
+
def generate_response(text, tokenizer, model):
|
| 30 |
+
"""Generate a response using the model"""
|
| 31 |
+
input_ids = tokenizer.encode(text, return_tensors="pt", max_length=MAX_LENGTH, truncation=True)
|
| 32 |
+
response_ids = model.generate(
|
| 33 |
+
input_ids=input_ids,
|
| 34 |
+
max_length=RESPONSE_MAX_LENGTH,
|
| 35 |
+
min_length=RESPONSE_MIN_LENGTH,
|
| 36 |
+
length_penalty=LENGTH_PENALTY,
|
| 37 |
+
num_beams=NUM_BEAMS,
|
| 38 |
+
no_repeat_ngram_size=NO_REPEAT_NGRAM_SIZE,
|
| 39 |
+
temperature=TEMPERATURE,
|
| 40 |
+
top_k=TOP_K,
|
| 41 |
+
top_p=TOP_P,
|
| 42 |
+
do_sample=True
|
| 43 |
+
)
|
| 44 |
output = tokenizer.decode(response_ids[0], skip_special_tokens=True)
|
| 45 |
return output
|
| 46 |
|
| 47 |
+
# Function to format messages for display
|
| 48 |
def format_messages_for_display(messages):
|
| 49 |
+
"""Format messages for display"""
|
| 50 |
formatted_text = []
|
| 51 |
for message in messages:
|
| 52 |
if message["role"] == "assistant":
|
| 53 |
+
formatted_text.append(f"**Assistant**: {message['content']}")
|
| 54 |
else:
|
| 55 |
+
formatted_text.append(f"**User**: {message['content']}")
|
| 56 |
+
return "\n\n".join(formatted_text)
|
|
|
|
| 57 |
|
| 58 |
+
# Main function to run the Streamlit app
|
| 59 |
def main():
|
| 60 |
+
"""Run the Streamlit app"""
|
| 61 |
+
st.set_page_config(page_title="LLaMA Chat Interface", page_icon="", layout="wide")
|
| 62 |
|
| 63 |
+
st.title("LLaMA Chat Interface")
|
| 64 |
+
st.write("This is a chat interface using the LLaMA model for generating responses. Enter a prompt below to start chatting with the model.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Load the model and tokenizer
|
| 67 |
+
tokenizer, model = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
if'messages' not in st.session_state:
|
| 70 |
+
st.session_state['messages'] = []
|
| 71 |
|
| 72 |
+
# Display chat messages
|
| 73 |
+
chat_placeholder = st.empty()
|
| 74 |
+
with chat_placeholder.container():
|
| 75 |
+
st.markdown(format_messages_for_display(st.session_state['messages']))
|
| 76 |
|
| 77 |
+
# Add text input and send button
|
| 78 |
+
user_input = st.text_input("Enter your prompt:", key="user_input")
|
| 79 |
+
if st.button("Send") and user_input.strip():
|
| 80 |
+
# Store user's message
|
| 81 |
+
st.session_state['messages'].append({"role": "user", "content": user_input})
|
| 82 |
|
| 83 |
+
# Generate and store the assistant's response
|
| 84 |
+
with st.spinner("Generating response..."):
|
| 85 |
+
response = generate_response(user_input, tokenizer, model)
|
| 86 |
+
st.session_state['messages'].append({"role": "assistant", "content": response})
|
| 87 |
|
| 88 |
+
# Update chat display
|
| 89 |
+
with chat_placeholder.container():
|
| 90 |
+
st.markdown(format_messages_for_display(st.session_state['messages']))
|
| 91 |
|
| 92 |
+
# Option to clear the chat history
|
| 93 |
+
if st.button("Clear Chat"):
|
| 94 |
+
st.session_state['messages'] = []
|
| 95 |
+
with chat_placeholder.container():
|
| 96 |
+
st.markdown("")
|
| 97 |
|
| 98 |
if __name__ == '__main__':
|
| 99 |
+
main()
|