Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import gradio as grfrom transformers import pipelinefrom diffusers import StableDiffusionPipelineimport torchimport wget
# Define the device to use (either "cuda" for GPU or "cpu" for CPU)device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the modelscaption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large", device=device)sd_pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device)
translator = pipeline( task="translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16, device=device)
# Function to generate images based on the image's captiondef generate_image_and_translate(image, num_images=1): caption_en = caption_image(image)[0]['generated_text'] caption_ar = translator(caption_en, src_lang="eng_Latn", tgt_lang="arb_Arab")[0]['translation_text']
generated_images = [] for _ in range(num_images): generated_image = sd_pipeline(prompt=caption_en).images[0] generated_images.append(generated_image)
return generated_images, caption_en, caption_ar
# Set up the Gradio interfaceinterface = gr.Interface( fn=generate_image_and_translate, inputs=[ gr.Image(type="pil", label="Upload Image"), gr.Slider(minimum=1, maximum=10, label="Number of Images", value=1, step=1) ], outputs=[ gr.Gallery(label="Generated Images"), gr.Textbox(label="Generated Caption (English)", interactive=False), gr.Textbox(label="Translated Caption (Arabic)", interactive=False) ], title="Image Generation and Translation", description="Upload an image to generate new images based on its caption and translate the caption into Arabic.", examples=[ ["sea.jpg", 3] ])
# Launch the Gradio applicationif __name__ == "__main__": interface.launch()app.pyAdded+54