Spaces:
Running
Running
Vaishnav14220
commited on
Commit
·
146b4ff
1
Parent(s):
a49be2e
Add reaction SVG images to PDF alongside reaction details
Browse files- app.py +24 -1
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -10,7 +10,8 @@ import os
|
|
| 10 |
from reportlab.pdfgen import canvas
|
| 11 |
from reportlab.lib.pagesizes import letter
|
| 12 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 13 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
|
|
|
| 14 |
|
| 15 |
# Load dataset
|
| 16 |
ds = load_dataset("smitathkr1/organic_reactions_enhanced")
|
|
@@ -68,6 +69,28 @@ def generate_all_reactions_pdf():
|
|
| 68 |
reaction_title = f"Reaction {idx+1}: {row['name']}"
|
| 69 |
story.append(Paragraph(reaction_title, styles['Heading2']))
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Handle potential None values in SMILES
|
| 72 |
reactants_smiles = [s for s in row['reactants_smiles'] if s is not None]
|
| 73 |
reagents_smiles = [s for s in row['reagents_smiles'] if s is not None]
|
|
|
|
| 10 |
from reportlab.pdfgen import canvas
|
| 11 |
from reportlab.lib.pagesizes import letter
|
| 12 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 13 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
|
| 14 |
+
from svglib.svglib import svg2rlg
|
| 15 |
|
| 16 |
# Load dataset
|
| 17 |
ds = load_dataset("smitathkr1/organic_reactions_enhanced")
|
|
|
|
| 69 |
reaction_title = f"Reaction {idx+1}: {row['name']}"
|
| 70 |
story.append(Paragraph(reaction_title, styles['Heading2']))
|
| 71 |
|
| 72 |
+
# Generate SVG for this reaction
|
| 73 |
+
try:
|
| 74 |
+
reactants_smiles = '.'.join([s for s in row['reactants_smiles'] if s is not None])
|
| 75 |
+
products_smiles = '.'.join([s for s in row['products_smiles'] if s is not None])
|
| 76 |
+
if reactants_smiles and products_smiles:
|
| 77 |
+
reaction_smiles = f"{reactants_smiles}>>{products_smiles}"
|
| 78 |
+
rxn = AllChem.ReactionFromSmarts(reaction_smiles)
|
| 79 |
+
if rxn:
|
| 80 |
+
svg_content = Draw.ReactionToImage(rxn, useSVG=True)
|
| 81 |
+
# Convert SVG to ReportLab drawing
|
| 82 |
+
drawing = svg2rlg(io.StringIO(svg_content))
|
| 83 |
+
if drawing:
|
| 84 |
+
# Scale the drawing to fit
|
| 85 |
+
drawing.width = 400
|
| 86 |
+
drawing.height = 200
|
| 87 |
+
drawing.scale(0.5, 0.5)
|
| 88 |
+
story.append(drawing)
|
| 89 |
+
story.append(Spacer(1, 12))
|
| 90 |
+
except Exception as e:
|
| 91 |
+
# If SVG generation fails, just continue
|
| 92 |
+
pass
|
| 93 |
+
|
| 94 |
# Handle potential None values in SMILES
|
| 95 |
reactants_smiles = [s for s in row['reactants_smiles'] if s is not None]
|
| 96 |
reagents_smiles = [s for s in row['reagents_smiles'] if s is not None]
|
requirements.txt
CHANGED
|
@@ -4,4 +4,5 @@ gradio
|
|
| 4 |
fuzzywuzzy
|
| 5 |
python-levenshtein
|
| 6 |
rdkit
|
| 7 |
-
reportlab
|
|
|
|
|
|
| 4 |
fuzzywuzzy
|
| 5 |
python-levenshtein
|
| 6 |
rdkit
|
| 7 |
+
reportlab
|
| 8 |
+
svglib
|