Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- .env +2 -0
- .env.example +1 -0
- .gitattributes +4 -0
- .gitignore +13 -0
- README.md +30 -8
- api/index.py +112 -0
- flagged/.DS_Store +0 -0
- flagged/log.csv +5 -0
- flagged/moodboard/.DS_Store +0 -0
- flagged/moodboard/14c7c3a52cd71affacbe/Screenshot 2024-06-11 at 5.27.28PM.png +3 -0
- flagged/moodboard/20a9ebffee3a984e4e92/Screenshot 2024-06-11 at 5.27.28PM.png +3 -0
- flagged/moodboard/98d40109371f437df727/Screenshot 2024-06-11 at 5.36.16PM.png +3 -0
- flagged/moodboard/fde8c7b6d7cdf2100e2e/Screenshot 2024-06-11 at 5.27.28PM.png +3 -0
- flagged/output/1d9e6b07d5463259c1ef/image.PNG +0 -0
- flagged/output/5c91673d54010e53397b/image.PNG +0 -0
- flagged/output/bddb16aad6e2eb4ce2f4/image.PNG +0 -0
- flagged/output/c2deadeee2b177d78214/image.PNG +0 -0
- ogimage.png +0 -0
- requirements.txt +4 -0
- vercel.json +14 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
.env
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
REPLICATE_API_TOKEN=r8_DAzyOBdCwUdt0b26ZMPWLyvyHTh55uh2Lwb3c
|
2 |
+
OPENAI_API_KEY=sk-proj-6lTXmIwTYmNo7uUpQwujT3BlbkFJDMVzyH5hzblFbgYLLMCP
|
.env.example
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
REPLICATE_API_TOKEN=
|
.gitattributes
CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
flagged/moodboard/14c7c3a52cd71affacbe/Screenshot[[:space:]]2024-06-11[[:space:]]at[[:space:]]5.27.28PM.png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
flagged/moodboard/20a9ebffee3a984e4e92/Screenshot[[:space:]]2024-06-11[[:space:]]at[[:space:]]5.27.28PM.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
flagged/moodboard/98d40109371f437df727/Screenshot[[:space:]]2024-06-11[[:space:]]at[[:space:]]5.36.16PM.png filter=lfs diff=lfs merge=lfs -text
|
39 |
+
flagged/moodboard/fde8c7b6d7cdf2100e2e/Screenshot[[:space:]]2024-06-11[[:space:]]at[[:space:]]5.27.28PM.png filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.vercel
|
2 |
+
*.log
|
3 |
+
*.pyc
|
4 |
+
__pycache__
|
5 |
+
|
6 |
+
# Environments
|
7 |
+
.env
|
8 |
+
.venv
|
9 |
+
env/
|
10 |
+
venv/
|
11 |
+
ENV/
|
12 |
+
env.bak/
|
13 |
+
venv.bak/
|
README.md
CHANGED
@@ -1,12 +1,34 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: gray
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
|
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: moodboard-gen-img
|
3 |
+
app_file: api/index.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
+
sdk_version: 4.5.0
|
|
|
|
|
6 |
---
|
7 |
+
# Alt Image Generator
|
8 |
|
9 |
+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/nutlope/alt-text-generator&env=REPLICATE_API_KEY&project-name=alt-tag-generator&repo-name=alt-tag-generator)
|
10 |
+
|
11 |
+
This Flask API will generate a description for any image using AI. If you're looking for the TypeScript version, [click here](https://github.com/vercel/examples/tree/main/solutions/alt-tag-generator).
|
12 |
+
|
13 |
+
![Alt Image Generator](ogimage.png)
|
14 |
+
|
15 |
+
## How it works
|
16 |
+
|
17 |
+
This project uses an ML modal from Salesforce called [BLIP](https://github.com/salesforce/BLIP) on [Replicate](https://replicate.com/) to generate relevant alt text for images. You can feed the Flask API endpoint an image as a query param and it will return a one sentence description of that image.
|
18 |
+
|
19 |
+
## Running Locally
|
20 |
+
|
21 |
+
After cloning the repo, go to [Replicate](https://replicate.com/) to make an account and put your API key in `.env`.
|
22 |
+
|
23 |
+
Then, run the following in the command line and your application will be available at `http://localhost:3000`
|
24 |
+
|
25 |
+
```bash
|
26 |
+
npm i -g vercel
|
27 |
+
vercel dev
|
28 |
+
```
|
29 |
+
|
30 |
+
To use the API route, go to the link below in your browser or run a curl command in your terminal to get a sample result. Feel free to replace the dub.sh link with a link to any image.
|
31 |
+
|
32 |
+
```bash
|
33 |
+
curl http://localhost:3000/generate?imageUrl=https://dub.sh/confpic
|
34 |
+
```
|
api/index.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import base64
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import requests
|
7 |
+
|
8 |
+
import replicate
|
9 |
+
from flask import Flask, request
|
10 |
+
import gradio as gr
|
11 |
+
from openai import OpenAI
|
12 |
+
|
13 |
+
from dotenv import load_dotenv, find_dotenv
|
14 |
+
|
15 |
+
# Locate the .env file
|
16 |
+
dotenv_path = find_dotenv()
|
17 |
+
|
18 |
+
load_dotenv(dotenv_path)
|
19 |
+
|
20 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
21 |
+
REPLICATE_API_TOKEN = os.getenv('REPLICATE_API_TOKEN')
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
client = OpenAI()
|
26 |
+
|
27 |
+
|
28 |
+
def call_openai(pil_image):
|
29 |
+
# Save the PIL image to a bytes buffer
|
30 |
+
buffered = io.BytesIO()
|
31 |
+
pil_image.save(buffered, format="JPEG")
|
32 |
+
|
33 |
+
# Encode the image to base64
|
34 |
+
image_data = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
35 |
+
|
36 |
+
response = client.chat.completions.create(
|
37 |
+
model="gpt-4o",
|
38 |
+
messages=[
|
39 |
+
{
|
40 |
+
"role": "user",
|
41 |
+
"content": [
|
42 |
+
{"type": "text", "text": "You are a product designer. I've attached a moodboard here. In one sentence, what do all of these elements have in common? Answer from a design language perspective, if you were telling another designer to create something similar, including any repeating colors and materials and shapes and textures"},
|
43 |
+
{
|
44 |
+
"type": "image_url",
|
45 |
+
"image_url": {
|
46 |
+
"url": "data:image/jpeg;base64," + image_data,
|
47 |
+
},
|
48 |
+
},
|
49 |
+
],
|
50 |
+
}
|
51 |
+
],
|
52 |
+
max_tokens=300,
|
53 |
+
)
|
54 |
+
|
55 |
+
return response.choices[0].message.content
|
56 |
+
|
57 |
+
def image_classifier(moodboard, starter_image, image_strength, prompt):
|
58 |
+
|
59 |
+
# Convert the numpy array to a PIL image
|
60 |
+
pil_image = Image.fromarray(moodboard.astype('uint8'))
|
61 |
+
starter_image_pil = Image.fromarray(starter_image.astype('uint8'))
|
62 |
+
|
63 |
+
# Resize the starter image if it's larger than 768x768
|
64 |
+
if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
|
65 |
+
starter_image_pil = starter_image_pil.resize((768, 768), Image.LANCZOS)
|
66 |
+
|
67 |
+
openai_response = call_openai(pil_image)
|
68 |
+
openai_response = openai_response.replace('moodboard', '')
|
69 |
+
|
70 |
+
# Save the starter image to a bytes buffer
|
71 |
+
buffered = io.BytesIO()
|
72 |
+
starter_image_pil.save(buffered, format="JPEG")
|
73 |
+
|
74 |
+
# Encode the starter image to base64
|
75 |
+
starter_image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
76 |
+
|
77 |
+
# Call Stable Diffusion API with the response from OpenAI
|
78 |
+
input = {
|
79 |
+
"width": 768,
|
80 |
+
"height": 768,
|
81 |
+
"prompt": "high quality render of " + prompt + ", " + openai_response[20:],
|
82 |
+
"negative_prompt": "worst quality, low quality, illustration, 2d, painting, cartoons, sketch",
|
83 |
+
"refine": "expert_ensemble_refiner",
|
84 |
+
"image": "data:image/jpeg;base64," + starter_image_base64,
|
85 |
+
"apply_watermark": False,
|
86 |
+
"num_inference_steps": 25,
|
87 |
+
"prompt_strength": 1-image_strength
|
88 |
+
}
|
89 |
+
|
90 |
+
output = replicate.run(
|
91 |
+
"stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc",
|
92 |
+
input=input
|
93 |
+
)
|
94 |
+
|
95 |
+
# Download the image from the URL
|
96 |
+
image_url = output[0]
|
97 |
+
print(image_url)
|
98 |
+
response = requests.get(image_url)
|
99 |
+
print(response)
|
100 |
+
img = Image.open(io.BytesIO(response.content))
|
101 |
+
|
102 |
+
return img # Return the image object
|
103 |
+
|
104 |
+
|
105 |
+
# app = Flask(__name__)
|
106 |
+
# os.environ.get("REPLICATE_API_TOKEN")
|
107 |
+
|
108 |
+
# @app.route("/")
|
109 |
+
# def index():
|
110 |
+
|
111 |
+
demo = gr.Interface(fn=image_classifier, inputs=["image", "image", gr.Slider(0, 1, step=0.025, value=0.2, label="Image Strength"), "text"], outputs="image")
|
112 |
+
demo.launch(share=True)
|
flagged/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
flagged/log.csv
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
moodboard,prompt,output,flag,username,timestamp
|
2 |
+
"{""path"":""flagged/moodboard/14c7c3a52cd71affacbe/Screenshot 2024-06-11 at 5.27.28PM.png"",""url"":""https://71d2f0a3d14e8c955e.gradio.live/file=/private/var/folders/yq/z7scmd9d1t5_v8ycgz9trrj80000gn/T/gradio/9ba7cc91249b0ba1ea627f60ec346bfafc2851a7/Screenshot 2024-06-11 at 5.27.28PM.png"",""size"":2794341,""orig_name"":""Screenshot 2024-06-11 at 5.27.28 PM.png"",""mime_type"":""""}",A white headphone,"{""path"":""flagged/output/bddb16aad6e2eb4ce2f4/image.PNG"",""url"":null,""size"":null,""orig_name"":""image.PNG"",""mime_type"":null}",,,2024-06-11 21:29:29.682875
|
3 |
+
"{""path"":""flagged/moodboard/20a9ebffee3a984e4e92/Screenshot 2024-06-11 at 5.27.28PM.png"",""url"":""https://71d2f0a3d14e8c955e.gradio.live/file=/private/var/folders/yq/z7scmd9d1t5_v8ycgz9trrj80000gn/T/gradio/9ba7cc91249b0ba1ea627f60ec346bfafc2851a7/Screenshot 2024-06-11 at 5.27.28PM.png"",""size"":2794341,""orig_name"":""Screenshot 2024-06-11 at 5.27.28 PM.png"",""mime_type"":""""}",A white headphone designed by dieter rams,"{""path"":""flagged/output/c2deadeee2b177d78214/image.PNG"",""url"":null,""size"":null,""orig_name"":""image.PNG"",""mime_type"":null}",,,2024-06-11 21:31:16.888982
|
4 |
+
"{""path"":""flagged/moodboard/fde8c7b6d7cdf2100e2e/Screenshot 2024-06-11 at 5.27.28PM.png"",""url"":""https://71d2f0a3d14e8c955e.gradio.live/file=/private/var/folders/yq/z7scmd9d1t5_v8ycgz9trrj80000gn/T/gradio/9ba7cc91249b0ba1ea627f60ec346bfafc2851a7/Screenshot 2024-06-11 at 5.27.28PM.png"",""size"":2794341,""orig_name"":""Screenshot 2024-06-11 at 5.27.28 PM.png"",""mime_type"":""""}",A white headphone designed by dieter rams,"{""path"":""flagged/output/5c91673d54010e53397b/image.PNG"",""url"":null,""size"":null,""orig_name"":""image.PNG"",""mime_type"":null}",,,2024-06-11 21:31:22.368689
|
5 |
+
"{""path"":""flagged/moodboard/98d40109371f437df727/Screenshot 2024-06-11 at 5.36.16PM.png"",""url"":""https://71d2f0a3d14e8c955e.gradio.live/file=/private/var/folders/yq/z7scmd9d1t5_v8ycgz9trrj80000gn/T/gradio/04e231aa9e4e507cd24df24d3ab7b8b87a22d315/Screenshot 2024-06-11 at 5.36.16PM.png"",""size"":1326224,""orig_name"":""Screenshot 2024-06-11 at 5.36.16 PM.png"",""mime_type"":""""}",A white sleek and geometric headphone ,"{""path"":""flagged/output/1d9e6b07d5463259c1ef/image.PNG"",""url"":null,""size"":null,""orig_name"":""image.PNG"",""mime_type"":null}",,,2024-06-11 21:45:46.737039
|
flagged/moodboard/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
flagged/moodboard/14c7c3a52cd71affacbe/Screenshot 2024-06-11 at 5.27.28PM.png
ADDED
Git LFS Details
|
flagged/moodboard/20a9ebffee3a984e4e92/Screenshot 2024-06-11 at 5.27.28PM.png
ADDED
Git LFS Details
|
flagged/moodboard/98d40109371f437df727/Screenshot 2024-06-11 at 5.36.16PM.png
ADDED
Git LFS Details
|
flagged/moodboard/fde8c7b6d7cdf2100e2e/Screenshot 2024-06-11 at 5.27.28PM.png
ADDED
Git LFS Details
|
flagged/output/1d9e6b07d5463259c1ef/image.PNG
ADDED
flagged/output/5c91673d54010e53397b/image.PNG
ADDED
flagged/output/bddb16aad6e2eb4ce2f4/image.PNG
ADDED
flagged/output/c2deadeee2b177d78214/image.PNG
ADDED
ogimage.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask==3.0.0
|
2 |
+
replicate==0.14.0
|
3 |
+
gradio
|
4 |
+
#openai
|
vercel.json
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"builds": [
|
3 |
+
{
|
4 |
+
"src": "api/index.py",
|
5 |
+
"use": "@vercel/python"
|
6 |
+
}
|
7 |
+
],
|
8 |
+
"routes": [
|
9 |
+
{
|
10 |
+
"src": "/(.*)",
|
11 |
+
"dest": "api/index.py"
|
12 |
+
}
|
13 |
+
]
|
14 |
+
}
|