Update app.py
Browse files
app.py
CHANGED
@@ -2,33 +2,39 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
-
from huggingface_hub import snapshot_download
|
6 |
|
7 |
-
# ββ 1)
|
8 |
-
|
9 |
-
local_dir = snapshot_download(repo_id=
|
10 |
|
11 |
-
# the GGUF
|
12 |
gguf_filename = "qwen2.5-0.5b-instruct-q5_k_m.gguf"
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# sanity check
|
15 |
gguf_path = os.path.join(local_dir, gguf_filename)
|
16 |
assert os.path.isfile(gguf_path), f"GGUF not found at {gguf_path}"
|
17 |
|
18 |
-
# ββ
|
19 |
tokenizer = AutoTokenizer.from_pretrained(
|
20 |
local_dir,
|
21 |
-
trust_remote_code=True
|
22 |
)
|
23 |
|
24 |
-
# ββ
|
25 |
model = AutoModelForCausalLM.from_pretrained(
|
26 |
local_dir,
|
27 |
-
gguf_file=gguf_filename,
|
28 |
device_map="auto",
|
29 |
-
trust_remote_code=True
|
30 |
)
|
31 |
-
model = torch.compile(model)
|
32 |
|
33 |
# ββ Prompt template βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
34 |
prompt_prefix = """
|
@@ -62,7 +68,7 @@ iface = gr.Interface(
|
|
62 |
inputs=gr.Textbox(lines=8, placeholder="e.g. Refrigerator: 150β―W, 8β―h/day, 7β―days/week\n..."),
|
63 |
outputs="text",
|
64 |
title="EnergyβSaving Tips (Qwen2.5β0.5BβInstructβGGUF)",
|
65 |
-
description="Provide your
|
66 |
)
|
67 |
|
68 |
if __name__ == "__main__":
|
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
from huggingface_hub import snapshot_download, hf_hub_download
|
6 |
|
7 |
+
# ββ 1) download the βfullβ Instruct repo (config.json, tokenizer files, etc.) ββββ
|
8 |
+
instruct_repo = "Qwen/Qwen2.5-0.5B-Instruct"
|
9 |
+
local_dir = snapshot_download(repo_id=instruct_repo)
|
10 |
|
11 |
+
# ββ 2) download the GGUF weights into that same folder βββββββββββββββββββββββββββ
|
12 |
gguf_filename = "qwen2.5-0.5b-instruct-q5_k_m.gguf"
|
13 |
+
hf_hub_download(
|
14 |
+
repo_id="Qwen/Qwen2.5-0.5B-Instruct-GGUF",
|
15 |
+
filename=gguf_filename,
|
16 |
+
local_dir=local_dir,
|
17 |
+
local_dir_use_symlinks=False
|
18 |
+
)
|
19 |
|
20 |
# sanity check
|
21 |
gguf_path = os.path.join(local_dir, gguf_filename)
|
22 |
assert os.path.isfile(gguf_path), f"GGUF not found at {gguf_path}"
|
23 |
|
24 |
+
# ββ 3) load tokenizer from the combined folder ββββββββββββββββββββββββββββββββββ
|
25 |
tokenizer = AutoTokenizer.from_pretrained(
|
26 |
local_dir,
|
27 |
+
trust_remote_code=True
|
28 |
)
|
29 |
|
30 |
+
# ββ 4) load the causalβLM model, pointing at the GGUF file ββββββββββββββββββββββ
|
31 |
model = AutoModelForCausalLM.from_pretrained(
|
32 |
local_dir,
|
33 |
+
gguf_file=gguf_filename, # relative to local_dir
|
34 |
device_map="auto",
|
35 |
+
trust_remote_code=True
|
36 |
)
|
37 |
+
model = torch.compile(model) # PyTorch 2.x compile for ~20β30% speedup
|
38 |
|
39 |
# ββ Prompt template βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
40 |
prompt_prefix = """
|
|
|
68 |
inputs=gr.Textbox(lines=8, placeholder="e.g. Refrigerator: 150β―W, 8β―h/day, 7β―days/week\n..."),
|
69 |
outputs="text",
|
70 |
title="EnergyβSaving Tips (Qwen2.5β0.5BβInstructβGGUF)",
|
71 |
+
description="Provide your appliance usage summary to get targeted, GGUFβpowered energyβsaving recommendations."
|
72 |
)
|
73 |
|
74 |
if __name__ == "__main__":
|