Jatin112002 commited on
Commit
badc329
·
verified ·
1 Parent(s): 2064e7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -1,15 +1,29 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import gradio as gr
 
3
 
4
- # Load the DeepSeek-Coder model
5
- model_name = "deepseek-ai/deepseek-coder-6.7b-instruct" # You can use any LLaMA-based model
 
 
 
 
 
 
 
 
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
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