Katieearl1513 commited on
Commit
7bd8347
·
verified ·
1 Parent(s): 5b50f11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -21
app.py CHANGED
@@ -5,21 +5,8 @@
5
  import gradio as gr
6
  import torch
7
  from transformers import AutoModelForCausalLM, AutoTokenizer
8
- #code addition to handle memery offloading
9
- from accelerate import Accelerator
10
- import spaces
11
-
12
- accelerator = Accelerator()
13
- '''
14
- import spaces
15
- MPS (metal Performance Shaders) uses in bfloat16 (torch_dtype) for its low memory usage, quicker inference - which is supported by GPU.
16
- if a lower float version is used then device can maintain to cpu without issues
17
- in Original code: "cuda" if torch.cuda.is_available() else "cpu"
18
- alterative: "mps" if torch.backends.mps.is_available() else "cpu"
19
- if no MPS is used then just use device = 'cpu'
20
- I am suing accelerator's device management
21
- '''
22
- device = accelerator.device
23
 
24
  #model used: from transformers library - example used is Qwen/Qwen2–0.5B-Instruct
25
  #The tokenizer is loaded to convert the user’s input into a form that the model can understand (tokenization).
@@ -30,12 +17,8 @@ model = AutoModelForCausalLM.from_pretrained(
30
  ) #.to(device)
31
  tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
32
 
33
- #code addition to handle model placement: instead of .to(device) use the accelerator.prepare to ensure
34
- #that the model and data are on the right devices for distributed training or inference.
35
- model = accelerator.prepare(model)
36
-
37
  #decorator used to signal that the subsequent function “def chatbot()” can use GPU memory
38
- @spaces.GPU
39
 
40
  #assigning role to chatbot with system message, and message definition. Also checks for history (previous messages)
41
  def chatbot(user_input, history):
@@ -49,7 +32,6 @@ def chatbot(user_input, history):
49
  messages,
50
  tokenize=False,
51
  add_generation_prompt=True
52
- )
53
 
54
  #prepared text is converted into tokens the model understands (model_input) and resulting 'pt' is moved to device (CPU/GPU)
55
  #the 'important' part of the input is signaled with attention_marsk but in this case it is all input - thats why useing torch.ones
@@ -110,4 +92,5 @@ demo.launch()
110
 
111
 
112
 
 
113
 
 
5
  import gradio as gr
6
  import torch
7
  from transformers import AutoModelForCausalLM, AutoTokenizer
8
+
9
+ device = torch.device('cuda' if torch.cuda.is_available() else "cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  #model used: from transformers library - example used is Qwen/Qwen2–0.5B-Instruct
12
  #The tokenizer is loaded to convert the user’s input into a form that the model can understand (tokenization).
 
17
  ) #.to(device)
18
  tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
19
 
 
 
 
 
20
  #decorator used to signal that the subsequent function “def chatbot()” can use GPU memory
21
+ #@spaces.GPU
22
 
23
  #assigning role to chatbot with system message, and message definition. Also checks for history (previous messages)
24
  def chatbot(user_input, history):
 
32
  messages,
33
  tokenize=False,
34
  add_generation_prompt=True
 
35
 
36
  #prepared text is converted into tokens the model understands (model_input) and resulting 'pt' is moved to device (CPU/GPU)
37
  #the 'important' part of the input is signaled with attention_marsk but in this case it is all input - thats why useing torch.ones
 
92
 
93
 
94
 
95
+
96