Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 trust_remote_code=True
|
| 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 |
+
# ✅ Run inference
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**inputs)
|
| 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)
|