Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,20 +5,36 @@ import torch
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
|
| 8 |
-
# β
Set a writable cache directory inside
|
| 9 |
-
os.environ["HF_HOME"] = "/
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
| 13 |
-
# β
Load the model with
|
| 14 |
model = AutoModelForImageClassification.from_pretrained(
|
| 15 |
"shahad-alh/arabichar-finetuned-v2",
|
| 16 |
trust_remote_code=True,
|
| 17 |
-
cache_dir=os.environ["HF_HOME"] # β
Fix: Store model inside /
|
| 18 |
)
|
| 19 |
|
| 20 |
processor = AutoProcessor.from_pretrained(
|
| 21 |
"shahad-alh/arabichar-finetuned-v2",
|
| 22 |
-
cache_dir=os.environ["HF_HOME"] # β
Fix: Store processor inside /
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
|
| 8 |
+
# β
Set a writable cache directory inside /tmp
|
| 9 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
| 13 |
+
# β
Load the model with the correct cache directory
|
| 14 |
model = AutoModelForImageClassification.from_pretrained(
|
| 15 |
"shahad-alh/arabichar-finetuned-v2",
|
| 16 |
trust_remote_code=True,
|
| 17 |
+
cache_dir=os.environ["HF_HOME"] # β
Fix: Store model inside /tmp
|
| 18 |
)
|
| 19 |
|
| 20 |
processor = AutoProcessor.from_pretrained(
|
| 21 |
"shahad-alh/arabichar-finetuned-v2",
|
| 22 |
+
cache_dir=os.environ["HF_HOME"] # β
Fix: Store processor inside /tmp
|
| 23 |
)
|
| 24 |
|
| 25 |
+
@app.route('/classify', methods=['POST'])
|
| 26 |
+
def classify():
|
| 27 |
+
if 'file' not in request.files:
|
| 28 |
+
return jsonify({"error": "No file uploaded"})
|
| 29 |
+
|
| 30 |
+
file = request.files['file']
|
| 31 |
+
image = Image.open(file.stream)
|
| 32 |
+
|
| 33 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 34 |
+
outputs = model(**inputs)
|
| 35 |
+
predicted_class = torch.argmax(outputs.logits, dim=-1).item()
|
| 36 |
+
|
| 37 |
+
return jsonify({"prediction": predicted_class})
|
| 38 |
+
|
| 39 |
+
if __name__ == '__main__':
|
| 40 |
+
app.run(host="0.0.0.0", port=7860)
|