|
import gradio as gr |
|
import requests |
|
import os |
|
from PIL import Image |
|
import os |
|
|
|
CHEVERETO_API_URL = os.environ.get("API_URL") |
|
CHEVERETO_API_KEY = os.environ.get("API_KEY") |
|
CHEVERETO_ALBUM_ID = os.environ.get("ALBUM_ID") |
|
|
|
|
|
model = gr.Interface.load("models/stabilityai/stable-diffusion-3.5-large") |
|
|
|
def upload_to_chevereto(img_path: str) -> str: |
|
"""Uploads an image to Chevereto and returns the public image URL.""" |
|
with open(img_path, "rb") as image_file: |
|
files = {"source": image_file} |
|
data = { |
|
"key": CHEVERETO_API_KEY, |
|
"format": "json", |
|
"album": CHEVERETO_ALBUM_ID |
|
} |
|
response = requests.post(CHEVERETO_API_URL, files=files, data=data) |
|
|
|
if response.status_code == 200: |
|
return response.json().get("image", {}).get("url") |
|
else: |
|
return f"Error uploading image: {response.text}" |
|
|
|
def generate_image_and_upload(prompt: str) -> str: |
|
"""Generates an image using the AI model and uploads it to Chevereto.""" |
|
img = model(prompt) |
|
|
|
if isinstance(img, list): |
|
img = img[0] |
|
|
|
if isinstance(img, Image.Image): |
|
img_path = "generated_image.png" |
|
img.save(img_path, format="PNG") |
|
|
|
return upload_to_chevereto(img_path) |
|
else: |
|
return "Error: Model did not return a valid image" |
|
|
|
|
|
|
|
|
|
iface = gr.Interface(fn=generate_image_and_upload, inputs="text", outputs="text") |
|
|
|
iface.launch() |
|
|