Wh1plashR commited on
Commit
89e764a
·
verified ·
1 Parent(s): 660fef1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -56
app.py CHANGED
@@ -1,86 +1,73 @@
1
  import os
2
  import gradio as gr
3
  import torch
4
- from transformers import AutoTokenizer, AutoModelForCausalLM
5
- from peft import PeftModel
6
-
7
- # Model initialization
8
 
 
9
  def setup_model():
10
- # Load base model in 4-bit or full precision depending on availability
11
- base = AutoModelForCausalLM.from_pretrained(
12
- "Qwen/Qwen2.5-0.5B-Instruct", device_map="auto", trust_remote_code=True
 
 
 
 
 
13
  )
14
- # Load LoRA adapters
15
- model = PeftModel.from_pretrained(
16
- base,
17
- "Wh1plashR/qwen-energy-lora",
18
- device_map="auto",
19
  trust_remote_code=True
20
  )
21
- # Load tokenizer
22
- tokenizer = AutoTokenizer.from_pretrained("Wh1plashR/qwen-energy-lora", use_fast=True)
23
-
24
- # Set to eval and optionally compile (requires PyTorch 2+)
25
- model.eval()
26
- try:
27
- model = torch.compile(model)
28
- except Exception:
29
- pass
30
 
31
- return tokenizer, model
32
-
33
- # Initialize
34
  tokenizer, model = setup_model()
35
 
36
- # Prompt prefix
37
  prompt_prefix = """
38
  You are an energy‑saving expert tasked to help households reduce their monthly electricity bills.
39
  Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
40
  1. Flag the highest energy consumers.
41
  2. Recommend practical, empathetic, achievable actions.
42
  3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
43
- Give at most 5 suggestions and format with bullet points that is <= 100 tokens.
44
- Don't add anything to the response besides the recommendation.
45
  Here is the user's input:
46
  """
47
 
48
  def generate_recommendation(appliance_info: str) -> str:
49
- # Build prompt
50
- prompt = prompt_prefix + appliance_info.strip() + "\n\nRecommendations:"
51
- # Tokenize and move to model device
52
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True).to(model.device)
53
-
54
- # Generate
55
  with torch.no_grad():
56
  outputs = model.generate(
57
  **inputs,
58
  max_new_tokens=100,
 
59
  do_sample=False,
60
- temperature=0.0,
61
- use_cache=True
62
  )
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- # Decode and clean
65
- text = tokenizer.decode(outputs[0], skip_special_tokens=True)
66
- # Extract recommendations
67
- rec = text.split("Recommendations:")[-1].strip()
68
- # Remove any trailing notes
69
- rec = rec.split("Note:")[0].strip()
70
- # Clean empty lines
71
- cleaned = "\n".join(line.strip() for line in rec.splitlines() if line.strip())
72
- return cleaned
73
-
74
- # Gradio interface
75
- def main():
76
- iface = gr.Interface(
77
- fn=generate_recommendation,
78
- inputs=gr.Textbox(lines=10, placeholder="Enter appliance usage details..."),
79
- outputs=gr.Textbox(label="Energy-Saving Recommendations"),
80
- title="Energy-Saving Recommendation Generator",
81
- description="Provide appliance usage details to receive actionable energy-saving tips."
82
- )
83
- iface.launch()
84
 
85
  if __name__ == "__main__":
86
- main()
 
1
  import os
2
  import gradio as gr
3
  import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ from huggingface_hub import snapshot_download, hf_hub_download
 
 
6
 
7
+ # Download model files
8
  def setup_model():
9
+ instruct_repo = "Qwen/Qwen2.5-0.5B-Instruct"
10
+ local_dir = snapshot_download(repo_id=instruct_repo)
11
+ gguf_filename = "qwen2.5-0.5b-instruct-q5_k_m.gguf"
12
+ hf_hub_download(
13
+ repo_id="Qwen/Qwen2.5-0.5B-Instruct-GGUF",
14
+ filename=gguf_filename,
15
+ local_dir=local_dir,
16
+ local_dir_use_symlinks=False
17
  )
18
+ tokenizer = AutoTokenizer.from_pretrained(local_dir, trust_remote_code=True)
19
+ model = AutoModelForCausalLM.from_pretrained(
20
+ local_dir,
21
+ gguf_file=gguf_filename,
 
22
  trust_remote_code=True
23
  )
24
+ return tokenizer, torch.compile(model)
 
 
 
 
 
 
 
 
25
 
 
 
 
26
  tokenizer, model = setup_model()
27
 
28
+
29
  prompt_prefix = """
30
  You are an energy‑saving expert tasked to help households reduce their monthly electricity bills.
31
  Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
32
  1. Flag the highest energy consumers.
33
  2. Recommend practical, empathetic, achievable actions.
34
  3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
35
+ Give at most 5 recommendations and format with bullet points that is <= 100 tokens.
36
+ Stop when you have 5 recommendations.
37
  Here is the user's input:
38
  """
39
 
40
  def generate_recommendation(appliance_info: str) -> str:
41
+ prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
42
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
 
 
43
  with torch.no_grad():
44
  outputs = model.generate(
45
  **inputs,
46
  max_new_tokens=100,
47
+ use_cache=True,
48
  do_sample=False,
49
+ temperature=0.0
 
50
  )
51
+ text = tokenizer.decode(outputs[0], skip_special_tokens=True)
52
+ recommendation = text.split("Recommendations:")[-1].strip()
53
+
54
+
55
+ if "Note:" in recommendation:
56
+
57
+ recommendation = recommendation.split("Note:")[0].strip()
58
+
59
+ cleaned_recommendation = "\n".join(line.strip() for line in recommendation.splitlines() if line.strip())
60
+
61
+ return cleaned_recommendation
62
 
63
+ # Define the Gradio interface
64
+ iface = gr.Interface(
65
+ fn=generate_recommendation,
66
+ inputs=gr.Textbox(lines=10, placeholder="Enter appliance usage details..."),
67
+ outputs="text",
68
+ title="Energy-Saving Recommendation Generator",
69
+ description="Provide appliance usage details to receive energy-saving tips."
70
+ )
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  if __name__ == "__main__":
73
+ iface.launch()