from fastai.vision.all import * from io import BytesIO import requests import streamlit as st # 应用标题 st.title("能源類型分類器") st.write("這是一個可以分類不同能源形式(如風能、太陽能、水能)的分類器。") # 展示示例图片 st.write("## 範例圖") st.image("exwind.jpg", caption="Example EKG Image", use_column_width=True) def predict(img): st.image(img, caption="Your image", use_column_width=True) pred, key, probs = learn_inf.predict(img) result_message = f""" ### Prediction result: {pred} ### Probability of {pred}: {probs[key].item()*100: .2f}% """ st.write(result_message) # 加载模型 path = "./" learn_inf = load_learner(path + "resnet34_stage_4.pkl") # 用户上传图片或提供图片 URL option = st.radio("", ["Upload Image", "Image URL"]) if option == "Upload Image": uploaded_file = st.file_uploader("Please upload an image.") if uploaded_file is not None: img = PILImage.create(uploaded_file) predict(img) else: url = st.text_input("Please input a URL for an image.") if url != "": try: response = requests.get(url) pil_img = PILImage.create(BytesIO(response.content)) predict(pil_img) except Exception as e: st.error(f"Problem reading image from the URL. Please check the URL and try again. Error: {str(e)}")