diff --git a/image_app_simple/README.md b/image_app_simple/README.md index 5915a12..bfecd3b 100644 --- a/image_app_simple/README.md +++ b/image_app_simple/README.md @@ -4,25 +4,10 @@ This example shows an interface for a text-to-image model. The user enters a des ![Screenshot](screenshot.png) - -This app requires setting REPLICATE_API_KEY as a variable in the Railway project or as an environment variable for running locally. +This app requires setting `REPLICATE_API_KEY` as a variable in the Railway project or as an environment variable for running locally: `REPLICATE_API_KEY= python main.py` **WARNING**: Deploying this publically will 1) let anyone see all images generated and 2) let anyone generate images (using up your replicate credits). We recommend NOT doing this! See the `image_app_session_credits` example for one that adds usage limits and the ability for users to donate "credits" to the public pool. -If you want to try with a free alternative to Replicate, you can use the Pollinations API (for now) with: - -```python -# URL (for image generation) -def get_url(prompt): - return f"https://image.pollinations.ai/prompt/{prompt.replace(' ', '%20')}?model=flux&width=1024&height=1024&seed=42&nologo=true&enhance=true" - -... - -@threaded -def generate_and_save(prompt, id, folder): - full_url = get_url(prompt) - Image.open(requests.get(full_url, stream=True).raw).save(f"{folder}/{id}.png") - return True -``` +If you want to try with a free alternative to Replicate, you can use the Pollinations API (for now) and not having to specify the Replicate API key: `python main.py` (Thanks to [Zaseem](https://github.com/Zaseem-BIsquared) for the suggestion!) \ No newline at end of file diff --git a/image_app_simple/main.py b/image_app_simple/main.py index 1ffdc91..190f72b 100644 --- a/image_app_simple/main.py +++ b/image_app_simple/main.py @@ -5,11 +5,44 @@ # Replicate setup (for generating images) -replicate_api_token = os.environ['REPLICATE_API_KEY'] -client = replicate.Client(api_token=replicate_api_token) +replicate_api_token = os.getenv('REPLICATE_API_KEY', None) + +if replicate_api_token: + client = replicate.Client(api_token=replicate_api_token) + + # Generate an image and save it to the folder (in a separate thread) + @threaded + def generate_and_save(prompt, id, folder): + output = client.run( + "playgroundai/playground-v2.5-1024px-aesthetic:a45f82a1382bed5c7aeb861dac7c7d191b0fdf74d8d57c4a0e6ed7d4d0bf7d24", + input={ + "width": 1024,"height": 1024,"prompt": prompt,"scheduler": "DPMSolver++", + "num_outputs": 1,"guidance_scale": 3,"apply_watermark": True, + "negative_prompt": "ugly, deformed, noisy, blurry, distorted", + "prompt_strength": 0.8, "num_inference_steps": 25 + } + ) + Image.open(requests.get(output[0], stream=True).raw).save(f"{folder}/{id}.png") + return True + + print("Replicate client initialized") + +else: + # URL (for image generation) + def get_url(prompt): + return f"https://image.pollinations.ai/prompt/{prompt.replace(' ', '%20')}?model=flux&width=1024&height=1024&seed=42&nologo=true&enhance=true" + + @threaded + def generate_and_save(prompt, id, folder): + full_url = get_url(prompt) + Image.open(requests.get(full_url, stream=True).raw).save(f"{folder}/{id}.png") + return True + + print(f"No Replicate API Key. Use Pollinations API (for now) for image generation") + # gens database for storing generated image details -tables = database('data/gens.db').t +tables = database('data/gens.db') gens = tables.t.gens if not gens in tables.t: gens.create(prompt=str, id=int, folder=str, pk='id') @@ -63,19 +96,5 @@ def post(prompt:str): clear_input = Input(id="new-prompt", name="prompt", placeholder="Enter a prompt", hx_swap_oob='true') return generation_preview(g), clear_input -# Generate an image and save it to the folder (in a separate thread) -@threaded -def generate_and_save(prompt, id, folder): - output = client.run( - "playgroundai/playground-v2.5-1024px-aesthetic:a45f82a1382bed5c7aeb861dac7c7d191b0fdf74d8d57c4a0e6ed7d4d0bf7d24", - input={ - "width": 1024,"height": 1024,"prompt": prompt,"scheduler": "DPMSolver++", - "num_outputs": 1,"guidance_scale": 3,"apply_watermark": True, - "negative_prompt": "ugly, deformed, noisy, blurry, distorted", - "prompt_strength": 0.8, "num_inference_steps": 25 - } - ) - Image.open(requests.get(output[0], stream=True).raw).save(f"{folder}/{id}.png") - return True if __name__ == '__main__': uvicorn.run("main:app", host='0.0.0.0', port=int(os.getenv("PORT", default=5000)))