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