Nikhil0987 commited on
Commit
4161eb3
·
verified ·
1 Parent(s): 829651d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -25
app.py CHANGED
@@ -1,32 +1,30 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
4
- # Title
5
- st.title("Text Generation ")
 
 
6
 
7
- # IMPORTANT: Model Loading Considerations
8
- # st.warning("Large Language Models (LLMs) like Llama 2-70b can be resource-intensive. This is a simplified demo. Loading the full model might exceed memory limits on standard machines.")
9
 
10
- # Function to load a small portion of the model (for demo purposes)
11
- @st.cache_resource
12
- def load_demo_model():
13
- tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-13b-hf")
14
- model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-13b-hf")
15
- # ... Add logic to load a tiny slice of the model ...
16
- return tokenizer, model
17
 
18
- # # Load model on demand
19
- # if st.button("Load Demo Model"):
20
- # with st.spinner("Loading model (this might still take some time)..."):
21
- #
22
- # st.success("Demo model loaded!")
23
 
24
- # Text input for prompt
25
- input_text = st.text_area("Enter a prompt for text generation:")
 
 
 
26
 
27
- # Button to trigger generation
28
- if st.button("Generate Text"):
29
- # ... Logic to use model.generate() with input_text ...
30
- generated_text = "..." #Replace with your generation code
31
- tokenizer, model = load_demo_model()
32
- st.write(generated_text)
 
 
 
1
  import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
+ # Your model and tokenizer definitions remain the same
5
+ model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
7
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
8
 
9
+ st.title("Chat with the Language Model")
 
10
 
11
+ # Area to display chat history
12
+ chat_history = st.empty()
 
 
 
 
 
13
 
14
+ # User input box
15
+ user_input = st.text_input("Your message:", key="input")
 
 
 
16
 
17
+ # Submit button
18
+ if st.button('Send'):
19
+ # Add user message to chat history
20
+ messages.append({"role": "user", "content": user_input})
21
+ chat_history.text("\n".join([f"**{msg['role']}**: {msg['content']}" for msg in messages]))
22
 
23
+ # Process the input with the model
24
+ inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
25
+ outputs = model.generate(inputs, max_new_tokens=20)
26
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
+
28
+ # Add model response to chat history
29
+ messages.append({"role": "assistant", "content": generated_text})
30
+ chat_history.text("\n".join([f"**{msg['role']}**: {msg['content']}" for msg in messages]))