Skip to content

Instantly share code, notes, and snippets.

@churnikov
Created October 29, 2024 18:02
Show Gist options
  • Save churnikov/cd0b78c4309bd68b0d9a4d59564d8c65 to your computer and use it in GitHub Desktop.
Save churnikov/cd0b78c4309bd68b0d9a4d59564d8c65 to your computer and use it in GitHub Desktop.
Test files for the gradio app
# Select base image (can be ubuntu, python, shiny etc)
FROM python:3.11-slim
# Create user name and home directory variables.
# The variables are later used as $USER and $HOME.
ENV USER=username
ENV HOME=/home/$USER
# Add user to system
RUN useradd -m -u 1000 $USER
# Set working directory (this is where the code should go)
WORKDIR $HOME/app
# Update system and install dependencies.
RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
software-properties-common
RUN python3 -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
RUN pip install --no-cache-dir gradio
RUN apt-get install -y curl
COPY main.py $HOME/app/main.py
# copy any other files that are needed for your app with the directory structure as your files expect
COPY start-script.sh $HOME/app/start-script.sh
COPY data/ $HOME/app/data
RUN chmod +x start-script.sh \
&& chown -R $USER:$USER $HOME \
&& rm -rf /var/lib/apt/lists/*
USER $USER
EXPOSE 8080
ENTRYPOINT ["./start-script.sh"]
# This demo needs to be run from the repo folder.
# python demo/fake_gan/run.py
import random
from io import BytesIO
import gradio as gr
import requests
from PIL import Image
def fake_gan():
images = [
(random.choice(
[
"http://www.marketingtool.online/en/face-generator/img/faces/avatar-1151ce9f4b2043de0d2e3b7826127998.jpg",
"http://www.marketingtool.online/en/face-generator/img/faces/avatar-116b5e92936b766b7fdfc242649337f7.jpg",
"http://www.marketingtool.online/en/face-generator/img/faces/avatar-1163530ca19b5cebe1b002b8ec67b6fc.jpg",
"http://www.marketingtool.online/en/face-generator/img/faces/avatar-1116395d6e6a6581eef8b8038f4c8e55.jpg",
"http://www.marketingtool.online/en/face-generator/img/faces/avatar-11319be65db395d0e8e6855d18ddcef0.jpg",
]
), f"label {i}")
for i in range(3)
]
return images
def predict(mol: str, confidence: float, imageWidth, imageHeight):
resp = requests.get(
"https://cplogdapi.serve.scilifelab.se/api/v2/predictImage",
params={
"molecule": mol,
"confidence": confidence,
"imageWidth": imageWidth,
"imageHeight": imageHeight,
},
)
print(resp.raw)
return Image.open(BytesIO(resp.content), formats=["png"])
with gr.Blocks() as demo:
gallery = gr.Gallery(
label="Generated images", show_label=False, elem_id="gallery"
, columns=[1], rows=[1], object_fit="contain", height="auto")
btn = gr.Button("Generate images", scale=0)
btn.click(fake_gan, None, gallery)
text_inupt = gr.Textbox(lines=5, label="Input smiles", placeholder="Enter smiles here")
confidence = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, label="Confidence")
imageWidth = gr.Slider(minimum=0, maximum=1000, step=1, label="Image width")
imageHeight = gr.Slider(minimum=0, maximum=1000, step=1, label="Image height")
output = gr.Image(label="Predicted image", show_label=False, elem_id="output")
btn2 = gr.Button("Predict", scale=0)
btn2.click(predict, [text_inupt, confidence, imageWidth, imageHeight], output)
if __name__ == "__main__":
print("Starting demo server...")
demo.launch(server_name="0.0.0.0", server_port=8080)
# demo.launch()
#!/bin/bash
python3 main.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment