Upload folder using huggingface_hub
Browse files- app.py +8 -2
- app/infer.py +19 -9
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -6,13 +6,17 @@ from app.infer import load, predict
|
|
| 6 |
TEXT_COLUMNS = ("text", "comment", "message", "content")
|
| 7 |
MAX_ROWS = 5000
|
| 8 |
|
| 9 |
-
|
| 10 |
|
|
|
|
| 11 |
|
|
|
|
|
|
|
| 12 |
def classify_text(text: str):
|
| 13 |
text = (text or "").strip()
|
| 14 |
if not text:
|
| 15 |
raise gr.Error("text is empty")
|
|
|
|
| 16 |
return predict([text])[0]
|
| 17 |
|
| 18 |
|
|
@@ -24,9 +28,11 @@ def _text_column(columns: list[str]) -> str:
|
|
| 24 |
raise gr.Error(f"CSV needs a text column. Tried: {', '.join(TEXT_COLUMNS)}")
|
| 25 |
|
| 26 |
|
|
|
|
| 27 |
def classify_csv(file):
|
| 28 |
if file is None:
|
| 29 |
raise gr.Error("no CSV")
|
|
|
|
| 30 |
df = pd.read_csv(file)
|
| 31 |
if df.empty:
|
| 32 |
return pd.DataFrame()
|
|
@@ -63,4 +69,4 @@ with gr.Blocks(title="Bengali abusive classifier") as demo:
|
|
| 63 |
gr.Button("Classify CSV").click(classify_csv, f_in, f_out)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
-
demo.launch()
|
|
|
|
| 6 |
TEXT_COLUMNS = ("text", "comment", "message", "content")
|
| 7 |
MAX_ROWS = 5000
|
| 8 |
|
| 9 |
+
import spaces
|
| 10 |
|
| 11 |
+
load(prefer_cuda=False)
|
| 12 |
|
| 13 |
+
|
| 14 |
+
@spaces.GPU(duration=60)
|
| 15 |
def classify_text(text: str):
|
| 16 |
text = (text or "").strip()
|
| 17 |
if not text:
|
| 18 |
raise gr.Error("text is empty")
|
| 19 |
+
load(prefer_cuda=True)
|
| 20 |
return predict([text])[0]
|
| 21 |
|
| 22 |
|
|
|
|
| 28 |
raise gr.Error(f"CSV needs a text column. Tried: {', '.join(TEXT_COLUMNS)}")
|
| 29 |
|
| 30 |
|
| 31 |
+
@spaces.GPU(duration=120)
|
| 32 |
def classify_csv(file):
|
| 33 |
if file is None:
|
| 34 |
raise gr.Error("no CSV")
|
| 35 |
+
load(prefer_cuda=True)
|
| 36 |
df = pd.read_csv(file)
|
| 37 |
if df.empty:
|
| 38 |
return pd.DataFrame()
|
|
|
|
| 69 |
gr.Button("Classify CSV").click(classify_csv, f_in, f_out)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
demo.launch(ssr_mode=False)
|
app/infer.py
CHANGED
|
@@ -26,16 +26,26 @@ def _pick_device() -> str:
|
|
| 26 |
return "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
|
| 28 |
|
| 29 |
-
def load() -> None:
|
| 30 |
global _device, _tokenizer, _model, _xlit
|
| 31 |
-
if
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
def status() -> dict[str, Any]:
|
|
|
|
| 26 |
return "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
|
| 28 |
|
| 29 |
+
def load(prefer_cuda: bool = False) -> None:
|
| 30 |
global _device, _tokenizer, _model, _xlit
|
| 31 |
+
if _tokenizer is None:
|
| 32 |
+
_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 33 |
+
if _model is None:
|
| 34 |
+
# CPU at import: ZeroGPU has no CUDA until @spaces.GPU runs.
|
| 35 |
+
_model = AutoModelForSequenceClassification.from_pretrained(
|
| 36 |
+
MODEL_NAME,
|
| 37 |
+
use_safetensors=True,
|
| 38 |
+
)
|
| 39 |
+
_model.eval()
|
| 40 |
+
_device = "cpu"
|
| 41 |
+
if _xlit is None:
|
| 42 |
+
path = snapshot_download(repo_id=XLIT_REPO, allow_patterns="indicxlit_ct2_fp32/*")
|
| 43 |
+
_xlit = ctranslate2.Translator(os.path.join(path, "indicxlit_ct2_fp32"), device="cpu")
|
| 44 |
+
if prefer_cuda:
|
| 45 |
+
want = _pick_device()
|
| 46 |
+
if want != _device:
|
| 47 |
+
_model = _model.to(want)
|
| 48 |
+
_device = want
|
| 49 |
|
| 50 |
|
| 51 |
def status() -> dict[str, Any]:
|
requirements.txt
CHANGED
|
@@ -9,3 +9,4 @@ accelerate>=1.0.0
|
|
| 9 |
ctranslate2>=4.5.0
|
| 10 |
huggingface_hub>=0.26.0
|
| 11 |
gradio>=5.29.0
|
|
|
|
|
|
| 9 |
ctranslate2>=4.5.0
|
| 10 |
huggingface_hub>=0.26.0
|
| 11 |
gradio>=5.29.0
|
| 12 |
+
spaces>=0.30.0
|