krisha06 commited on
Commit
75a88cc
·
verified ·
1 Parent(s): ca66e20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -14
app.py CHANGED
@@ -3,26 +3,24 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
- # Load base model and tokenizer
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
  tokenizer = AutoTokenizer.from_pretrained(base_model)
9
 
10
- # Load base model in CPU-only mode
11
  model = AutoModelForCausalLM.from_pretrained(
12
  base_model,
13
- device_map="auto", # Use 'auto' or manually move later
14
  torch_dtype=torch.float32,
15
- low_cpu_mem_usage=True # Ensures meta device usage
 
16
  )
17
-
18
- # Move the model to CPU safely
19
  model = model.to_empty(device=torch.device("cpu"))
20
 
21
- # Now load the LoRA adapter
22
- from peft import PeftModel
23
  model = PeftModel.from_pretrained(model, "lora_adapter", device_map="cpu")
24
  model.eval()
25
 
 
26
  def format_prompt(instruction):
27
  return f"""### SYSTEM:
28
  You are a helpful and expert Python programming tutor.
@@ -30,18 +28,13 @@ You only answer questions related to Python programming.
30
  If the question is unrelated to Python, say:
31
  "Sorry, I can only answer Python-related questions."
32
 
33
- ### USER:
34
- What is a tuple in Python?
35
-
36
- ### ASSISTANT:
37
- A tuple in Python is an ordered and immutable collection of elements. Tuples are defined using parentheses, like (1, 2, 3). They are useful when you want to store multiple items and ensure they cannot be changed.
38
-
39
  ### USER:
40
  {instruction}
41
 
42
  ### ASSISTANT:
43
  """
44
 
 
45
  def chat(instruction):
46
  prompt = format_prompt(instruction)
47
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
+ # Load tokenizer
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
  tokenizer = AutoTokenizer.from_pretrained(base_model)
9
 
10
+ # Load base model in empty (meta) state and move to CPU
11
  model = AutoModelForCausalLM.from_pretrained(
12
  base_model,
 
13
  torch_dtype=torch.float32,
14
+ low_cpu_mem_usage=True,
15
+ device_map="auto"
16
  )
 
 
17
  model = model.to_empty(device=torch.device("cpu"))
18
 
19
+ # Load LoRA adapter and move to CPU
 
20
  model = PeftModel.from_pretrained(model, "lora_adapter", device_map="cpu")
21
  model.eval()
22
 
23
+ # Format prompt for Python tutoring
24
  def format_prompt(instruction):
25
  return f"""### SYSTEM:
26
  You are a helpful and expert Python programming tutor.
 
28
  If the question is unrelated to Python, say:
29
  "Sorry, I can only answer Python-related questions."
30
 
 
 
 
 
 
 
31
  ### USER:
32
  {instruction}
33
 
34
  ### ASSISTANT:
35
  """
36
 
37
+ # Generate answer
38
  def chat(instruction):
39
  prompt = format_prompt(instruction)
40
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)