Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,29 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
model_name = "deepseek-ai/deepseek-coder-6.7b-instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Function to generate comments
|
| 10 |
def generate_code_comments(code_snippet):
|
| 11 |
prompt = f"### Code:\n{code_snippet}\n### Add meaningful comments to this code:\n"
|
| 12 |
-
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 13 |
outputs = model.generate(**inputs, max_length=512)
|
| 14 |
commented_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 15 |
return commented_code
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Model name
|
| 6 |
+
model_name = "deepseek-ai/deepseek-coder-6.7b-instruct"
|
| 7 |
+
|
| 8 |
+
# Use quantization (4-bit) to reduce memory usage
|
| 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 |
+
quantization_config=bnb_config,
|
| 20 |
+
device_map="auto" # Automatically chooses best device (CPU/GPU)
|
| 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).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 27 |
outputs = model.generate(**inputs, max_length=512)
|
| 28 |
commented_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
return commented_code
|