Spaces:
Build error
Build error
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373 unmodified lines414243444546474849506 unmodified lines57585960import streamlit as stfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipelineimport torchfrom accelerate import init_empty_weights, load_checkpoint_and_dispatch# Load the model using Accelerate for memory optimization@st.cache_resource()def load_model():MODEL_NAME = "Salesforce/codegen-2B-multi" # Updated model nametokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)# Load model with accelerate to optimize for memory usagewith init_empty_weights():model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,torch_dtype=torch.float32, # Use float32 for CPUlow_cpu_mem_usage=True # Enable low memory usage on CPU)# Use 'auto' or 'balanced' device_map for CPU or GPUmodel = load_checkpoint_and_dispatch(model,MODEL_NAME,device_map="auto", # Automatically determine the best device allocation)return pipeline("text-generation", model=model, tokenizer=tokenizer)# Initialize the modelcode_generator = load_model()# Streamlit UIst.title("CodeGen-2B Code Bot 🚀")st.subheader("Generate code snippets using Hugging Face CodeGen-2B")# User inputprompt = st.text_area("Enter a coding prompt (e.g., 'Write a Python function to sort a list'): ")3 unmodified linesif prompt.strip():st.info("Generating code... Please wait ⏳")try:# Generate code using the CodeGen-2B modelresponse = code_generator(prompt,max_length=512, # Increase for longer code generationtemperature=0.2, # Lower temperature for more deterministic resultsdo_sample=True, # Enable samplingnum_return_sequences=16 unmodified lineselse:st.warning("Please enter a prompt.")st.caption("Powered by CodeGen-2B | Streamlit UI | CPU Optimized")12345678910111213141516171819202122233 unmodified lines272829303132333435366 unmodified lines43444546import streamlit as stfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipelineimport torch# Load the model and tokenizer@st.cache_resource()def load_model():MODEL_NAME = "Salesforce/codegen-350M-mono" # Use a known modeltokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float32)# Load model into the pipeline for generationreturn pipeline("text-generation", model=model, tokenizer=tokenizer)# Initialize the modelcode_generator = load_model()# Streamlit UIst.title("CodeGen Code Bot 🚀")st.subheader("Generate code snippets using Hugging Face CodeGen")# User inputprompt = st.text_area("Enter a coding prompt (e.g., 'Write a Python function to sort a list'): ")3 unmodified linesif prompt.strip():st.info("Generating code... Please wait ⏳")try:# Generate code using the modelresponse = code_generator(prompt,max_length=512, # Adjust for longer code if neededtemperature=0.2, # Lower temperature for more deterministic resultsdo_sample=True, # Enable samplingnum_return_sequences=16 unmodified lineselse:st.warning("Please enter a prompt.")st.caption("Powered by CodeGen | Streamlit UI")
app.py+10-24