Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391 unmodified linefrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Threadimport torchimport time# 1. Advanced CSS: Branding, Animations, and Split-Pane Artifactsst.set_page_config(page_title="Claude", page_icon="☁️", layout="wide")# Claude's exact 2026 color paletteCLAUDE_ORANGE = "#d97757"CLAUDE_PAPER = "#f9f9f8"st.markdown(f"""<style>@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');.stApp {{ background-color: #ffffff; font-family: 'Inter', sans-serif; }}/* Sidebar: warm paper texture */[data-testid="stSidebar"] {{background-color: {CLAUDE_PAPER} !important;border-right: 1px solid #e5e5e5 !important;}}/* The "Living Avatar" Animation - Moving Lines Effect */@keyframes breathe {{0% {{ transform: scale(1); opacity: 0.8; }}50% {{ transform: scale(1.05); opacity: 1; }}100% {{ transform: scale(1); opacity: 0.8; }}}}[data-testid="chatAvatarAssistant"] {{background-color: {CLAUDE_ORANGE} !important;border-radius: 8px !important;animation: breathe 3s infinite ease-in-out;box-shadow: 0 0 15px rgba(217, 119, 87, 0.2);}}/* Artifacts Window Styling */.artifact-container {{background-color: #fcfcfb;border: 1px solid #e5e5e5;border-radius: 12px;padding: 20px;height: 80vh;overflow-y: auto;box-shadow: inset 0 0 10px rgba(0,0,0,0.02);}}/* Floating Input Bar */.stChatInputContainer {{border: 1px solid #d1d1d1 !important;border-radius: 16px !important;box-shadow: 0 8px 32px rgba(0,0,0,0.06) !important;max-width: 800px !important;margin: 0 auto 20px auto !important;}}header, footer {{ visibility: hidden; }}</style>""", unsafe_allow_html=True)# 2. Model Initialization@st.cache_resourcedef load_llm():model_id = "Qwen/Qwen2.5-3B-Instruct" # Highest 2026 performance/efficiency ratiotokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")return model, tokenizermodel, tokenizer = load_llm()# 3. Sidebar: Context & Settingswith st.sidebar:st.markdown(f"<h2 style='color: #1a1a1a; font-size: 1.25rem;'>Claude 3.5 Sonnet</h2>", unsafe_allow_html=True)if st.button("+ Start New Chat", use_container_width=True):st.session_state.messages = []st.session_state.artifact_content = ""st.rerun()st.divider()st.caption("CAPABILITIES")show_artifacts = st.toggle("Artifacts (Preview)", value=True)st.caption("Recent Artifacts")if st.session_state.get("artifact_content"):st.info("📄 current_code_snippet.py")# 4. Layout Definition (Chat vs Artifacts)if show_artifacts and st.session_state.get("artifact_content"):col_chat, col_art = st.columns([1, 1], gap="large")else:col_chat = st.container()col_art = None# 5. Chat Logicif "messages" not in st.session_state: st.session_state.messages = []if "artifact_content" not in st.session_state: st.session_state.artifact_content = ""with col_chat:# Landing Viewif not st.session_state.messages:st.markdown("<div style='height: 10vh;'></div>", unsafe_allow_html=True)st.markdown("<h1 style='text-align: center; font-weight: 500;'>How can I help you today?</h1>", unsafe_allow_html=True)for m in st.session_state.messages:with st.chat_message(m["role"]):st.markdown(m["content"])if prompt := st.chat_input("Ask Claude..."):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)msgs = [{"role": "system", "content": "You are Claude. If the user asks for code, provide it clearly."}] + st.session_state.messages[-6:]inputs = tokenizer.apply_chat_template(msgs, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)thread = Thread(target=model.generate, kwargs=dict(input_ids=inputs, streamer=streamer, max_new_tokens=1024))thread.start()full_response = st.write_stream(streamer)# Detect if response contains code to trigger "Artifact"if "```" in full_response:code_content = full_response.split("```")[1]st.session_state.artifact_content = code_contentst.rerun()st.session_state.messages.append({"role": "assistant", "content": full_response})# 6. Artifact Display (Right Panel)if col_art and st.session_state.artifact_content:with col_art:st.markdown("<p style='font-size: 0.8rem; color: #6e6e6e; margin-bottom: 5px;'>ARTIFACT</p>", unsafe_allow_html=True)with st.container(border=True):st.code(st.session_state.artifact_content, language="python")if st.button("Copy to Clipboard"):st.toast("Code copied!")1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889901 unmodified linefrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Threadimport torch# App Configurationst.set_page_config(page_title="Qwen3 Turbo", page_icon="⚡")# 1. Load Qwen3 (Cached for efficiency)@st.cache_resourcedef load_qwen3():model_id = "Qwen/Qwen3-1.7B-Instruct"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype="auto",device_map="auto")return tokenizer, modeltokenizer, model = load_qwen3()# 2. System Prompt Selection (Main UI, No Sidebar)st.title("⚡ Qwen3-1.7B Local Chat")system_options = {"General Assistant": "You are a helpful and concise assistant.","Python Expert": "You are an expert Python developer. Provide clean, efficient code with brief explanations.","Creative Storyteller": "You are a creative writer. Use vivid imagery and engaging narrative styles."}# Horizontal layout for the selector and a clear buttoncol1, col2 = st.columns([3, 1])with col1:selected_role = st.selectbox("Choose AI Personality:", list(system_options.keys()))with col2:if st.button("Clear History", use_container_width=True):st.session_state.messages = []st.rerun()system_prompt = system_options[selected_role]# 3. Setup Session Stateif "messages" not in st.session_state:st.session_state.messages = []# Display Chat Historyfor message in st.session_state.messages:if message["role"] != "system": # Hide system prompt from UIwith st.chat_message(message["role"]):st.markdown(message["content"])# 4. Chat Input & Streamingif prompt := st.chat_input("Message Qwen3..."):# Insert system prompt if history is emptyif len(st.session_state.messages) == 0:st.session_state.messages.append({"role": "system", "content": system_prompt})# Add User Messagest.session_state.messages.append({"role": "user", "content": prompt})with st.chat_message("user"):st.markdown(prompt)# Generate Assistant Responsewith st.chat_message("assistant"):# Prepare input with chat templateinput_ids = tokenizer.apply_chat_template(st.session_state.messages,tokenize=True,add_generation_prompt=True,return_tensors="pt").to(model.device)streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)generation_kwargs = dict(input_ids=input_ids,streamer=streamer,max_new_tokens=1024,temperature=0.7)# Threaded generation for real-time streamingthread = Thread(target=model.generate, kwargs=generation_kwargs)thread.start()# Stream the outputfull_response = st.write_stream(streamer)st.session_state.messages.append({"role": "assistant", "content": full_response})
app.py+76-125