Wh1plashR commited on
Commit
547b515
·
verified ·
1 Parent(s): 2178dfb

optimise code

Browse files
Files changed (1) hide show
  1. app.py +27 -28
app.py CHANGED
@@ -1,39 +1,38 @@
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),
17
- analyze and recommend specific, practical ways they can reduce their energy consumption.
18
-
19
- Always prioritize suggestions like:
20
- - Reducing usage time or frequency when excessive
21
- - Replacing high-consumption appliances with more efficient alternatives (e.g., inverter air conditioners, LED lights)
22
- - Changing habits (e.g., using fans instead of air conditioners when possible)
23
- - Avoiding unnecessary standby power consumption
24
- - Considering lifestyle changes (e.g., reading books instead of watching TV)
25
-
26
- Format the output clearly with bullet points or short paragraphs.
27
- Be empathetic, practical, and encouraging. Focus on achievable actions for the user.
28
-
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(
@@ -45,4 +44,4 @@ iface = gr.Interface(
45
  )
46
 
47
  if __name__ == "__main__":
48
- iface.launch()
 
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 = "google/flan-t5-base"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
9
  model = torch.compile(model)
10
 
11
+ prompt_prefix = """
12
+ You are an energy-saving expert tasked to help households reduce their monthly electricity bills.
13
+ Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
14
+ 1. Flag the highest energy consumers.
15
+ 2. Recommend practical, empathetic, achievable actions.
16
+ 3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
17
+ Format with bullet points.
18
+ Here is the summary:
 
 
 
 
 
 
 
19
  """
20
 
21
+ def generate_recommendation(appliance_info: str) -> str:
22
+ # Build the full prompt
23
+ prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
24
+ # Tokenize and move inputs to the model device
25
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
26
+ # Generate with no grad, limited tokens
27
  with torch.no_grad():
28
+ outputs = model.generate(
29
+ **inputs,
30
+ max_new_tokens=120,
31
+ use_cache=True
32
+ )
33
+ # Decode and return only the recommendations section
34
+ text = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
+ return text.split("Recommendations:")[-1].strip()
36
 
37
  # Define the Gradio interface
38
  iface = gr.Interface(
 
44
  )
45
 
46
  if __name__ == "__main__":
47
+ iface.launch()