Joodson commited on
Commit
3dd0b53
Β·
verified Β·
1 Parent(s): de2955f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -5
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 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)