martynka commited on
Commit
bcbe36c
·
verified ·
1 Parent(s): 0dd7450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -1
app.py CHANGED
@@ -1,3 +1,44 @@
1
  import gradio as gr
 
 
 
 
 
 
 
2
 
3
- gr.load("models/stabilityai/stable-diffusion-3.5-large").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ inport os
6
+ # Load Stable Diffusion model
7
+ HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None
8
+ model = gr.load("models/stabilityai/stable-diffusion-3.5-large", hf_token=HF_TOKEN)
9
 
10
+ # Chevereto API details
11
+ CHEVERETO_API_URL = os.environ.get("api_url") # Replace with your Chevereto API endpoint
12
+ CHEVERETO_API_KEY = os.environ.get("API_KEY") # Replace with your API key
13
+ CHEVERETO_ALBUM_ID = os.environ.get("ALBUM_ID") # Replace with the album ID where you want to store images
14
+
15
+ # Upload image to Chevereto
16
+ def upload_to_chevereto(image: Image.Image) -> str:
17
+ # Convert image to bytes
18
+ buffered = BytesIO()
19
+ image.save(buffered, format="PNG")
20
+ files = {"source": buffered.getvalue()} # Convert to binary for upload
21
+
22
+ # Set API parameters
23
+ data = {
24
+ "key": CHEVERETO_API_KEY,
25
+ "format": "json",
26
+ "album": CHEVERETO_ALBUM_ID # Specify album ID
27
+ }
28
+
29
+ # Upload request
30
+ response = requests.post(CHEVERETO_API_URL, files=files, data=data)
31
+
32
+ if response.status_code == 200:
33
+ return response.json()["image"]["url"] # Return public URL of uploaded image
34
+ return "Error uploading image"
35
+
36
+ # Generate and upload the image
37
+ def generate_image_and_upload(prompt: str) -> str:
38
+ img = model.predict(prompt)[0] # Get generated image
39
+ return upload_to_chevereto(img) # Upload and return the URL
40
+
41
+ # Define Gradio API returning the Chevereto public URL
42
+ iface = gr.Interface(fn=generate_image_and_upload, inputs="text", outputs="text")
43
+
44
+ iface.launch()