Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import streamlit as stfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipelineimport torch# 1. Page Configuration (Hide Sidebar & Set Theme)st.set_page_config(page_title="Claude Clone", page_icon="🤖", layout="centered")# Custom CSS to force-hide the sidebar button and clean up the UIst.markdown("""<style>[data-testid="stSidebar"] {display: none;}[data-testid="stHeader"] {background: rgba(0,0,0,0);}.stChatMessage {border-radius: 15px; padding: 10px; margin-bottom: 10px;}</style>""", unsafe_allow_html=True)st.title("Qwen 2.5 Coder 🤖")st.caption("A lightweight, powerful Claude-style clone powered by Alibaba's Qwen 2.5 1.5B")# 2. Load Model & Tokenizer@st.cache_resourcedef load_model():model_id = "Qwen/Qwen2.5-Coder-1.5B-Instruct"tokenizer = AutoTokenizer.from_pretrained(model_id)# Using torch_dtype="auto" to handle CPU/GPU environments automaticallymodel = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype="auto",device_map="auto")return pipeline("text-generation", model=model, tokenizer=tokenizer)generator = load_model()# 3. Initialize Chat Historyif "messages" not in st.session_state:st.session_state.messages = [{"role": "system", "content": "You are a helpful assistant named Claude-Clone. You excel at coding and technical tasks."}]# Display Chat Historyfor message in st.session_state.messages:if message["role"] != "system":with st.chat_message(message["role"]):st.markdown(message["content"])# 4. Chat Input & Logicif prompt := st.chat_input("How can I help you today?"):# User Messagest.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)# Assistant Responsewith st.chat_message("assistant"):with st.spinner("Thinking..."):# Format history for the modelfull_prompt = st.session_state.messages# Generate responseoutputs = generator(full_prompt,max_new_tokens=512,do_sample=True,temperature=0.7,top_p=0.9)response = outputs[0]['generated_text'][-1]['content']st.markdown(response)st.session_state.messages.append({"role": "assistant", "content": response})123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import streamlit as stfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Threadimport torch# 1. Page Configuration (No Sidebar)st.set_page_config(page_title="Claude Clone", page_icon="🤖", layout="centered")st.markdown("""<style>[data-testid="stSidebar"] {display: none;}.stChatMessage {border-radius: 15px; padding: 10px; margin-bottom: 10px;}</style>""", unsafe_allow_html=True)st.title("Qwen 2.5 Coder 1.5B 🚀")st.caption("Now with real-time streaming and optimized CPU inference.")# 2. Optimized Model Loading@st.cache_resourcedef load_model():model_id = "Qwen/Qwen2.5-Coder-1.5B-Instruct"tokenizer = AutoTokenizer.from_pretrained(model_id)# Use bfloat16 for speed on modern CPUs, or float32 for maximum compatibilitymodel = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype=torch.float32, # CPU-friendlydevice_map="auto")return model, tokenizermodel, tokenizer = load_model()# 3. Session Stateif "messages" not in st.session_state:st.session_state.messages = []# Display Historyfor message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])# 4. Chat Input & Streaming Logicif prompt := st.chat_input("Ask me anything..."):st.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)with st.chat_message("assistant"):# Set up the streamerstreamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)# Prepare the inputmessages = [{"role": "system", "content": "You are a helpful coding assistant."}] + st.session_state.messagesinputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)# Run generation in a separate thread to allow UI to remain responsivegeneration_kwargs = dict(input_ids=inputs,streamer=streamer,max_new_tokens=512,do_sample=True,temperature=0.7,top_p=0.9,pad_token_id=tokenizer.eos_token_id)thread = Thread(target=model.generate, kwargs=generation_kwargs)thread.start()# Stream the response to the UIfull_response = st.write_stream(streamer)st.session_state.messages.append({"role": "assistant", "content": full_response})
app.py+43-40