Wh1plashR commited on
Commit
12662f5
Β·
verified Β·
1 Parent(s): 77179bf

clean code

Browse files
Files changed (1) hide show
  1. app.py +31 -49
app.py CHANGED
@@ -4,65 +4,47 @@ import torch
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  from huggingface_hub import snapshot_download, hf_hub_download
6
 
7
- instruct_repo = "Qwen/Qwen2.5-0.5B-Instruct"
8
- local_dir = snapshot_download(repo_id=instruct_repo)
9
-
10
- gguf_filename = "qwen2.5-0.5b-instruct-q5_k_m.gguf"
11
- hf_hub_download(
12
- repo_id="Qwen/Qwen2.5-0.5B-Instruct-GGUF",
13
- filename=gguf_filename,
14
- local_dir=local_dir,
15
- local_dir_use_symlinks=False
16
- )
17
-
18
- gguf_path = os.path.join(local_dir, gguf_filename)
19
- assert os.path.isfile(gguf_path), f"GGUF not found at {gguf_path}"
20
-
21
- tokenizer = AutoTokenizer.from_pretrained(
22
- local_dir,
23
- trust_remote_code=True
24
- )
25
-
26
- model = AutoModelForCausalLM.from_pretrained(
27
- local_dir,
28
- gguf_file=gguf_filename, # relative to local_dir
29
- trust_remote_code=True
 
 
 
 
 
30
  )
31
- model = torch.compile(model) # PyTorch 2.x compile for ~20–30% speedup
32
-
33
- prompt_prefix = """
34
- You are an energy‑saving expert tasked to help households reduce their monthly electricity bills.
35
- Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
36
- 1. Flag the highest energy consumers.
37
- 2. Recommend practical, empathetic, achievable actions.
38
- 3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
39
- Give at most 5 suggestions and format with bullet points that is less than or equal to 120 tokens.
40
- Don't add anything unnecessary on your response
41
- Here is the summary:
42
- """
43
 
 
44
  def generate_recommendation(appliance_info: str) -> str:
45
- prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
46
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
47
  with torch.no_grad():
48
  outputs = model.generate(
49
  **inputs,
50
  max_new_tokens=120,
51
- use_cache=True,
52
  do_sample=False,
53
  temperature=0.0
54
  )
55
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
56
  return text.split("Recommendations:")[-1].strip()
57
-
58
- # ── Gradio interface ────────────────────────────────────────────────────────────
59
- iface = gr.Interface(
60
- fn=generate_recommendation,
61
- inputs=gr.Textbox(lines=8, placeholder="e.g. Refrigerator: 150β€―W, 8β€―h/day, 7β€―days/week\n..."),
62
- outputs="text",
63
- title="Energy‑Saving Tips (Qwen2.5‑0.5B‑Instruct‑GGUF)",
64
- description="Provide your appliance usage summary to get targeted, GGUF‑powered energy‑saving recommendations."
65
- )
66
-
67
- if __name__ == "__main__":
68
- iface.launch()
 
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
+ prompt_prefix = (
29
+ "You are the best energy-saving advisor. "
30
+ "Given appliances (name, wattage, hours/day, days/week), identify top consumers and up to 5 actionable bullet-point recommendations (practical, empathetic), "
31
+ "including appliance swaps and habit changes. "
32
+ "For each, include estimated monthly kWh saved and cost reduction. "
33
+ "Keep response under 120 tokens, bullets only."
34
+ "\nSummary:\n"
35
  )
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ # Generation function
38
  def generate_recommendation(appliance_info: str) -> str:
39
+ prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
40
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
41
  with torch.no_grad():
42
  outputs = model.generate(
43
  **inputs,
44
  max_new_tokens=120,
45
+ return_dict_in_generate=False,
46
  do_sample=False,
47
  temperature=0.0
48
  )
49
  text = tokenizer.decode(outputs[0], skip_special_tokens=True)
50
  return text.split("Recommendations:")[-1].strip()