File size: 1,596 Bytes
875ef36
 
 
 
 
 
 
 
 
 
 
 
5a69a35
f070ffb
5a69a35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
875ef36
5a69a35
 
 
 
875ef36
5a69a35
 
 
875ef36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- 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()