Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,24 @@
|
|
1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
model_name = "deepseek-ai/deepseek-coder-
|
7 |
|
8 |
-
#
|
9 |
-
bnb_config = BitsAndBytesConfig(
|
10 |
-
load_in_4bit=True, # Use 4-bit quantization
|
11 |
-
bnb_4bit_compute_dtype=torch.float16, # Reduce precision
|
12 |
-
bnb_4bit_use_double_quant=True, # Further optimize memory
|
13 |
-
)
|
14 |
-
|
15 |
-
# Load model with optimizations
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
|
17 |
model = AutoModelForCausalLM.from_pretrained(
|
18 |
model_name,
|
19 |
-
|
20 |
-
device_map="
|
21 |
)
|
22 |
|
23 |
# Function to generate comments
|
24 |
def generate_code_comments(code_snippet):
|
25 |
prompt = f"### Code:\n{code_snippet}\n### Add meaningful comments to this code:\n"
|
26 |
-
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
27 |
outputs = model.generate(**inputs, max_length=512)
|
28 |
commented_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
return commented_code
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
|
5 |
+
# Load a smaller model that fits within 16GB RAM
|
6 |
+
model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
7 |
|
8 |
+
# Load tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Load model in CPU-friendly format (low precision for efficiency)
|
12 |
model = AutoModelForCausalLM.from_pretrained(
|
13 |
model_name,
|
14 |
+
torch_dtype=torch.float32, # Use float32 since CPU-only
|
15 |
+
device_map="cpu" # Ensure it runs only on CPU
|
16 |
)
|
17 |
|
18 |
# Function to generate comments
|
19 |
def generate_code_comments(code_snippet):
|
20 |
prompt = f"### Code:\n{code_snippet}\n### Add meaningful comments to this code:\n"
|
21 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
22 |
outputs = model.generate(**inputs, max_length=512)
|
23 |
commented_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
return commented_code
|