t2i / app.py
Yash911's picture
Update app.py
5f0d084
raw
history blame
1.63 kB
import gradio as gr
import requests
import cloudinary
import cloudinary.uploader
from PIL import Image
import io
# Set up Cloudinary credentials
cloudinary.config(
cloud_name="dvuowbmrz",
api_key="177664162661619",
api_secret="qVMYel17N_C5QUUUuBIuatB5tq0"
)
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
headers = {"Authorization": "Bearer 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"}
def query_model_with_image(image_description):
payload = {
"inputs": image_description
}
response = requests.post(API_URL, headers=headers, json=payload)
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
return image
def upload_to_cloudinary(image):
image_data = io.BytesIO()
image.save(image_data, format="JPEG")
image_data.seek(0)
upload_result = cloudinary.uploader.upload(image_data, folder="compvis_app")
return upload_result["secure_url"]
def get_last_uploaded_images(limit=4):
images = cloudinary.api.resources(type="upload", max_results=limit, prefix="compvis_app/")
return [img["secure_url"] for img in images["resources"]]
def process_and_upload(image_description):
processed_image = query_model_with_image(image_description)
uploaded_url = upload_to_cloudinary(processed_image)
last_uploaded_images = get_last_uploaded_images()
return processed_image, uploaded_url, last_uploaded_images
iface = gr.Interface(
fn=process_and_upload,
inputs=gr.inputs.Textbox(label="Image Description"),
outputs=["image", "text", "text"]
)
if __name__ == "__main__":
iface.launch()