Delete backend.py
Browse files- backend.py +0 -97
backend.py
DELETED
|
@@ -1,97 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from huggingface_hub import HfApi
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import re
|
| 6 |
-
|
| 7 |
-
class ModelResearcher:
|
| 8 |
-
def __init__(self):
|
| 9 |
-
self.api = HfApi()
|
| 10 |
-
|
| 11 |
-
def search_models(self, task_domain="Language", architecture_type="All", sort_by="downloads", limit=50):
|
| 12 |
-
hf_task = "text-generation" if task_domain == "Language" else "image-classification"
|
| 13 |
-
filter_tags = []
|
| 14 |
-
if architecture_type == "Recurrent (RNN/RWKV/Mamba)": filter_tags.append("rwkv")
|
| 15 |
-
elif architecture_type == "Attention (Transformer)": filter_tags.append("transformers")
|
| 16 |
-
|
| 17 |
-
models = self.api.list_models(
|
| 18 |
-
sort=sort_by, direction=-1, limit=limit,
|
| 19 |
-
filter=filter_tags if filter_tags else None, task=hf_task
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
model_list = []
|
| 23 |
-
for m in models:
|
| 24 |
-
size_match = re.search(r'([0-9\.]+)b', m.modelId.lower())
|
| 25 |
-
size_label = f"{size_match.group(1)}B" if size_match else "N/A"
|
| 26 |
-
if size_label == "N/A": # Fallback check for millions
|
| 27 |
-
size_match_m = re.search(r'([0-9\.]+)m', m.modelId.lower())
|
| 28 |
-
size_label = f"{size_match_m.group(1)}M" if size_match_m else "N/A"
|
| 29 |
-
|
| 30 |
-
model_list.append({
|
| 31 |
-
"model_id": m.modelId, "likes": m.likes, "downloads": m.downloads,
|
| 32 |
-
"created_at": str(m.created_at)[:10], "estimated_params": size_label
|
| 33 |
-
})
|
| 34 |
-
return pd.DataFrame(model_list)
|
| 35 |
-
|
| 36 |
-
class ModelManager:
|
| 37 |
-
def __init__(self, device="cpu"):
|
| 38 |
-
self.device = device
|
| 39 |
-
self.loaded_models = {}
|
| 40 |
-
|
| 41 |
-
def load_model(self, model_id, quantization="None"):
|
| 42 |
-
"""
|
| 43 |
-
Loads model with optional 8-bit quantization.
|
| 44 |
-
quantization: "None" (FP16/32) or "8-bit"
|
| 45 |
-
"""
|
| 46 |
-
# Create a unique key for caching (e.g., "distilgpt2_8bit")
|
| 47 |
-
cache_key = f"{model_id}_{quantization}"
|
| 48 |
-
|
| 49 |
-
if cache_key in self.loaded_models:
|
| 50 |
-
return True, "Already Loaded"
|
| 51 |
-
|
| 52 |
-
try:
|
| 53 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 54 |
-
if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token
|
| 55 |
-
|
| 56 |
-
# Quantization Logic
|
| 57 |
-
load_kwargs = {"trust_remote_code": True}
|
| 58 |
-
|
| 59 |
-
if quantization == "8-bit":
|
| 60 |
-
if self.device == "cpu":
|
| 61 |
-
return False, "8-bit quantization requires a GPU (CUDA)."
|
| 62 |
-
load_kwargs["load_in_8bit"] = True
|
| 63 |
-
load_kwargs["device_map"] = "auto" # Required for bitsandbytes
|
| 64 |
-
else:
|
| 65 |
-
# Standard Loading
|
| 66 |
-
dtype = torch.float16 if self.device == "cuda" else torch.float32
|
| 67 |
-
load_kwargs["torch_dtype"] = dtype
|
| 68 |
-
|
| 69 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, **load_kwargs)
|
| 70 |
-
|
| 71 |
-
if quantization != "8-bit":
|
| 72 |
-
model = model.to(self.device)
|
| 73 |
-
|
| 74 |
-
model.eval()
|
| 75 |
-
self.loaded_models[cache_key] = {"model": model, "tokenizer": tokenizer}
|
| 76 |
-
return True, "Success"
|
| 77 |
-
except Exception as e:
|
| 78 |
-
return False, str(e)
|
| 79 |
-
|
| 80 |
-
def generate_text(self, model_id, quantization, prompt, max_new_tokens=100):
|
| 81 |
-
cache_key = f"{model_id}_{quantization}"
|
| 82 |
-
if cache_key not in self.loaded_models: return "Error: Model not loaded."
|
| 83 |
-
|
| 84 |
-
pkg = self.loaded_models[cache_key]
|
| 85 |
-
inputs = pkg["tokenizer"](prompt, return_tensors="pt").to(self.device)
|
| 86 |
-
|
| 87 |
-
with torch.no_grad():
|
| 88 |
-
outputs = pkg["model"].generate(
|
| 89 |
-
**inputs, max_new_tokens=max_new_tokens, pad_token_id=pkg["tokenizer"].eos_token_id
|
| 90 |
-
)
|
| 91 |
-
return pkg["tokenizer"].decode(outputs[0], skip_special_tokens=True)
|
| 92 |
-
|
| 93 |
-
def get_components(self, model_id, quantization="None"):
|
| 94 |
-
cache_key = f"{model_id}_{quantization}"
|
| 95 |
-
if cache_key in self.loaded_models:
|
| 96 |
-
return self.loaded_models[cache_key]["model"], self.loaded_models[cache_key]["tokenizer"]
|
| 97 |
-
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|