fine-tune model
Browse files
app.py
CHANGED
@@ -1,31 +1,39 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
-
from transformers import
|
5 |
-
from
|
|
|
|
|
6 |
|
7 |
-
# Download model files
|
8 |
def setup_model():
|
9 |
-
|
10 |
-
|
11 |
-
|
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 |
-
|
19 |
-
model =
|
20 |
-
|
21 |
-
|
|
|
22 |
trust_remote_code=True
|
23 |
)
|
24 |
-
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
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):
|
@@ -33,41 +41,46 @@ Given the user's appliance usage information (device name, wattage, hours used p
|
|
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 suggestions and format with bullet points that is <= 100 tokens.
|
36 |
-
Don't add anything to the response besides the recommendation
|
37 |
Here is the user's input:
|
38 |
"""
|
39 |
|
40 |
def generate_recommendation(appliance_info: str) -> str:
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
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 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
)
|
|
|
71 |
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
iface.launch()
|
|
|
|
|
|
|
|
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/Qwen-2.5B-0.5", 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):
|
|
|
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()
|