Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import streamlit as st
|
2 |
from flask import Flask, request, jsonify
|
3 |
from flask_cors import CORS
|
4 |
from transformers import AutoModelForImageClassification, AutoProcessor
|
@@ -6,12 +5,8 @@ from PIL import Image
|
|
6 |
import io
|
7 |
import fitz
|
8 |
import torch
|
9 |
-
import threading
|
10 |
-
import requests
|
11 |
import os
|
12 |
|
13 |
-
HF_SPACE_URL = os.environ.get("HF_SPACE_URL")
|
14 |
-
|
15 |
app = Flask(__name__)
|
16 |
CORS(app)
|
17 |
|
@@ -36,9 +31,8 @@ def pdf_to_images_pymupdf(pdf_data):
|
|
36 |
@app.route('/classify', methods=['POST', 'OPTIONS'])
|
37 |
def classify_file():
|
38 |
if request.method == 'OPTIONS':
|
39 |
-
# Respond to preflight request
|
40 |
response = jsonify()
|
41 |
-
response.headers.add('Access-Control-Allow-Origin', '*')
|
42 |
response.headers.add('Access-Control-Allow-Methods', 'POST, OPTIONS')
|
43 |
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
|
44 |
return response
|
@@ -64,7 +58,6 @@ def classify_file():
|
|
64 |
elif file_type == 'pdf':
|
65 |
pdf_data = uploaded_file.read()
|
66 |
images = pdf_to_images_pymupdf(pdf_data)
|
67 |
-
|
68 |
if images:
|
69 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
70 |
inputs = processor(images=image, return_tensors="pt")
|
@@ -76,41 +69,11 @@ def classify_file():
|
|
76 |
return jsonify({'result': result})
|
77 |
else:
|
78 |
return jsonify({'error': 'PDF conversion failed.'}), 500
|
79 |
-
|
80 |
else:
|
81 |
return jsonify({'error': 'Unsupported file type'}), 400
|
82 |
|
83 |
except Exception as e:
|
84 |
return jsonify({'error': f'An error occurred: {e}'}), 500
|
85 |
|
86 |
-
def run_flask_app():
|
87 |
-
app.run(host='0.0.0.0', port=5000, debug=False, use_reloader=False)
|
88 |
-
|
89 |
-
def main():
|
90 |
-
st.title("RetinApp Backend Tester")
|
91 |
-
st.write("Upload an image or PDF to test the /classify endpoint.")
|
92 |
-
|
93 |
-
uploaded_file = st.file_uploader("Choose a file...", type=["jpg", "jpeg", "png", "gif", "pdf"])
|
94 |
-
|
95 |
-
if uploaded_file is not None:
|
96 |
-
st.write("Uploaded File:", uploaded_file.name)
|
97 |
-
|
98 |
-
if st.button("Classify"):
|
99 |
-
files = {"file": uploaded_file}
|
100 |
-
try:
|
101 |
-
api_url = f"{HF_SPACE_URL}/classify"
|
102 |
-
response = requests.post(api_url, files=files)
|
103 |
-
response.raise_for_status()
|
104 |
-
result = response.json()
|
105 |
-
st.success(f"Classification Result: {result['result']}")
|
106 |
-
except requests.exceptions.RequestException as e:
|
107 |
-
st.error(f"Error during classification: {e}")
|
108 |
-
except Exception as e:
|
109 |
-
st.error(f"Error processing response: {e}")
|
110 |
-
|
111 |
-
flask_thread = threading.Thread(target=run_flask_app)
|
112 |
-
flask_thread.daemon = True
|
113 |
-
flask_thread.start()
|
114 |
-
|
115 |
if __name__ == '__main__':
|
116 |
-
|
|
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
from flask_cors import CORS
|
3 |
from transformers import AutoModelForImageClassification, AutoProcessor
|
|
|
5 |
import io
|
6 |
import fitz
|
7 |
import torch
|
|
|
|
|
8 |
import os
|
9 |
|
|
|
|
|
10 |
app = Flask(__name__)
|
11 |
CORS(app)
|
12 |
|
|
|
31 |
@app.route('/classify', methods=['POST', 'OPTIONS'])
|
32 |
def classify_file():
|
33 |
if request.method == 'OPTIONS':
|
|
|
34 |
response = jsonify()
|
35 |
+
response.headers.add('Access-Control-Allow-Origin', '*')
|
36 |
response.headers.add('Access-Control-Allow-Methods', 'POST, OPTIONS')
|
37 |
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
|
38 |
return response
|
|
|
58 |
elif file_type == 'pdf':
|
59 |
pdf_data = uploaded_file.read()
|
60 |
images = pdf_to_images_pymupdf(pdf_data)
|
|
|
61 |
if images:
|
62 |
image = Image.open(io.BytesIO(images[0])).convert("RGB")
|
63 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
69 |
return jsonify({'result': result})
|
70 |
else:
|
71 |
return jsonify({'error': 'PDF conversion failed.'}), 500
|
|
|
72 |
else:
|
73 |
return jsonify({'error': 'Unsupported file type'}), 400
|
74 |
|
75 |
except Exception as e:
|
76 |
return jsonify({'error': f'An error occurred: {e}'}), 500
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
if __name__ == '__main__':
|
79 |
+
app.run(host='0.0.0.0', port=7860)
|