Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
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
|
| 7 |
@st.cache_resource()
|
| 8 |
def load_model():
|
| 9 |
-
MODEL_NAME = "Salesforce/codegen-
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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
|
| 34 |
-
st.subheader("Generate code snippets using Hugging Face CodeGen
|
| 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
|
| 45 |
response = code_generator(
|
| 46 |
prompt,
|
| 47 |
-
max_length=512, #
|
| 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
|
|
|
|
| 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")
|