Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request
|
2 |
+
from transformers import AutoModelForImageClassification
|
3 |
+
from transformers import AutoImageProcessor
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
model = AutoModelForImageClassification.from_pretrained(
|
10 |
+
'../apiModel/myModel')
|
11 |
+
image_processor = AutoImageProcessor.from_pretrained(
|
12 |
+
"google/vit-base-patch16-224-in21k")
|
13 |
+
|
14 |
+
|
15 |
+
@app.route('/upload_image', methods=['POST'])
|
16 |
+
def upload_image():
|
17 |
+
# Get the image file from the request
|
18 |
+
image_file = request.files['image']
|
19 |
+
|
20 |
+
# Save the image file to a desired location on the server
|
21 |
+
image_path = "assets/img.jpg"
|
22 |
+
image_file.save(image_path)
|
23 |
+
|
24 |
+
# You can perform additional operations with the image here
|
25 |
+
# ...
|
26 |
+
|
27 |
+
return 'Image uploaded successfully'
|
28 |
+
|
29 |
+
|
30 |
+
@app.route('/get_text', methods=['GET'])
|
31 |
+
def get_text():
|
32 |
+
image = Image.open('assets/img.jpg')
|
33 |
+
inputs = image_processor(image, return_tensors="pt")
|
34 |
+
|
35 |
+
with torch.no_grad():
|
36 |
+
logits = model(**inputs).logits
|
37 |
+
|
38 |
+
predicted_label = logits.argmax(-1).item()
|
39 |
+
|
40 |
+
disease = model.config.id2label[predicted_label]
|
41 |
+
|
42 |
+
return disease
|
43 |
+
|
44 |
+
|
45 |
+
if __name__ == '__app__':
|
46 |
+
app.run(host='192.168.1.7', port=5000)
|