Skip to content

Commit

Permalink
feat: update requirements, stop installing dependencies twice and add…
Browse files Browse the repository at this point in the history
…ed decimer segmentation
  • Loading branch information
Kohulan committed Mar 13, 2023
1 parent d484b9d commit 096dc67
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ WORKDIR /code
RUN python3 -m pip install -U pip

COPY ./requirements.txt /code/requirements.txt

RUN pip3 install --upgrade setuptools pip
RUN pip3 install --no-cache-dir --upgrade -r /code/requirements.txt
RUN pip3 install --no-deps decimer-segmentation
RUN pip3 install --no-deps decimer>=2.2.0
RUN pip3 install --no-deps STOUT-pypi>=2.0.5

RUN python3 -m pip uninstall -y uvicorn

Expand Down
28 changes: 28 additions & 0 deletions app/modules/decimermodules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import cv2
from decimer_segmentation import segment_chemical_structures_from_file
from DECIMER import predict_SMILES

def getPredictedSegments(path:str):
"""Takes an image filepath and returns a set of paths of segmented images
Args:
input_path (str): path of an image
Returns:
segment_paths (list): a list of paths of segmented images.
"""
smiles_predicted = []
image_name = os.path.split(path)[1]
segments = segment_chemical_structures_from_file(path)
if len(segments) == 0:
smiles = predict_SMILES(path)
return smiles
else:
for segment_index in range(len(segments)):
segmentname = f"{image_name[:-5]}_{segment_index}.png"
segment_path = os.path.join(segmentname)
cv2.imwrite(segment_path, segments[segment_index])
smiles = predict_SMILES(segment_path)
smiles_predicted.append(smiles)
os.remove(segment_path)
return '.'.join(smiles_predicted)
6 changes: 3 additions & 3 deletions app/routers/chem.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from app.modules.classyfire import classify, result
from app.modules.cdkmodules import getCDKSDGMol
from app.modules.depict import getRDKitDepiction, getCDKDepiction
from DECIMER import predict_SMILES
from app.modules.decimermodules import getPredictedSegments

router = APIRouter(
prefix="/chem",
Expand Down Expand Up @@ -183,7 +183,7 @@ async def extract_chemicalinfo(request: Request):
response = urlopen(imgDataURI)
with open(filename, "wb") as f:
f.write(response.file.read())
smiles = predict_SMILES(filename)
smiles = getPredictedSegments(filename)
os.remove(filename)
return JSONResponse(
content={"reference": reference, "smiles": smiles.split(".")}
Expand All @@ -193,7 +193,7 @@ async def extract_chemicalinfo(request: Request):
if response.status_code == 200:
with open(filename, "wb") as f:
f.write(response.content)
smiles = predict_SMILES(filename)
smiles = getPredictedSegments(filename)
os.remove(filename)
return JSONResponse(
content={"reference": reference, "smiles": smiles.split(".")}
Expand Down
13 changes: 11 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ uvicorn>=0.15.0,<0.16.0
psycopg2>=2.7,<2.8
fastapi-pagination==0.10.0
rdkit-pypi>=2022.09.4
STOUT-pypi>=2.0.5
websockets==10.4
decimer>=2.2.0
pillow
opencv-python
matplotlib
scikit-image
imantics
pdf2image
IPython
pystow
unicodedata2
efficientnet
tensorflow==2.10.0

0 comments on commit 096dc67

Please sign in to comment.