Add error handling to process_chem_image for debugging
Browse files
app.py
CHANGED
|
@@ -103,33 +103,44 @@ def format_reactions_html(reactions_data):
|
|
| 103 |
|
| 104 |
|
| 105 |
def process_chem_image(image, selected_task):
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
prompts_with_names = list_prompt_files_with_names()
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
def process_chem_image(image, selected_task):
|
| 106 |
+
import traceback
|
| 107 |
+
try:
|
| 108 |
+
image_path = "temp_image.png"
|
| 109 |
+
if image is None:
|
| 110 |
+
raise ValueError("No image provided")
|
| 111 |
+
image.save(image_path)
|
| 112 |
+
|
| 113 |
+
# Run local model (MolScribe + OCR) — no external API needed
|
| 114 |
+
predictions = model.predict_image_file(image_path, molscribe=True, ocr=True)
|
| 115 |
+
|
| 116 |
+
# Format predictions into structured data
|
| 117 |
+
reactions_data, smiles_list = format_local_predictions(predictions)
|
| 118 |
+
|
| 119 |
+
# Generate HTML output
|
| 120 |
+
detailed_reactions = format_reactions_html(reactions_data)
|
| 121 |
+
|
| 122 |
+
# Generate visualization
|
| 123 |
+
try:
|
| 124 |
+
combined_image_path = generate_combined_image(predictions, image_path)
|
| 125 |
+
except Exception:
|
| 126 |
+
combined_image_path = None
|
| 127 |
+
|
| 128 |
+
# Save JSON
|
| 129 |
+
json_file_path = "output.json"
|
| 130 |
+
with open(json_file_path, "w") as f:
|
| 131 |
+
json.dump(reactions_data, f, indent=4)
|
| 132 |
+
|
| 133 |
+
return (
|
| 134 |
+
"\n\n".join(detailed_reactions),
|
| 135 |
+
"\n".join(smiles_list),
|
| 136 |
+
combined_image_path,
|
| 137 |
+
example_diagram,
|
| 138 |
+
json_file_path,
|
| 139 |
+
)
|
| 140 |
+
except Exception as e:
|
| 141 |
+
tb = traceback.format_exc()
|
| 142 |
+
error_html = f"<b>Error:</b> {str(e)}<br><pre>{tb}</pre>"
|
| 143 |
+
return (error_html, "", None, example_diagram, None)
|
| 144 |
|
| 145 |
|
| 146 |
prompts_with_names = list_prompt_files_with_names()
|