Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines3456789101112131415162 unmodified lines1920212223242526272829303132333435363738394041424344454647484950515253545556575859606162631 unmodified line65666768697071727374757677782 unmodified linesfrom threading import Threadimport torch# 1. Page Config - No Sidebarst.set_page_config(page_title="Qwen 3 0.6B Instant", page_icon="⚡", layout="centered", initial_sidebar_state="collapsed")# Model ID: The 2026 ultra-lightweight versionMODEL_ID = "Qwen/Qwen3-0.6B-Instruct"@st.cache_resourcedef load_resource():tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained(MODEL_ID,2 unmodified lines)return tokenizer, modeltokenizer, model = load_resource()# Hide Sidebar Togglest.markdown("<style>[data-testid='collapsedControl'] { display: none; }</style>", unsafe_allow_html=True)st.title("⚡ Qwen 3 0.6B: Instant")st.caption("The fastest chat model of 2026 | Pure CPU Speed")if "messages" not in st.session_state:st.session_state.messages = []# Header button for clearingif st.button("Reset Chat"):st.session_state.messages = []st.rerun()# Display chat historyfor msg in st.session_state.messages:with st.chat_message(msg["role"]):st.markdown(msg["content"])# 2. Chat Input & Generationif 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"):streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)# Apply Qwen 3 template (disabling 'thinking' for maximum chat speed)input_text = tokenizer.apply_chat_template(st.session_state.messages,tokenize=False,add_generation_prompt=True,enable_thinking=False)inputs = tokenizer([input_text], return_tensors="pt").to(model.device)generation_kwargs = dict(**inputs,streamer=streamer,1 unmodified linedo_sample=True,temperature=0.7,top_p=0.8,)thread = Thread(target=model.generate, kwargs=generation_kwargs)thread.start()# Word-by-word streamingplaceholder = st.empty()full_response = ""for new_text in streamer:full_response += new_textplaceholder.markdown(full_response + "▌")2 unmodified lines3456789101112131415161718192021222324252 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364656667681 unmodified line707172737475767778798081828384852 unmodified linesfrom threading import Threadimport torch# 1. Page Configuration (Centered and No Sidebar)st.set_page_config(page_title="Qwen 3 0.6B Chat",page_icon="⚡",layout="centered",initial_sidebar_state="collapsed")# Custom CSS to hide the sidebar toggle button entirelyst.markdown("<style>[data-testid='collapsedControl'] { display: none; }</style>", unsafe_allow_html=True)# 2. Model & Tokenizer Initialization (Using your direct load logic)MODEL_ID = "Qwen/Qwen3-0.6B"@st.cache_resourcedef load_llm():# Loading the tokenizer and model directly as requestedtokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained(MODEL_ID,2 unmodified lines)return tokenizer, modeltokenizer, model = load_llm()# 3. Chat UI Logicst.title("⚡ Qwen 3 0.6B")st.caption("Using your direct-load logic with real-time streaming.")if "messages" not in st.session_state:st.session_state.messages = []# Display historyfor msg in st.session_state.messages:with st.chat_message(msg["role"]):st.markdown(msg["content"])# 4. Input & Streaming Generationif prompt := st.chat_input("Ask Qwen 3..."):# Store and display user messagest.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)with st.chat_message("assistant"):# Initialize the streamerstreamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)# Using your chat template logicinputs = tokenizer.apply_chat_template(st.session_state.messages,add_generation_prompt=True,tokenize=True,return_dict=True,return_tensors="pt",).to(model.device)# Background thread for generationgeneration_kwargs = dict(**inputs,streamer=streamer,1 unmodified linedo_sample=True,temperature=0.7,top_p=0.8,pad_token_id=tokenizer.eos_token_id)thread = Thread(target=model.generate, kwargs=generation_kwargs)thread.start()# Update the UI as tokens arriveplaceholder = st.empty()full_response = ""for new_text in streamer:full_response += new_textplaceholder.markdown(full_response + "▌")
app.py+34-27