clean code
Browse files
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 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
)
|
25 |
-
|
26 |
-
model =
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|