Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
from huggingface_hub import login
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
|
|
@@ -28,17 +30,25 @@ model.to(device)
|
|
| 28 |
|
| 29 |
@spaces.GPU(duration=300)
|
| 30 |
def generate_caption_and_image(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Process the image
|
| 32 |
raw_image = image.convert("RGB")
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Generate caption
|
| 35 |
inputs = processor(raw_image, return_tensors="pt", padding=True, truncation=True, max_length=250)
|
| 36 |
inputs = {key: val.to(device) for key, val in inputs.items()}
|
| 37 |
out = model.generate(**inputs)
|
| 38 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Generate image based on the caption
|
| 41 |
-
generated_image = pipe(
|
| 42 |
|
| 43 |
return caption, generated_image
|
| 44 |
|
|
|
|
| 1 |
import os
|
| 2 |
from huggingface_hub import login
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import pytesseract
|
| 6 |
|
| 7 |
|
| 8 |
|
|
|
|
| 30 |
|
| 31 |
@spaces.GPU(duration=300)
|
| 32 |
def generate_caption_and_image(image):
|
| 33 |
+
img = image.convert("RGB")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
extracted_text = pytesseract.image_to_string(img)
|
| 37 |
# Process the image
|
| 38 |
raw_image = image.convert("RGB")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
|
| 42 |
# Generate caption
|
| 43 |
inputs = processor(raw_image, return_tensors="pt", padding=True, truncation=True, max_length=250)
|
| 44 |
inputs = {key: val.to(device) for key, val in inputs.items()}
|
| 45 |
out = model.generate(**inputs)
|
| 46 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 47 |
+
prompt = f"Create a highly realistic design of a clothing item based on the following description: 'The design should incorporate elements from the extracted text: {extracted_text}, along with the caption: {caption}. The clothing should look realistic, modern, and stylish. Use high-quality fabric textures and realistic lighting to give the design a lifelike appearance. The colors, patterns, and materials should reflect the essence of the caption and extracted text.'"
|
| 48 |
+
|
| 49 |
|
| 50 |
# Generate image based on the caption
|
| 51 |
+
generated_image = pipe(prompt).images[0]
|
| 52 |
|
| 53 |
return caption, generated_image
|
| 54 |
|