StyleSync / app.py
Fiqa's picture
Update app.py
a04adbd verified
raw
history blame
1.8 kB
import os
from huggingface_hub import login
from transformers import BlipProcessor, BlipForConditionalGeneration
# Get Hugging Face Token from environment variable
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)
# Load the processor and model
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 # Hugging Face Spaces module
# Initialize the model
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium")
st.title("Image Caption Generator")
st.write("Upload an image or provide an image URL to generate its caption.")
# Option for image upload
img_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
if img_file is not None:
raw_image = Image.open(img_file).convert('RGB')
text = "a photography of"
inputs = processor(raw_image, text, return_tensors="pt", padding =True, truncation=True, max_length =250)
out = model.generate(**inputs)
caption = processor.decode(out[0], skip_special_tokens=True)
@spaces.GPU(duration=300)
def generate_image(prompt):
# Move the model to GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe.to(device)
image = pipe(prompt).images[0]
return image
# Create the Gradio interface
iface = gr.Interface(fn=generate_image,
inputs=caption,
outputs=gr.Image(label="Generated Image"),
title="Astronaut in a Jungle Model")
# Launch the interface
iface.launch(share=True)