File size: 1,633 Bytes
5a69a35
f070ffb
5a69a35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31e9a5d
 
 
 
5a69a35
 
 
31e9a5d
 
5a69a35
 
 
 
31e9a5d
5a69a35
 
 
5f0d084
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
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()