Update app.py
Browse files
app.py
CHANGED
|
@@ -10,24 +10,56 @@ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
| 10 |
from ONNX0630 import main as predict_smiles
|
| 11 |
from PIL import Image
|
| 12 |
import io
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def gradio_predict(image):
|
| 17 |
try:
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
smiles = predict_smiles(temp_path)
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
os.remove(temp_path)
|
| 27 |
-
|
|
|
|
| 28 |
return smiles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
# Define Gradio interface
|
| 33 |
iface = gr.Interface(
|
|
|
|
| 10 |
from ONNX0630 import main as predict_smiles
|
| 11 |
from PIL import Image
|
| 12 |
import io
|
| 13 |
+
import tempfile
|
| 14 |
+
import logging
|
| 15 |
|
| 16 |
+
# 设置日志
|
| 17 |
+
logging.basicConfig(level=logging.INFO)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
# Gradio interface
|
| 21 |
+
# def gradio_predict(image):
|
| 22 |
+
# try:
|
| 23 |
+
# # Save the uploaded image
|
| 24 |
+
# temp_path = "temp_image.png"
|
| 25 |
+
# image.save(temp_path)
|
| 26 |
+
|
| 27 |
+
# # Call the model function
|
| 28 |
+
# smiles = predict_smiles(temp_path)
|
| 29 |
+
|
| 30 |
+
# # Clean up
|
| 31 |
+
# os.remove(temp_path)
|
| 32 |
+
|
| 33 |
+
# return smiles
|
| 34 |
+
# except Exception as e:
|
| 35 |
+
# return f"Error: {str(e)}"
|
| 36 |
+
|
| 37 |
def gradio_predict(image):
|
| 38 |
try:
|
| 39 |
+
# 使用临时文件目录保存图片
|
| 40 |
+
with tempfile.NamedTemporaryFile(suffix=".png", dir="/app", delete=False) as temp_file:
|
| 41 |
+
temp_path = temp_file.name
|
| 42 |
+
image.save(temp_path)
|
| 43 |
+
logger.info(f"图片已保存至: {temp_path}")
|
| 44 |
+
|
| 45 |
+
# 调用模型的 main 函数
|
| 46 |
smiles = predict_smiles(temp_path)
|
| 47 |
+
logger.info(f"SMILES 输出: {smiles}")
|
| 48 |
+
|
| 49 |
+
# 清理临时文件
|
| 50 |
os.remove(temp_path)
|
| 51 |
+
logger.info(f"已删除临时文件: {temp_path}")
|
| 52 |
+
|
| 53 |
return smiles
|
| 54 |
+
except FileNotFoundError as e:
|
| 55 |
+
logger.error(f"文件路径错误: {e}")
|
| 56 |
+
return f"错误: 文件路径问题 - {str(e)}"
|
| 57 |
+
except PermissionError as e:
|
| 58 |
+
logger.error(f"权限错误: {e}")
|
| 59 |
+
return f"错误: 文件权限问题 - {str(e)}"
|
| 60 |
except Exception as e:
|
| 61 |
+
logger.error(f"处理错误: {e}")
|
| 62 |
+
return f"错误: {str(e)}"
|
| 63 |
|
| 64 |
# Define Gradio interface
|
| 65 |
iface = gr.Interface(
|