Update app.py
Browse files
app.py
CHANGED
|
@@ -19,9 +19,8 @@ def visualize_molecule(smiles):
|
|
| 19 |
except:
|
| 20 |
return None
|
| 21 |
|
| 22 |
-
def
|
| 23 |
try:
|
| 24 |
-
# 检查图片输入
|
| 25 |
if image is None:
|
| 26 |
logger.error("上传的图片为空")
|
| 27 |
return "错误: 请上传有效的图片文件"
|
|
@@ -32,22 +31,19 @@ def gradio_predict(image):
|
|
| 32 |
logger.error(f"工作目录不存在或不是目录: {work_dir}")
|
| 33 |
return f"错误: 工作目录无效 - {work_dir}"
|
| 34 |
|
| 35 |
-
# 使用 /home/user/app 保存临时文件
|
| 36 |
with tempfile.NamedTemporaryFile(suffix=".png", dir=work_dir, delete=False) as temp_file:
|
| 37 |
temp_path = os.path.abspath(temp_file.name)
|
| 38 |
logger.info(f"保存图片到: {temp_path}, 类型: {type(temp_path)}")
|
| 39 |
image.save(temp_path)
|
| 40 |
|
| 41 |
-
# 验证文件存在
|
| 42 |
if not os.path.exists(temp_path):
|
| 43 |
logger.error(f"图片文件未正确保存: {temp_path}")
|
| 44 |
return f"错误: 图片文件未正确保存 - {temp_path}"
|
| 45 |
|
| 46 |
-
# 调用模型的 main 函数
|
| 47 |
logger.info(f"传递给 main 的路径: {temp_path}")
|
| 48 |
smiles = predict_smiles([temp_path])
|
| 49 |
mol_image = visualize_molecule(smiles)
|
| 50 |
-
|
| 51 |
try:
|
| 52 |
os.remove(temp_path)
|
| 53 |
logger.info(f"已删除临时文件: {temp_path}")
|
|
@@ -74,15 +70,17 @@ def gradio_predict(image):
|
|
| 74 |
|
| 75 |
# 定义 Gradio 界面
|
| 76 |
iface = gr.Interface(
|
| 77 |
-
fn=
|
| 78 |
-
inputs=gr.Image(type="pil"),
|
| 79 |
-
# outputs=gr.Textbox(),
|
| 80 |
outputs=[
|
| 81 |
-
gr.Image(label="
|
| 82 |
gr.Text(label="SMILES"),
|
| 83 |
],
|
| 84 |
-
title="
|
| 85 |
-
description="
|
|
|
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
|
|
|
| 19 |
except:
|
| 20 |
return None
|
| 21 |
|
| 22 |
+
def predict(image):
|
| 23 |
try:
|
|
|
|
| 24 |
if image is None:
|
| 25 |
logger.error("上传的图片为空")
|
| 26 |
return "错误: 请上传有效的图片文件"
|
|
|
|
| 31 |
logger.error(f"工作目录不存在或不是目录: {work_dir}")
|
| 32 |
return f"错误: 工作目录无效 - {work_dir}"
|
| 33 |
|
|
|
|
| 34 |
with tempfile.NamedTemporaryFile(suffix=".png", dir=work_dir, delete=False) as temp_file:
|
| 35 |
temp_path = os.path.abspath(temp_file.name)
|
| 36 |
logger.info(f"保存图片到: {temp_path}, 类型: {type(temp_path)}")
|
| 37 |
image.save(temp_path)
|
| 38 |
|
|
|
|
| 39 |
if not os.path.exists(temp_path):
|
| 40 |
logger.error(f"图片文件未正确保存: {temp_path}")
|
| 41 |
return f"错误: 图片文件未正确保存 - {temp_path}"
|
| 42 |
|
|
|
|
| 43 |
logger.info(f"传递给 main 的路径: {temp_path}")
|
| 44 |
smiles = predict_smiles([temp_path])
|
| 45 |
mol_image = visualize_molecule(smiles)
|
| 46 |
+
|
| 47 |
try:
|
| 48 |
os.remove(temp_path)
|
| 49 |
logger.info(f"已删除临时文件: {temp_path}")
|
|
|
|
| 70 |
|
| 71 |
# 定义 Gradio 界面
|
| 72 |
iface = gr.Interface(
|
| 73 |
+
fn=predict,
|
| 74 |
+
inputs=gr.Image(label="Upload molecular image", type="pil"),
|
|
|
|
| 75 |
outputs=[
|
| 76 |
+
gr.Image(label="Mol Image"),
|
| 77 |
gr.Text(label="SMILES"),
|
| 78 |
],
|
| 79 |
+
title="Optical Chemical Structure Recognition",
|
| 80 |
+
description="Convert a molecular image into SMILES.<br> ",
|
| 81 |
+
examples=[
|
| 82 |
+
["example.png"]
|
| 83 |
+
]
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|