Spaces:
Running
Running
File size: 1,611 Bytes
020d89a bcbe36c 020d89a bcbe36c |
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 |
import gradio as gr
import requests
from io import BytesIO
from PIL import Image
inport os
# Load Stable Diffusion model
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None
model = gr.load("models/stabilityai/stable-diffusion-3.5-large", hf_token=HF_TOKEN)
# Chevereto API details
CHEVERETO_API_URL = os.environ.get("api_url") # Replace with your Chevereto API endpoint
CHEVERETO_API_KEY = os.environ.get("API_KEY") # Replace with your API key
CHEVERETO_ALBUM_ID = os.environ.get("ALBUM_ID") # Replace with the album ID where you want to store images
# Upload image to Chevereto
def upload_to_chevereto(image: Image.Image) -> str:
# Convert image to bytes
buffered = BytesIO()
image.save(buffered, format="PNG")
files = {"source": buffered.getvalue()} # Convert to binary for upload
# Set API parameters
data = {
"key": CHEVERETO_API_KEY,
"format": "json",
"album": CHEVERETO_ALBUM_ID # Specify album ID
}
# Upload request
response = requests.post(CHEVERETO_API_URL, files=files, data=data)
if response.status_code == 200:
return response.json()["image"]["url"] # Return public URL of uploaded image
return "Error uploading image"
# Generate and upload the image
def generate_image_and_upload(prompt: str) -> str:
img = model.predict(prompt)[0] # Get generated image
return upload_to_chevereto(img) # Upload and return the URL
# Define Gradio API returning the Chevereto public URL
iface = gr.Interface(fn=generate_image_and_upload, inputs="text", outputs="text")
iface.launch()
|