Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error and improve code of image_app_simple #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
- fix the AttributeError: 'Table' object has no attribute 'gens'
- integrate the code to run with and without Replicate API into one py file. Update README.md accordingly
khoaguin committed Sep 8, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 96bad5c2692715ba623db3999ccd2fb20e853a88
19 changes: 2 additions & 17 deletions image_app_simple/README.md
Original file line number Diff line number Diff line change
@@ -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=<your_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!)
53 changes: 36 additions & 17 deletions image_app_simple/main.py
Original file line number Diff line number Diff line change
@@ -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')
Copy link
Contributor

@matatratata matatratata Sep 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for my mistake. I locally missed the .t in the database instantiation. The correct approach is to revert my changes. The tables variable, as the name suggests, should point to the tables. I have already submitted a PR to address this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @matatratata. I will update my code after your PR getting merged then

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)))