Spaces:
Sleeping
Sleeping
freeball0328
commited on
Commit
•
1f74b34
1
Parent(s):
59e267e
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,48 @@
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
#
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.all import *
|
2 |
+
from io import BytesIO
|
3 |
+
import requests
|
4 |
import streamlit as st
|
5 |
|
6 |
+
# 标题和应用描述
|
7 |
+
st.title("HeartNet")
|
8 |
+
st.write("This is a classifier for images of 12-lead EKGs. It will attempt to detect whether the EKG indicates an acute MI. It was trained on simulated images.")
|
9 |
+
|
10 |
+
# 展示示例图片
|
11 |
+
st.write("## Example of an EKG image to upload")
|
12 |
+
st.image("exwind.jpg", caption="Example EKG Image", use_column_width=True)
|
13 |
+
|
14 |
+
def predict(img):
|
15 |
+
st.image(img, caption="Your image", use_column_width=True)
|
16 |
+
pred, key, probs = learn_inf.predict(img)
|
17 |
+
|
18 |
+
result_message = f"""
|
19 |
+
## This **{'is ' if pred == 'mi' else 'is not'}** an MI (heart attack).
|
20 |
+
### Prediction result: {pred}
|
21 |
+
### Probability of {pred}: {probs[key].item()*100: .2f}%
|
22 |
+
"""
|
23 |
+
st.write(result_message)
|
24 |
+
|
25 |
+
# 加载模型
|
26 |
+
path = "./"
|
27 |
+
learn_inf = load_learner(path + "resnet34_stage_4.pkl")
|
28 |
+
|
29 |
+
# 用户上传图片或提供图片 URL
|
30 |
+
option = st.radio("", ["Upload Image", "Image URL"])
|
31 |
+
|
32 |
+
if option == "Upload Image":
|
33 |
+
uploaded_file = st.file_uploader("Please upload an image.")
|
34 |
+
|
35 |
+
if uploaded_file is not None:
|
36 |
+
img = PILImage.create(uploaded_file)
|
37 |
+
predict(img)
|
38 |
+
|
39 |
+
else:
|
40 |
+
url = st.text_input("Please input a URL for an image.")
|
41 |
+
|
42 |
+
if url != "":
|
43 |
+
try:
|
44 |
+
response = requests.get(url)
|
45 |
+
pil_img = PILImage.create(BytesIO(response.content))
|
46 |
+
predict(pil_img)
|
47 |
+
except Exception as e:
|
48 |
+
st.error(f"Problem reading image from the URL. Please check the URL and try again. Error: {str(e)}")
|