Wh1plashR commited on
Commit
be2c3d1
·
verified ·
1 Parent(s): cf42c9a

Optimised code

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -1,11 +1,16 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
  # Load the pre-trained model and tokenizer
6
- model_name = "microsoft/phi-2"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
 
9
 
10
  promptPre = f"""You are an energy-saving expert tasked to help households reduce their monthly electricity bills.
11
  Given the user's appliance usage information (including device name, wattage, hours used per day, and days used per week),
@@ -24,12 +29,11 @@ Be empathetic, practical, and encouraging. Focus on achievable actions for the u
24
  Here is the user's input:
25
  """
26
 
27
- def generate_recommendation(appliance_info):
28
- prompt = f"Input: promptPre + {appliance_info}\nOutput:"
29
- inputs = tokenizer(prompt, return_tensors="pt")
30
- outputs = model.generate(**inputs, max_new_tokens=200)
31
- recommendation = tokenizer.decode(outputs[0], skip_special_tokens=True)
32
- return recommendation.split("Output:")[-1].strip()
33
 
34
  # Define the Gradio interface
35
  iface = gr.Interface(
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
  # Load the pre-trained model and tokenizer
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ "microsoft/phi-2",
10
+ load_in_8bit=True,
11
+ device_map="auto"
12
+ )
13
+ model = torch.compile(model)
14
 
15
  promptPre = f"""You are an energy-saving expert tasked to help households reduce their monthly electricity bills.
16
  Given the user's appliance usage information (including device name, wattage, hours used per day, and days used per week),
 
29
  Here is the user's input:
30
  """
31
 
32
+ def generate_recommendation(summary):
33
+ inputs = tokenizer(summary, return_tensors="pt").to(model.device)
34
+ with torch.no_grad():
35
+ out = model.generate(**inputs, max_new_tokens=100, use_cache=True)
36
+ return tokenizer.decode(out[0], skip_special_tokens=True)
 
37
 
38
  # Define the Gradio interface
39
  iface = gr.Interface(