Wh1plashR commited on
Commit
e14bcdf
Β·
verified Β·
1 Parent(s): 964994d

change to phi-3

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -1,46 +1,56 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
 
 
 
 
 
 
 
 
 
 
4
 
5
- model_name = "TheBloke/openinstruct‑mistral‑7B‑AWQ"
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-saving expert tasked to help households reduce their monthly electricity bills.
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 input of the user:
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=120,
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
- # Define the Gradio interface
37
  iface = gr.Interface(
38
  fn=generate_recommendation,
39
- inputs=gr.Textbox(lines=10, placeholder="Enter appliance usage details..."),
40
  outputs="text",
41
- title="Energy-Saving Recommendation Generator",
42
- description="Provide appliance usage details to receive energy-saving tips."
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()