Skip to content

Commit

Permalink
Minor update of image gen script
Browse files Browse the repository at this point in the history
  • Loading branch information
eleurent committed Oct 15, 2023
1 parent a371392 commit affcbd0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
17 changes: 4 additions & 13 deletions generation/image/generate_illustrations.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@

import argparse
import requests
from PIL import Image
import io
import base64
import pandas as pd
from pathlib import Path
import wikipedia
import urllib.request
import tqdm
import nature_go_client
import matplotlib.pyplot as plt
import sys
import rembg
from illustration_workflows import txt2img_workflow
from illustration_workflows import controlnet_workflow

sys.path.append('..')
import nature_go_client

# Stable Diffusion parameters
comfyui_path = txt2img_workflow.find_path("ComfyUI")
SD_HOST = 'http://nature-go.edouardleurent.com'
PROMPT = "herbarium illustration of {commonNames} {scientificNameWithoutAuthor}, 19th century, transactions of the Botanical Society of London"
PROMPT = "{commonNames} {scientificNameWithoutAuthor}, 19th century botanical illustration, transactions of the Botanical Society of London"
BATCH_SIZE = 100

# ## Retrieve species with missing images through Nature go API


def get_species(client, batch_size=5, ordering=None):
ordering = ordering or ordering.split(',')
ordering = ordering.split(',') if ',' in ordering else ordering
species_list = client.get_labeled_species(illustration=False, limit=batch_size, ordering=ordering)
print(f'Found {len(species_list)} species')
return pd.DataFrame(species_list)
Expand All @@ -42,13 +40,6 @@ def fetch_wikipedia_images(scientific_name):
page = wikipedia.page(scientific_name)
return page.images[:1]


def display_images(*images):
_, axes = plt.subplots(1, len(images), figsize=(5*len(images), 5), squeeze=False)
for i, image in enumerate(images):
axes.flat[i].imshow(image)
axes.flat[i].axis('off')

def run_txt2img_worflow(positive_prompts, negative_prompts, rembg_session):
import random
random.seed(len(positive_prompts[0]))
Expand Down
8 changes: 4 additions & 4 deletions generation/nature_go_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def login(self):
logger.info('Login successful')
self.token = response.json()['token']
else:
raise ValueError(f'Login failed with status {response.status_code} and response {response.json()}')
raise ValueError(f'Login failed with status {response.status_code} and response {response}')

def assert_logged_in(self):
if self.token is None:
Expand All @@ -42,7 +42,7 @@ def get_all_species(self, limit=100, offset=0, ordering='-occurences_cdf') -> li
logger.info(f'Fetched {len(result)} species')
return result['results']
else:
raise ValueError(f'{SPECIES_LIST_URL} failed with status {response.status_code} and response {response.json()}')
raise ValueError(f'{SPECIES_LIST_URL} failed with status {response.status_code} and response {response}')

def get_labeled_species(
self,
Expand Down Expand Up @@ -71,7 +71,7 @@ def get_labeled_species(
logger.info(f'Fetched {len(result)} species')
return result['results']
else:
raise ValueError(f'{SPECIES_LABELED_URL} failed with status {response.status_code} and response {response.json()}')
raise ValueError(f'{SPECIES_LABELED_URL} failed with status {response.status_code} and response {response}')



Expand Down Expand Up @@ -109,7 +109,7 @@ def get_all_questions(self) -> list:
logger.info(f'Fetched {len(result)} species')
return result
else:
raise ValueError(f'{QUESTION_CREATE_URL} failed with status {response.status_code} and response {response.json()}')
raise ValueError(f'{QUESTION_CREATE_URL} failed with status {response.status_code} and response {response}')


def post_species_questions(self, species_id: int, questions: list):
Expand Down
3 changes: 2 additions & 1 deletion generation/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pandas
wikipedia
openai
python-dotenv
tiktoken
tiktoken
matplotlib

0 comments on commit affcbd0

Please sign in to comment.