Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines3456789101112131415162 unmodified lines192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778792 unmodified linesfrom threading import Threadimport torch# Clean, centered layout without sidebarst.set_page_config(page_title="Qwen 3 4B Stream", page_icon="⚡", layout="centered", initial_sidebar_state="collapsed")# 1. Model Configuration (Qwen 3 4B - 4-bit for speed)MODEL_ID = "unsloth/Qwen3-4B-Instruct-2507-bnb-4bit"@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()# Custom CSS to hide the sidebar togglest.markdown("<style>[data-testid='collapsedControl'] { display: none; }</style>", unsafe_allow_html=True)st.title("⚡ Qwen 3 4B Stream")st.caption("Real-time local generation | No Sidebar")if "messages" not in st.session_state:st.session_state.messages = []# Action Buttonscol1, col2 = st.columns([5, 1])with col2:if st.button("Reset"):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. Streaming Chat Inputif prompt := st.chat_input("Ask Qwen 3..."):st.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)with st.chat_message("assistant"):# Setup the Streamerstreamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)# Prepare inputinput_text = tokenizer.apply_chat_template(st.session_state.messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer([input_text], return_tensors="pt").to(model.device)# 3. Generation in a separate threadgeneration_kwargs = dict(**inputs,streamer=streamer,max_new_tokens=1024,do_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()# 4. Stream to UIplaceholder = st.empty()full_response = ""for new_text in streamer:full_response += new_textplaceholder.markdown(full_response + "▌")2 unmodified lines345678910111213141516172 unmodified lines2021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475762 unmodified linesfrom threading import Threadimport torch# UI Setup - No Sidebarst.set_page_config(page_title="Gemma 3 1B Fast Chat", page_icon="💎", layout="centered", initial_sidebar_state="collapsed")# 1. Model ID for Gemma 3 1B InstructMODEL_ID = "google/gemma-3-1b-it"@st.cache_resourcedef load_model():# Gemma 3 1B is small enough to load in bfloat16 or float32 quicklytokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained(MODEL_ID,2 unmodified lines)return tokenizer, modeltokenizer, model = load_model()# Custom CSS to keep the clean lookst.markdown("<style>[data-testid='collapsedControl'] { display: none; }</style>", unsafe_allow_html=True)st.title("💎 Gemma 3 1B")st.caption("Lightweight Google AI | High-Speed Local Chat")if "messages" not in st.session_state:st.session_state.messages = []# Action Buttonif st.button("Clear Chat History"):st.session_state.messages = []st.rerun()# Display historyfor msg in st.session_state.messages:with st.chat_message(msg["role"]):st.markdown(msg["content"])# 2. Streaming Chat Inputif prompt := st.chat_input("Message Gemma 3..."):st.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)with st.chat_message("assistant"):# Setup Streamerstreamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)# Gemma 3 uses a specific chat template formatinput_text = tokenizer.apply_chat_template(st.session_state.messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer([input_text], return_tensors="pt").to(model.device)# Threaded generation for real-time streaminggeneration_kwargs = dict(**inputs,streamer=streamer,max_new_tokens=1024,do_sample=True,temperature=0.7,top_p=0.9,)thread = Thread(target=model.generate, kwargs=generation_kwargs)thread.start()# Update UI word-by-wordplaceholder = st.empty()full_response = ""for new_text in streamer:full_response += new_textplaceholder.markdown(full_response + "▌")
app.py+21-24