|
import gradio as gr |
|
import requests |
|
from io import BytesIO |
|
from PIL import Image |
|
inport os |
|
|
|
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_URL = os.environ.get("api_url") |
|
CHEVERETO_API_KEY = os.environ.get("API_KEY") |
|
CHEVERETO_ALBUM_ID = os.environ.get("ALBUM_ID") |
|
|
|
|
|
def upload_to_chevereto(image: Image.Image) -> str: |
|
|
|
buffered = BytesIO() |
|
image.save(buffered, format="PNG") |
|
files = {"source": buffered.getvalue()} |
|
|
|
|
|
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()["image"]["url"] |
|
return "Error uploading image" |
|
|
|
|
|
def generate_image_and_upload(prompt: str) -> str: |
|
img = model.predict(prompt)[0] |
|
return upload_to_chevereto(img) |
|
|
|
|
|
iface = gr.Interface(fn=generate_image_and_upload, inputs="text", outputs="text") |
|
|
|
iface.launch() |
|
|