Eiad Gomaa
commited on
Commit
·
335e8ff
1
Parent(s):
5d8d025
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,96 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
|
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
st.session_state.history = []
|
10 |
-
|
11 |
-
# Set up the Streamlit interface
|
12 |
st.title("Chat with Quasar-32B")
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
# Add user input to chat history
|
20 |
-
st.session_state.history.append({"role": "user", "content": user_input})
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Display chat history
|
29 |
-
|
30 |
-
for
|
31 |
-
|
32 |
-
st.
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
@st.cache_resource
|
6 |
+
def load_model():
|
7 |
+
"""Load model and tokenizer with caching"""
|
8 |
+
try:
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("eyad-silx/Quasar-32B")
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("eyad-silx/Quasar-32B")
|
11 |
+
return model, tokenizer
|
12 |
+
except Exception as e:
|
13 |
+
st.error(f"Error loading model: {str(e)}")
|
14 |
+
return None, None
|
15 |
|
16 |
+
# Page config
|
17 |
+
st.set_page_config(page_title="Chat with Quasar-32B", layout="wide")
|
|
|
|
|
|
|
18 |
st.title("Chat with Quasar-32B")
|
19 |
|
20 |
+
# Initialize session state for chat history
|
21 |
+
if 'messages' not in st.session_state:
|
22 |
+
st.session_state.messages = []
|
23 |
|
24 |
+
# Load model and tokenizer
|
25 |
+
model, tokenizer = load_model()
|
|
|
|
|
26 |
|
27 |
+
# Chat interface
|
28 |
+
def generate_response(prompt):
|
29 |
+
"""Generate response from the model"""
|
30 |
+
try:
|
31 |
+
# Prepare the input
|
32 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True)
|
33 |
+
|
34 |
+
# Generate response
|
35 |
+
with torch.no_grad():
|
36 |
+
outputs = model.generate(
|
37 |
+
inputs["input_ids"],
|
38 |
+
max_length=200,
|
39 |
+
num_return_sequences=1,
|
40 |
+
temperature=0.7,
|
41 |
+
pad_token_id=tokenizer.eos_token_id
|
42 |
+
)
|
43 |
+
|
44 |
+
# Decode and return the response
|
45 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
46 |
+
return response
|
47 |
+
except Exception as e:
|
48 |
+
return f"Error generating response: {str(e)}"
|
49 |
+
|
50 |
+
# Chat interface
|
51 |
+
st.write("### Chat")
|
52 |
+
chat_container = st.container()
|
53 |
|
54 |
# Display chat history
|
55 |
+
with chat_container:
|
56 |
+
for message in st.session_state.messages:
|
57 |
+
with st.chat_message(message["role"]):
|
58 |
+
st.write(message["content"])
|
59 |
+
|
60 |
+
# User input
|
61 |
+
if prompt := st.chat_input("Type your message here"):
|
62 |
+
# Add user message to chat history
|
63 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
64 |
+
|
65 |
+
# Display user message
|
66 |
+
with chat_container:
|
67 |
+
with st.chat_message("user"):
|
68 |
+
st.write(prompt)
|
69 |
+
|
70 |
+
# Generate and display assistant response
|
71 |
+
if model and tokenizer:
|
72 |
+
with st.chat_message("assistant"):
|
73 |
+
with st.spinner("Thinking..."):
|
74 |
+
response = generate_response(prompt)
|
75 |
+
st.write(response)
|
76 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
77 |
+
else:
|
78 |
+
st.error("Model failed to load. Please check your configuration.")
|
79 |
+
|
80 |
+
# Add a button to clear chat history
|
81 |
+
if st.button("Clear Chat History"):
|
82 |
+
st.session_state.messages = []
|
83 |
+
st.experimental_rerun()
|
84 |
+
|
85 |
+
# Display system information
|
86 |
+
with st.sidebar:
|
87 |
+
st.write("### System Information")
|
88 |
+
st.write("Model: Quasar-32B")
|
89 |
+
st.write("Status: Running" if model and tokenizer else "Status: Not loaded")
|
90 |
+
|
91 |
+
# Add some helpful instructions
|
92 |
+
st.write("### Instructions")
|
93 |
+
st.write("1. Type your message in the chat input")
|
94 |
+
st.write("2. Press Enter or click Send")
|
95 |
+
st.write("3. Wait for the AI to respond")
|
96 |
+
st.write("4. Use 'Clear Chat History' to start fresh")
|