Spaces:
Runtime error
Runtime error
Update app.py
Browse files12323456785678910101112131415161718192021222324252627282930313212131415161718192021222324253435262737282930403132333435364344454647373839404142495051434445534647485758596061626364654950515253545556575859606168696263646566677172686970import streamlit as stfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipelineimport torchfrom llama_cpp import Llamaimport os
# 1. Page Configurationst.set_page_config(page_title="QwenCoder-Mini", page_icon="💻")st.title("💻 Qwen2.5 Coder: Mini-Claude")st.markdown("Running on **Qwen2.5-Coder-3B-Instruct** (CPU Optimized)")# 1. Page Configst.set_page_config(page_title="Qwen Coder GGUF", page_icon="🤖", layout="wide")st.title("🚀 Qwen2.5-Coder (GGUF CPU)")st.caption("Optimized for Hugging Face Free Tier")
# 2. Model Loading (Cached to prevent reloading on every click)# 2. Model Loading with specific error handling@st.cache_resourcedef load_model(): model_id = "Qwen/Qwen2.5-Coder-3B-Instruct" # Load tokenizer tokenizer = AutoTokenizer.from_pretrained(model_id) # Load model with 4-bit quantization to save RAM (Crucial for 16GB limit) model = AutoModelForCausalLM.from_pretrained( model_id, device_map="auto", torch_dtype="auto", trust_remote_code=True ) # Create the pipeline pipe = pipeline( "text-generation", model=model, tokenizer=tokenizer, ) return pipedef load_llm(): try: # We use the 3B-Q4_K_M for a good balance of logic and RAM usage return Llama.from_pretrained( repo_id="Qwen/Qwen2.5-Coder-3B-Instruct-GGUF", filename="qwen2.5-coder-3b-instruct-q4_k_m.gguf", # Explicit filename n_ctx=4096, # Context window n_threads=2, # Matches HF Free Tier vCPUs verbose=False # Reduces log clutter ) except Exception as e: st.error(f"Error loading model: {e}") return None
# Initialize the pipelinegenerator = load_model()llm = load_llm()
# 3. Chat History Setup# 3. Enhanced "Claude-style" System Promptif "messages" not in st.session_state: st.session_state.messages = [ {"role": "system", "content": "You are an expert software engineer like Claude. Provide complete, production-ready code with explanations."} { "role": "system", "content": "You are an expert AI programming assistant. When asked to write code, provide the full file content. Use clear comments, follow best practices, and ensure the code is production-ready." } ]
# Display chat historyfor message in st.session_state.messages: if message["role"] != "system": with st.chat_message(message["role"]): st.markdown(message["content"])# Display historyfor msg in st.session_state.messages: if msg["role"] != "system": with st.chat_message(msg["role"]): st.markdown(msg["content"])
# 4. Chat Input & Generationif prompt := st.chat_input("Ask me to write some code..."): # Add user message to state# 4. Generation Logicif prompt := st.chat_input("Write a Python script to scrape a website..."): st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt)
with st.chat_message("assistant"): with st.spinner("Writing code..."): # Generate response response = generator( st.session_state.messages, max_new_tokens=1024, temperature=0.7, top_p=0.9, return_full_text=False if llm: with st.chat_message("assistant"): response_placeholder = st.empty() full_response = "" # Stream the response output = llm.create_chat_completion( messages=st.session_state.messages, stream=True, max_tokens=1500, # Increased for "Complete Code" tasks temperature=0.1 # Lower temperature = more precise code ) answer = response[0]['generated_text'] st.markdown(answer) for chunk in output: if 'content' in chunk['choices'][0]['delta']: token = chunk['choices'][0]['delta']['content'] full_response += token response_placeholder.markdown(full_response + "▌") # Add assistant message to state st.session_state.messages.append({"role": "assistant", "content": answer}) response_placeholder.markdown(full_response) st.session_state.messages.append({"role": "assistant", "content": full_response})app.py+52-54