L3ul commited on
Commit
1f4d8b3
·
verified ·
1 Parent(s): 0102396

Add error handling to process_chem_image for debugging

Browse files
Files changed (1) hide show
  1. app.py +38 -27
app.py CHANGED
@@ -103,33 +103,44 @@ def format_reactions_html(reactions_data):
103
 
104
 
105
  def process_chem_image(image, selected_task):
106
- image_path = "temp_image.png"
107
- image.save(image_path)
108
-
109
- # Run local model (MolScribe + OCR) — no external API needed
110
- predictions = model.predict_image_file(image_path, molscribe=True, ocr=True)
111
-
112
- # Format predictions into structured data
113
- reactions_data, smiles_list = format_local_predictions(predictions)
114
-
115
- # Generate HTML output
116
- detailed_reactions = format_reactions_html(reactions_data)
117
-
118
- # Generate visualization
119
- combined_image_path = generate_combined_image(predictions, image_path)
120
-
121
- # Save JSON
122
- json_file_path = "output.json"
123
- with open(json_file_path, "w") as f:
124
- json.dump(reactions_data, f, indent=4)
125
-
126
- return (
127
- "\n\n".join(detailed_reactions),
128
- "\n".join(smiles_list),
129
- combined_image_path,
130
- example_diagram,
131
- json_file_path,
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()