From 4e19fe709ec80e79eb019d91cc5ee57b7f8822df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=BB=E4=B9=90=E5=A4=9A?= <3065215860@qq.com> Date: Thu, 19 Sep 2024 15:24:21 +0000 Subject: [PATCH] add optimal_knn_webapp_pinecone.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 养乐多 <3065215860@qq.com> --- optimal_knn_webapp_pinecone.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 optimal_knn_webapp_pinecone.py diff --git a/optimal_knn_webapp_pinecone.py b/optimal_knn_webapp_pinecone.py new file mode 100644 index 0000000..763c9ca --- /dev/null +++ b/optimal_knn_webapp_pinecone.py @@ -0,0 +1,33 @@ +import numpy as np +import pickle +import gradio as gr +from PIL import Image + +# 加载保存的KNN模型 +with open('best_knn_model.pkl', 'rb') as f: + knn = pickle.load(f) + +# 定义预测函数,这个函数将用于Gradio接口进行预测 +def preprocess(image): + # 将输入的NumPy数组转换为PIL图像对象 + image = Image.fromarray(image) + image = image.resize((8, 8)).convert('L') + image_array = np.array(image) + flattened_image = image_array.ravel() + return flattened_image + +def predict(image): + preprocessed_image = preprocess(image) + predicted_digit = knn.predict([preprocessed_image])[0] + return str(predicted_digit) + +# 创建Gradio接口,这个接口将用于用户输入和显示预测结果 +interface = gr.Interface( + fn=predict, + inputs=gr.Sketchpad(label="Image", type="numpy"), + outputs=gr.Label(label="Guess"), + live=True +) + +# 启动Gradio接口,用户可以通过这个接口进行交互 +interface.launch(share=True) \ No newline at end of file -- Gitee