StyleSync / app.py
Fiqa's picture
Update app.py
3a94231 verified
import os
from huggingface_hub import login
from transformers import BlipProcessor, BlipForConditionalGeneration
from transformers import MllamaForConditionalGeneration, AutoProcessor
from PIL import Image
from dotenv import load_dotenv
import gradio as gr
from diffusers import DiffusionPipeline
import torch
import spaces # Hugging Face Spaces module
import requests
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
from diffusers import DiffusionPipeline
fabrics = ['cotton', 'silk', 'denim', 'linen', 'polyester', 'wool', 'velvet']
patterns = ['striped', 'floral', 'geometric', 'abstract', 'solid', 'polka dots']
textile_designs = ['woven texture', 'embroidery', 'printed fabric', 'hand-dyed', 'quilting']
# Get Hugging Face Token from environment variable
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
# Authenticate using the token
login(token =HUGGINGFACE_TOKEN)
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
processor1 = BlipProcessor.from_pretrained("noamrot/FuseCap")
model2 = BlipForConditionalGeneration.from_pretrained("noamrot/FuseCap")
from diffusers import FluxPipeline
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
device = "cuda" if torch.cuda.is_available() else "cpu"
# pipe.to(device)
model.to(device)
pipe.to(device)
model2.to(device)
@spaces.GPU(duration=150)
def generate_caption_and_image(image, f, p, d):
if f!=None and p!=None and d!=None and image!=None:
img = image.convert("RGB")
# reader = easyocr.Reader(['en'])
# # result = reader.readtext(img)
# import random
text = "a picture of "
inputs = processor(img, text, return_tensors="pt").to(device)
out = model2.generate(**inputs, num_beams = 3)
caption2 = processor1.decode(out[0], skip_special_tokens=True)
inputs = processor(image, return_tensors="pt", padding=True, truncation=True, max_length=250)
inputs = {key: val.to(device) for key, val in inputs.items()}
out = model.generate(**inputs)
caption1 = processor.decode(out[0], skip_special_tokens=True)
prompt = f"Design a high-quality, stylish clothing item that flawlessly combines the essence of {caption1} and {caption2}. The design should emphasize the luxurious feel and practicality of {f} fabric, while integrating intricate {d} textual design elements. Incorporate {p} patterns that elevate the garment's aesthetic, ensuring a harmonious blend of textures and visuals. The final piece should be both sophisticated and innovative, reflecting modern trends while preserving timeless elegance. The design should be bold, wearable, and a true work of art."
image = pipe(prompt,height=1024,width=1024,guidance_scale=3.5,num_inference_steps=50,max_sequence_length=512,generator=torch.Generator("cpu").manual_seed(0)).images[0]
return image
return None
# Gradio UI
iface = gr.Interface(
fn=generate_caption_and_image,
inputs=[gr.Image(type="pil", label="Upload Image"), gr.Radio(fabrics, label="Select Fabric"), gr.Radio(patterns, label="Select Pattern"), gr.Radio(textile_designs, label="Select Textile Design")],
outputs=[gr.Image(label="Generated Design 1")],
live=True
)
iface.launch(share=True)