AppTry / app.py
Wh1plashR's picture
using qwen2.5
84f6b1a verified
raw
history blame
3.12 kB
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from huggingface_hub import hf_hub_download
# ── Download the GGUF file (only once) ───────────────────────────────────────────
gguf_path = hf_hub_download(
repo_id="Qwen/Qwen2.5-0.5B-Instruct-GGUF",
filename="qwen2.5-0.5b-instruct-q5_k_m.gguf"
)
# ── Load tokenizer & model via gguf_file ────────────────────────────────────────
# Transformers will dequantize the GGUF into fp32 for you
tokenizer = AutoTokenizer.from_pretrained(
"Qwen/Qwen2.5-0.5B-Instruct-GGUF",
gguf_file=gguf_path
)
model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2.5-0.5B-Instruct-GGUF",
gguf_file=gguf_path,
device_map="auto", # spread layers across GPU/CPU
torch_dtype=torch.float16 # run in fp16 for speed if GPU supports it
)
# Compile the model for ~20–30% extra speed (PyTorch 2.0+)
model = torch.compile(model)
# ── Prompt template ─────────────────────────────────────────────────────────────
prompt_prefix = """
You are an energy‑saving expert tasked to help households reduce their monthly electricity bills.
Given the user's appliance usage information (device name, wattage, hours used per day, days used per week):
1. Flag the highest energy consumers.
2. Recommend practical, empathetic, achievable actions.
3. Suggest appliance swaps (e.g. LED, inverter AC) and habit changes.
Format with bullet points.
Here is the summary:
"""
# ── Generation function ─────────────────────────────────────────────────────────
def generate_recommendation(appliance_info: str) -> str:
prompt = prompt_prefix + appliance_info + "\n\nRecommendations:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=120,
use_cache=True,
do_sample=False,
temperature=0.0
)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return text.split("Recommendations:")[-1].strip()
# ── Gradio interface ────────────────────────────────────────────────────────────
iface = gr.Interface(
fn=generate_recommendation,
inputs=gr.Textbox(lines=8, placeholder="e.g. Refrigerator: 150β€―W, 8β€―h/day, 7β€―days/week\n..."),
outputs="text",
title="Energy‑Saving Tips (Qwen2.5‑0.5B‑Instruct‑GGUF)",
description="Provide your appliance usage summary to get targeted, gguf‑powered energy‑saving recommendations."
)
if __name__ == "__main__":
iface.launch()