change to phi-3
Browse files
app.py
CHANGED
@@ -1,46 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
8 |
model = torch.compile(model)
|
9 |
|
|
|
10 |
prompt_prefix = """
|
11 |
-
You are an energy
|
12 |
Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
|
13 |
1. Flag the highest energy consumers.
|
14 |
2. Recommend practical, empathetic, achievable actions.
|
15 |
3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
|
16 |
Format with bullet points.
|
17 |
-
Here is the
|
18 |
"""
|
19 |
|
|
|
20 |
def generate_recommendation(appliance_info: str) -> str:
|
21 |
-
# Build the full prompt
|
22 |
prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
|
23 |
-
# Tokenize and move inputs to the model device
|
24 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
25 |
-
# Generate with no grad, limited tokens
|
26 |
with torch.no_grad():
|
27 |
outputs = model.generate(
|
28 |
**inputs,
|
29 |
-
max_new_tokens=
|
30 |
-
use_cache=True
|
|
|
|
|
31 |
)
|
32 |
-
# Decode and return only the recommendations section
|
33 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
34 |
return text.split("Recommendations:")[-1].strip()
|
35 |
|
36 |
-
#
|
37 |
iface = gr.Interface(
|
38 |
fn=generate_recommendation,
|
39 |
-
inputs=gr.Textbox(lines=
|
40 |
outputs="text",
|
41 |
-
title="Energy
|
42 |
-
description="
|
43 |
)
|
44 |
|
45 |
if __name__ == "__main__":
|
46 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
# ββ Model & tokenizer βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
6 |
+
model_name = "microsoft/Phi-3-mini-4k-instruct" # 3.8β―Bβparam open instruct model :contentReference[oaicite:0]{index=0}
|
7 |
+
|
8 |
+
# seed for reproducibility
|
9 |
+
torch.random.manual_seed(0)
|
10 |
+
|
11 |
+
# load tokenizer (no gating, open access) :contentReference[oaicite:1]{index=1}
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
13 |
|
14 |
+
# load as causalβLM, quantize to int8 to halve memory & speed up inference :contentReference[oaicite:2]{index=2}
|
|
|
15 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
16 |
+
# compile once for ~30% further speedβup :contentReference[oaicite:3]{index=3}
|
17 |
model = torch.compile(model)
|
18 |
|
19 |
+
# ββ Prompt template ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
20 |
prompt_prefix = """
|
21 |
+
You are an energyβsaving expert tasked to help households reduce their monthly electricity bills.
|
22 |
Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
|
23 |
1. Flag the highest energy consumers.
|
24 |
2. Recommend practical, empathetic, achievable actions.
|
25 |
3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
|
26 |
Format with bullet points.
|
27 |
+
Here is the summary:
|
28 |
"""
|
29 |
|
30 |
+
# ββ Generation function ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
31 |
def generate_recommendation(appliance_info: str) -> str:
|
|
|
32 |
prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
|
|
|
33 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
34 |
with torch.no_grad():
|
35 |
outputs = model.generate(
|
36 |
**inputs,
|
37 |
+
max_new_tokens=150,
|
38 |
+
use_cache=True,
|
39 |
+
do_sample=False,
|
40 |
+
temperature=0.0
|
41 |
)
|
|
|
42 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
43 |
+
# return only the recommendations section
|
44 |
return text.split("Recommendations:")[-1].strip()
|
45 |
|
46 |
+
# ββ Gradio interface ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
47 |
iface = gr.Interface(
|
48 |
fn=generate_recommendation,
|
49 |
+
inputs=gr.Textbox(lines=8, placeholder="e.g. Refrigerator: 150β―W, 8β―h/day, 7β―days/week\n..."),
|
50 |
outputs="text",
|
51 |
+
title="EnergyβSaving Tips (Phiβ3βMiniβ4KβInstruct)",
|
52 |
+
description="Paste your perβappliance summary to get targeted energyβsaving recommendations."
|
53 |
)
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
+
iface.launch()
|