optimise code
Browse files
app.py
CHANGED
@@ -1,39 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
|
5 |
# Load the pre-trained model and tokenizer
|
6 |
-
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
9 |
-
"microsoft/phi-2",
|
10 |
-
load_in_8bit=True,
|
11 |
-
device_map="auto"
|
12 |
-
)
|
13 |
model = torch.compile(model)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
- Avoiding unnecessary standby power consumption
|
24 |
-
- Considering lifestyle changes (e.g., reading books instead of watching TV)
|
25 |
-
|
26 |
-
Format the output clearly with bullet points or short paragraphs.
|
27 |
-
Be empathetic, practical, and encouraging. Focus on achievable actions for the user.
|
28 |
-
|
29 |
-
Here is the user's input:
|
30 |
"""
|
31 |
|
32 |
-
def generate_recommendation(
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
with torch.no_grad():
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Define the Gradio interface
|
39 |
iface = gr.Interface(
|
@@ -45,4 +44,4 @@ iface = gr.Interface(
|
|
45 |
)
|
46 |
|
47 |
if __name__ == "__main__":
|
48 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
# Load the pre-trained model and tokenizer
|
6 |
+
model_name = "google/flan-t5-base"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
9 |
model = torch.compile(model)
|
10 |
|
11 |
+
prompt_prefix = """
|
12 |
+
You are an energy-saving expert tasked to help households reduce their monthly electricity bills.
|
13 |
+
Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
|
14 |
+
1. Flag the highest energy consumers.
|
15 |
+
2. Recommend practical, empathetic, achievable actions.
|
16 |
+
3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
|
17 |
+
Format with bullet points.
|
18 |
+
Here is the summary:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
"""
|
20 |
|
21 |
+
def generate_recommendation(appliance_info: str) -> str:
|
22 |
+
# Build the full prompt
|
23 |
+
prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
|
24 |
+
# Tokenize and move inputs to the model device
|
25 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
26 |
+
# Generate with no grad, limited tokens
|
27 |
with torch.no_grad():
|
28 |
+
outputs = model.generate(
|
29 |
+
**inputs,
|
30 |
+
max_new_tokens=120,
|
31 |
+
use_cache=True
|
32 |
+
)
|
33 |
+
# Decode and return only the recommendations section
|
34 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
35 |
+
return text.split("Recommendations:")[-1].strip()
|
36 |
|
37 |
# Define the Gradio interface
|
38 |
iface = gr.Interface(
|
|
|
44 |
)
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
+
iface.launch()
|