|
import os |
|
from huggingface_hub import login |
|
from transformers import BlipProcessor, BlipForConditionalGeneration |
|
|
|
|
|
|
|
|
|
hf_token = os.getenv('HF_AUTH_TOKEN') |
|
if not hf_token: |
|
raise ValueError("Hugging Face token is not set in the environment variables.") |
|
login(token=hf_token) |
|
|
|
|
|
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large") |
|
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large") |
|
import gradio as gr |
|
from diffusers import DiffusionPipeline |
|
import torch |
|
import spaces |
|
|
|
|
|
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium") |
|
|
|
|
|
|
|
@spaces.GPU(duration=300) |
|
def generate_caption_and_image(image): |
|
|
|
raw_image = image.convert("RGB") |
|
|
|
|
|
inputs = processor(raw_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) |
|
caption = processor.decode(out[0], skip_special_tokens=True) |
|
|
|
|
|
generated_image = pipe(caption).images[0] |
|
|
|
return caption, generated_image |
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_caption_and_image, |
|
inputs=gr.Image(type="pil", label="Upload Image"), |
|
outputs=[gr.Textbox(label="Generated Caption"), gr.Image(label="Generated Design")], |
|
live=True |
|
) |
|
|
|
|