Spaces:
Running on Zero
Running on Zero
PabsCanCode commited on
Commit ·
1f94056
1
Parent(s): 2aa2ea0
Run Gradio inference on free CPU Basic without CUDA
Browse files- README.md +2 -1
- app.py +2 -21
- requirements.txt +1 -2
README.md
CHANGED
|
@@ -14,7 +14,8 @@ preload_from_hub:
|
|
| 14 |
# Rice Disease Detection
|
| 15 |
|
| 16 |
Upload a close-up rice leaf photo to identify disease and view treatment guidance.
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
Named Gradio endpoints: `/predict` (one uploaded file, JSON result) and `/diseases` (no inputs, JSON catalog).
|
| 20 |
Non-rice and invalid uploads return a JSON `detail` field. This is a Gradio API, not the FastAPI `/predict` route.
|
|
|
|
| 14 |
# Rice Disease Detection
|
| 15 |
|
| 16 |
Upload a close-up rice leaf photo to identify disease and view treatment guidance.
|
| 17 |
+
Select CPU Basic (Free) in Space Settings. Both models run on CPU; no GPU is required.
|
| 18 |
+
Requests are processed one at a time, and the Space may sleep when idle.
|
| 19 |
|
| 20 |
Named Gradio endpoints: `/predict` (one uploaded file, JSON result) and `/diseases` (no inputs, JSON catalog).
|
| 21 |
Non-rice and invalid uploads return a JSON `detail` field. This is a Gradio API, not the FastAPI `/predict` route.
|
app.py
CHANGED
|
@@ -1,31 +1,12 @@
|
|
| 1 |
-
"""Gradio entry point for
|
| 2 |
|
| 3 |
import io
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# Availability checks must not initialize the CUDA runtime in the parent process.
|
| 7 |
-
os.environ.setdefault("PYTORCH_NVML_BASED_CUDA_CHECK", "1")
|
| 8 |
-
|
| 9 |
-
# Import spaces before torch/transformers so ZeroGPU can enable CUDA emulation.
|
| 10 |
-
import spaces
|
| 11 |
import gradio as gr
|
| 12 |
-
import torch
|
| 13 |
from fastapi import HTTPException
|
| 14 |
from PIL import Image, ImageOps, UnidentifiedImageError
|
| 15 |
|
| 16 |
import main as backend
|
| 17 |
|
| 18 |
-
# Precomputed text embeddings are created on CPU by main, then moved with the models.
|
| 19 |
-
# ZeroGPU requires CUDA placement at module scope, outside the decorated function.
|
| 20 |
-
backend.disease_clf.model.to("cuda")
|
| 21 |
-
backend.disease_clf.device = torch.device("cuda")
|
| 22 |
-
backend.gate_model.to("cuda")
|
| 23 |
-
backend.gate_text = backend.gate_text.to("cuda")
|
| 24 |
-
backend.vocab_text = backend.vocab_text.to("cuda")
|
| 25 |
-
backend.gate_scale = backend.gate_scale.to("cuda")
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
@spaces.GPU(duration=20)
|
| 29 |
def infer(image):
|
| 30 |
try:
|
| 31 |
return backend.classify_image(image)
|
|
@@ -46,7 +27,7 @@ def predict(file):
|
|
| 46 |
return {"detail": "File is not a valid image."}
|
| 47 |
result = infer(image)
|
| 48 |
if "detail" not in result:
|
| 49 |
-
# Persist only accepted scans
|
| 50 |
result["storage"] = backend.store_image(image, result)
|
| 51 |
return result
|
| 52 |
|
|
|
|
| 1 |
+
"""Gradio entry point for CPU Basic hardware (upload as app.py)."""
|
| 2 |
|
| 3 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
from fastapi import HTTPException
|
| 6 |
from PIL import Image, ImageOps, UnidentifiedImageError
|
| 7 |
|
| 8 |
import main as backend
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def infer(image):
|
| 11 |
try:
|
| 12 |
return backend.classify_image(image)
|
|
|
|
| 27 |
return {"detail": "File is not a valid image."}
|
| 28 |
result = infer(image)
|
| 29 |
if "detail" not in result:
|
| 30 |
+
# Persist only accepted scans.
|
| 31 |
result["storage"] = backend.store_image(image, result)
|
| 32 |
return result
|
| 33 |
|
requirements.txt
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
-
#
|
| 2 |
torch==2.13.0
|
| 3 |
transformers==5.22.2
|
| 4 |
gradio>=6,<7
|
| 5 |
-
spaces
|
| 6 |
fastapi==0.162.1
|
| 7 |
python-multipart==0.0.33
|
| 8 |
# Gradio 6 requires Pillow below 13.
|
|
|
|
| 1 |
+
# CPU inference; no ZeroGPU allocation or spaces decorator is needed.
|
| 2 |
torch==2.13.0
|
| 3 |
transformers==5.22.2
|
| 4 |
gradio>=6,<7
|
|
|
|
| 5 |
fastapi==0.162.1
|
| 6 |
python-multipart==0.0.33
|
| 7 |
# Gradio 6 requires Pillow below 13.
|