Spaces:
Running
Running
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() | |