chrohi commited on
Commit
b38c0cb
Β·
verified Β·
1 Parent(s): 7644b0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -3
app.py CHANGED
@@ -227,12 +227,10 @@ def create_summary(results, processing_time):
227
 
228
  summary = f"""
229
  ## πŸ“ˆ Analysis Summary
230
-
231
  **🎯 Overall Performance:**
232
  - **Confidence Level:** {confidence_level} ({confidence:.1%})
233
  - **Processing Speed:** {processing_time:.1f} seconds
234
  - **Total Entities Extracted:** {total_entities}
235
-
236
  **πŸ“Š Extraction Breakdown:**
237
  - **Reactants:** {len(data.get('reactants', []))}
238
  - **Products:** {len(data.get('products', []))}
@@ -240,7 +238,6 @@ def create_summary(results, processing_time):
240
  - **Solvents:** {len(data.get('solvents', []))}
241
  - **Conditions:** {len([v for v in data.get('conditions', {}).values() if v])}
242
  - **Workup Steps:** {len(data.get('workup', []))}
243
-
244
  **πŸ’‘ Quality Assessment:**
245
  {get_quality_assessment(confidence, total_entities)}
246
  """
@@ -279,3 +276,129 @@ Stir for 12 hours, then cool and filter through celite.
279
  Purify by column chromatography to obtain 0.85 g of biphenyl derivative (92% yield)."""
280
  ]
281
  return examples
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
  summary = f"""
229
  ## πŸ“ˆ Analysis Summary
 
230
  **🎯 Overall Performance:**
231
  - **Confidence Level:** {confidence_level} ({confidence:.1%})
232
  - **Processing Speed:** {processing_time:.1f} seconds
233
  - **Total Entities Extracted:** {total_entities}
 
234
  **πŸ“Š Extraction Breakdown:**
235
  - **Reactants:** {len(data.get('reactants', []))}
236
  - **Products:** {len(data.get('products', []))}
 
238
  - **Solvents:** {len(data.get('solvents', []))}
239
  - **Conditions:** {len([v for v in data.get('conditions', {}).values() if v])}
240
  - **Workup Steps:** {len(data.get('workup', []))}
 
241
  **πŸ’‘ Quality Assessment:**
242
  {get_quality_assessment(confidence, total_entities)}
243
  """
 
276
  Purify by column chromatography to obtain 0.85 g of biphenyl derivative (92% yield)."""
277
  ]
278
  return examples
279
+
280
+ # Create the Gradio interface
281
+ def create_interface():
282
+ """Create and return the Gradio interface"""
283
+
284
+ with gr.Blocks(title="RxNExtract - Chemical Reaction Extraction", theme=gr.themes.Soft()) as demo:
285
+
286
+ # Header
287
+ gr.Markdown("""
288
+ # πŸ§ͺ RxNExtract - Chemical Reaction Extraction
289
+
290
+ Extract chemical entities and reaction information from synthetic procedures using advanced NLP models.
291
+ """)
292
+
293
+ # Model loading section
294
+ with gr.Row():
295
+ with gr.Column(scale=2):
296
+ gr.Markdown("### πŸ€– Model Management")
297
+ load_btn = gr.Button("Load RxNExtract Model", variant="primary", size="lg")
298
+ model_status = gr.Textbox(
299
+ label="Model Status",
300
+ value="Model not loaded. Click 'Load RxNExtract Model' to initialize.",
301
+ interactive=False
302
+ )
303
+
304
+ # Main interface
305
+ with gr.Row():
306
+ with gr.Column(scale=1):
307
+ gr.Markdown("### πŸ“ Input")
308
+
309
+ procedure_input = gr.Textbox(
310
+ label="Chemical Procedure",
311
+ placeholder="Enter your chemical synthesis procedure here...",
312
+ lines=8,
313
+ max_lines=15
314
+ )
315
+
316
+ with gr.Row():
317
+ temperature_slider = gr.Slider(
318
+ minimum=0.0,
319
+ maximum=1.0,
320
+ value=0.1,
321
+ step=0.1,
322
+ label="Temperature (Model Creativity)",
323
+ info="Lower values = more focused, higher values = more creative"
324
+ )
325
+
326
+ with gr.Row():
327
+ analyze_btn = gr.Button("πŸ” Analyze Procedure", variant="primary", size="lg")
328
+ clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
329
+
330
+ # Example procedures
331
+ gr.Markdown("### πŸ“‹ Example Procedures")
332
+ examples = get_example_procedures()
333
+
334
+ for i, example in enumerate(examples, 1):
335
+ with gr.Accordion(f"Example {i}", open=False):
336
+ gr.Textbox(
337
+ value=example,
338
+ label=f"Example {i}",
339
+ lines=4,
340
+ interactive=False
341
+ )
342
+ gr.Button(f"Use Example {i}", size="sm").click(
343
+ fn=lambda ex=example: ex,
344
+ outputs=procedure_input
345
+ )
346
+
347
+ with gr.Column(scale=2):
348
+ gr.Markdown("### πŸ“Š Results")
349
+
350
+ # Summary tab
351
+ with gr.Tabs():
352
+ with gr.TabItem("πŸ“ˆ Summary"):
353
+ summary_output = gr.Markdown()
354
+
355
+ with gr.TabItem("πŸ“‹ Detailed Results"):
356
+ detailed_output = gr.Markdown()
357
+
358
+ with gr.TabItem("πŸ“Š Entity Visualization"):
359
+ entity_plot = gr.Plot()
360
+
361
+ with gr.TabItem("🎯 Confidence & Timing"):
362
+ confidence_plot = gr.Plot()
363
+
364
+ # Event handlers
365
+ load_btn.click(
366
+ fn=load_model,
367
+ outputs=model_status
368
+ )
369
+
370
+ analyze_btn.click(
371
+ fn=analyze_procedure,
372
+ inputs=[procedure_input, gr.State(True), temperature_slider],
373
+ outputs=[summary_output, detailed_output, entity_plot, confidence_plot]
374
+ )
375
+
376
+ clear_btn.click(
377
+ fn=lambda: ("", "", "", "", ""),
378
+ outputs=[procedure_input, summary_output, detailed_output, entity_plot, confidence_plot]
379
+ )
380
+
381
+ # Footer
382
+ gr.Markdown("""
383
+ ---
384
+ **About RxNExtract:** This tool uses advanced natural language processing to extract chemical entities,
385
+ reaction conditions, and procedural information from synthetic chemistry procedures.
386
+
387
+ **Powered by:** Hugging Face Transformers, Gradio, and the ChemPlusX team.
388
+ """)
389
+
390
+ return demo
391
+
392
+ # Main execution
393
+ if __name__ == "__main__":
394
+ # Create and launch the interface
395
+ demo = create_interface()
396
+
397
+ # Launch with appropriate settings for Hugging Face Spaces
398
+ demo.launch(
399
+ server_name="0.0.0.0", # Required for Hugging Face Spaces
400
+ server_port=7860, # Default port for Hugging Face Spaces
401
+ share=False, # Don't create public links
402
+ debug=False, # Disable debug mode in production
403
+ show_error=True # Show errors in the interface
404
+ )