Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,75 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer,
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# 1. Page Configuration (
|
| 6 |
st.set_page_config(page_title="Claude Clone", page_icon="🤖", layout="centered")
|
| 7 |
|
| 8 |
-
# Custom CSS to force-hide the sidebar button and clean up the UI
|
| 9 |
st.markdown("""
|
| 10 |
<style>
|
| 11 |
[data-testid="stSidebar"] {display: none;}
|
| 12 |
-
[data-testid="stHeader"] {background: rgba(0,0,0,0);}
|
| 13 |
.stChatMessage {border-radius: 15px; padding: 10px; margin-bottom: 10px;}
|
| 14 |
</style>
|
| 15 |
""", unsafe_allow_html=True)
|
| 16 |
|
| 17 |
-
st.title("Qwen 2.5 Coder
|
| 18 |
-
st.caption("
|
| 19 |
|
| 20 |
-
# 2.
|
| 21 |
@st.cache_resource
|
| 22 |
def load_model():
|
| 23 |
model_id = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 25 |
-
|
|
|
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
model_id,
|
| 28 |
-
torch_dtype=
|
| 29 |
device_map="auto"
|
| 30 |
)
|
| 31 |
-
return
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
# 3.
|
| 36 |
if "messages" not in st.session_state:
|
| 37 |
-
st.session_state.messages = [
|
| 38 |
-
{"role": "system", "content": "You are a helpful assistant named Claude-Clone. You excel at coding and technical tasks."}
|
| 39 |
-
]
|
| 40 |
|
| 41 |
-
# Display
|
| 42 |
for message in st.session_state.messages:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
st.markdown(message["content"])
|
| 46 |
|
| 47 |
-
# 4. Chat Input & Logic
|
| 48 |
-
if prompt := st.chat_input("
|
| 49 |
-
# User Message
|
| 50 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 51 |
with st.chat_message("user"):
|
| 52 |
st.markdown(prompt)
|
| 53 |
|
| 54 |
-
# Assistant Response
|
| 55 |
with st.chat_message("assistant"):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 3 |
+
from threading import Thread
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# 1. Page Configuration (No Sidebar)
|
| 7 |
st.set_page_config(page_title="Claude Clone", page_icon="🤖", layout="centered")
|
| 8 |
|
|
|
|
| 9 |
st.markdown("""
|
| 10 |
<style>
|
| 11 |
[data-testid="stSidebar"] {display: none;}
|
|
|
|
| 12 |
.stChatMessage {border-radius: 15px; padding: 10px; margin-bottom: 10px;}
|
| 13 |
</style>
|
| 14 |
""", unsafe_allow_html=True)
|
| 15 |
|
| 16 |
+
st.title("Qwen 2.5 Coder 1.5B 🚀")
|
| 17 |
+
st.caption("Now with real-time streaming and optimized CPU inference.")
|
| 18 |
|
| 19 |
+
# 2. Optimized Model Loading
|
| 20 |
@st.cache_resource
|
| 21 |
def load_model():
|
| 22 |
model_id = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 24 |
+
|
| 25 |
+
# Use bfloat16 for speed on modern CPUs, or float32 for maximum compatibility
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
model_id,
|
| 28 |
+
torch_dtype=torch.float32, # CPU-friendly
|
| 29 |
device_map="auto"
|
| 30 |
)
|
| 31 |
+
return model, tokenizer
|
| 32 |
|
| 33 |
+
model, tokenizer = load_model()
|
| 34 |
|
| 35 |
+
# 3. Session State
|
| 36 |
if "messages" not in st.session_state:
|
| 37 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Display History
|
| 40 |
for message in st.session_state.messages:
|
| 41 |
+
with st.chat_message(message["role"]):
|
| 42 |
+
st.markdown(message["content"])
|
|
|
|
| 43 |
|
| 44 |
+
# 4. Chat Input & Streaming Logic
|
| 45 |
+
if prompt := st.chat_input("Ask me anything..."):
|
|
|
|
| 46 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 47 |
with st.chat_message("user"):
|
| 48 |
st.markdown(prompt)
|
| 49 |
|
|
|
|
| 50 |
with st.chat_message("assistant"):
|
| 51 |
+
# Set up the streamer
|
| 52 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 53 |
+
|
| 54 |
+
# Prepare the input
|
| 55 |
+
messages = [{"role": "system", "content": "You are a helpful coding assistant."}] + st.session_state.messages
|
| 56 |
+
inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 57 |
+
|
| 58 |
+
# Run generation in a separate thread to allow UI to remain responsive
|
| 59 |
+
generation_kwargs = dict(
|
| 60 |
+
input_ids=inputs,
|
| 61 |
+
streamer=streamer,
|
| 62 |
+
max_new_tokens=512,
|
| 63 |
+
do_sample=True,
|
| 64 |
+
temperature=0.7,
|
| 65 |
+
top_p=0.9,
|
| 66 |
+
pad_token_id=tokenizer.eos_token_id
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 70 |
+
thread.start()
|
| 71 |
+
|
| 72 |
+
# Stream the response to the UI
|
| 73 |
+
full_response = st.write_stream(streamer)
|
| 74 |
+
|
| 75 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|