Jatin112002 commited on
Commit
225a5e1
·
verified ·
1 Parent(s): 829da1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -1,29 +1,24 @@
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
 
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