Optimised code
Browse files
app.py
CHANGED
@@ -1,11 +1,16 @@
|
|
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 |
|
10 |
promptPre = f"""You are an energy-saving expert tasked to help households reduce their monthly electricity bills.
|
11 |
Given the user's appliance usage information (including device name, wattage, hours used per day, and days used per week),
|
@@ -24,12 +29,11 @@ Be empathetic, practical, and encouraging. Focus on achievable actions for the u
|
|
24 |
Here is the user's input:
|
25 |
"""
|
26 |
|
27 |
-
def generate_recommendation(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
return recommendation.split("Output:")[-1].strip()
|
33 |
|
34 |
# Define the Gradio interface
|
35 |
iface = gr.Interface(
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
5 |
# Load the pre-trained model and tokenizer
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
|
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 |
promptPre = f"""You are an energy-saving expert tasked to help households reduce their monthly electricity bills.
|
16 |
Given the user's appliance usage information (including device name, wattage, hours used per day, and days used per week),
|
|
|
29 |
Here is the user's input:
|
30 |
"""
|
31 |
|
32 |
+
def generate_recommendation(summary):
|
33 |
+
inputs = tokenizer(summary, return_tensors="pt").to(model.device)
|
34 |
+
with torch.no_grad():
|
35 |
+
out = model.generate(**inputs, max_new_tokens=100, use_cache=True)
|
36 |
+
return tokenizer.decode(out[0], skip_special_tokens=True)
|
|
|
37 |
|
38 |
# Define the Gradio interface
|
39 |
iface = gr.Interface(
|