# -*- coding: utf-8 -*- """mini_t2i.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1QL7cDE204_CEk2kw87aiM7ArlAVF-1Uu """ # !pip install gradio # !pip install cloudinary 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 process_and_upload(image_description): processed_image = query_model_with_image(image_description) uploaded_url = upload_to_cloudinary(processed_image) return processed_image, uploaded_url iface = gr.Interface( fn=process_and_upload, inputs=gr.inputs.Textbox(label="Image Description"), outputs=["image", "text"] ) if __name__ == "__main__": iface.launch()