Songyou commited on
Commit
14e32f4
·
verified ·
1 Parent(s): d7d65ed

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +32 -2
main.py CHANGED
@@ -3,11 +3,14 @@ from pydantic import BaseModel
3
  import subprocess
4
  from typing import List
5
  from fragment_processor import fragmentize_molecule
6
-
7
  import torch
8
  import pandas as pd
9
  from rdkit import Chem
10
- from rdkit.Chem import Descriptors, QED
 
 
 
11
  from generate import GenerateRunner
12
  from dataset import Dataset
13
  from combine_mol import connect_constVar_try
@@ -147,6 +150,33 @@ def run_generate_runner(const_smiles, var_smiles, main_cls, minor_cls, delta_val
147
  return result
148
 
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  @app.get("/fragmentize", response_model=FragmentResponse)
151
  async def fragmentize(smiles: str = Query(..., description="SMILES string of the molecule")):
152
  try:
 
3
  import subprocess
4
  from typing import List
5
  from fragment_processor import fragmentize_molecule
6
+ from fastapi.responses import StreamingResponse
7
  import torch
8
  import pandas as pd
9
  from rdkit import Chem
10
+ from rdkit.Chem import Descriptors, QED, Draw
11
+ from io import BytesIO
12
+ from PIL import Image
13
+ import io
14
  from generate import GenerateRunner
15
  from dataset import Dataset
16
  from combine_mol import connect_constVar_try
 
150
  return result
151
 
152
 
153
+ @app.get("/smiles2img")
154
+ async def smiles2img(smiles: str = Query(..., description="SMILES string of the molecule")):
155
+
156
+ # 生成分子对象
157
+ print("---开始生成分子图像--")
158
+ print(smiles)
159
+ mol = Chem.MolFromSmiles(smiles)
160
+ if mol is None:
161
+ return {"error": "Invalid SMILES string"}
162
+
163
+ # 创建绘图对象
164
+ drawer = Draw.MolDraw2DCairo(200, 200)
165
+ drawer.SetFontSize(1.0)
166
+ drawer.DrawMolecule(mol)
167
+ drawer.FinishDrawing()
168
+
169
+ # 将绘制的图像转换为PIL图像对象
170
+ pil_image = Image.open(io.BytesIO(drawer.GetDrawingText()))
171
+
172
+ # 创建字节流
173
+ strIO = BytesIO()
174
+ pil_image.save(strIO, "PNG")
175
+ strIO.seek(0)
176
+
177
+ # 返回图像作为流
178
+ return StreamingResponse(strIO, media_type="image/png")
179
+
180
  @app.get("/fragmentize", response_model=FragmentResponse)
181
  async def fragmentize(smiles: str = Query(..., description="SMILES string of the molecule")):
182
  try: