SHAMIL SHAHBAZ AWAN commited on
Commit
1eea6c5
Β·
verified Β·
1 Parent(s): aad3197

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -24
app.py CHANGED
@@ -1,37 +1,23 @@
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import torch
4
- from accelerate import init_empty_weights, load_checkpoint_and_dispatch
5
 
6
- # Load the model using Accelerate for memory optimization
7
  @st.cache_resource()
8
  def load_model():
9
- MODEL_NAME = "Salesforce/codegen-2B-multi" # Updated model name
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
-
12
- # Load model with accelerate to optimize for memory usage
13
- with init_empty_weights():
14
- model = AutoModelForCausalLM.from_pretrained(
15
- MODEL_NAME,
16
- torch_dtype=torch.float32, # Use float32 for CPU
17
- low_cpu_mem_usage=True # Enable low memory usage on CPU
18
- )
19
-
20
- # Use 'auto' or 'balanced' device_map for CPU or GPU
21
- model = load_checkpoint_and_dispatch(
22
- model,
23
- MODEL_NAME,
24
- device_map="auto", # Automatically determine the best device allocation
25
- )
26
-
27
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
28
 
29
  # Initialize the model
30
  code_generator = load_model()
31
 
32
  # Streamlit UI
33
- st.title("CodeGen-2B Code Bot πŸš€")
34
- st.subheader("Generate code snippets using Hugging Face CodeGen-2B")
35
 
36
  # User input
37
  prompt = st.text_area("Enter a coding prompt (e.g., 'Write a Python function to sort a list'): ")
@@ -41,10 +27,10 @@ if st.button("Generate Code"):
41
  if prompt.strip():
42
  st.info("Generating code... Please wait ⏳")
43
  try:
44
- # Generate code using the CodeGen-2B model
45
  response = code_generator(
46
  prompt,
47
- max_length=512, # Increase for longer code generation
48
  temperature=0.2, # Lower temperature for more deterministic results
49
  do_sample=True, # Enable sampling
50
  num_return_sequences=1
@@ -57,4 +43,4 @@ if st.button("Generate Code"):
57
  else:
58
  st.warning("Please enter a prompt.")
59
 
60
- st.caption("Powered by CodeGen-2B | Streamlit UI | CPU Optimized")
 
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import torch
 
4
 
5
+ # Load the model and tokenizer
6
  @st.cache_resource()
7
  def load_model():
8
+ MODEL_NAME = "Salesforce/codegen-350M-mono" # Use a known model
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float32)
11
+
12
+ # Load model into the pipeline for generation
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
14
 
15
  # Initialize the model
16
  code_generator = load_model()
17
 
18
  # Streamlit UI
19
+ st.title("CodeGen Code Bot πŸš€")
20
+ st.subheader("Generate code snippets using Hugging Face CodeGen")
21
 
22
  # User input
23
  prompt = st.text_area("Enter a coding prompt (e.g., 'Write a Python function to sort a list'): ")
 
27
  if prompt.strip():
28
  st.info("Generating code... Please wait ⏳")
29
  try:
30
+ # Generate code using the model
31
  response = code_generator(
32
  prompt,
33
+ max_length=512, # Adjust for longer code if needed
34
  temperature=0.2, # Lower temperature for more deterministic results
35
  do_sample=True, # Enable sampling
36
  num_return_sequences=1
 
43
  else:
44
  st.warning("Please enter a prompt.")
45
 
46
+ st.caption("Powered by CodeGen | Streamlit UI")