Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,39 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Hugging Face API Configuration
|
| 8 |
HF_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large"
|
| 9 |
-
HF_API_KEY = os.
|
| 10 |
|
| 11 |
-
# Chevereto
|
| 12 |
-
CHEVERETO_API_URL = os.
|
| 13 |
-
CHEVERETO_API_KEY = os.
|
| 14 |
-
CHEVERETO_ALBUM_ID = os.
|
| 15 |
|
| 16 |
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
"""Generates an image using Hugging Face API
|
| 20 |
-
|
| 21 |
response = requests.post(HF_API_URL, headers=HEADERS, json={"inputs": prompt})
|
| 22 |
-
|
| 23 |
if response.status_code == 200:
|
| 24 |
image_data = response.content
|
| 25 |
-
image_path = "
|
| 26 |
-
|
| 27 |
with open(image_path, "wb") as f:
|
| 28 |
f.write(image_data)
|
| 29 |
|
| 30 |
-
return
|
| 31 |
else:
|
| 32 |
return f"Error generating image: {response.text}"
|
| 33 |
|
| 34 |
-
def upload_to_chevereto(
|
| 35 |
-
"""Uploads
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
"key": CHEVERETO_API_KEY,
|
| 41 |
-
"format": "json",
|
| 42 |
-
"album": CHEVERETO_ALBUM_ID
|
| 43 |
-
}
|
| 44 |
response = requests.post(CHEVERETO_API_URL, files=files, data=data)
|
| 45 |
|
| 46 |
if response.status_code == 200:
|
|
@@ -48,22 +41,27 @@ def upload_to_chevereto(img_path: str) -> str:
|
|
| 48 |
else:
|
| 49 |
return f"Error uploading image: {response.text}"
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
app.run(host="0.0.0.0", port=7680)
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# Hugging Face API
|
|
|
|
|
|
|
| 6 |
HF_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large"
|
| 7 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 8 |
|
| 9 |
+
# Chevereto API
|
| 10 |
+
CHEVERETO_API_URL = os.getenv("API_URL")
|
| 11 |
+
CHEVERETO_API_KEY = os.getenv("API_KEY")
|
| 12 |
+
CHEVERETO_ALBUM_ID = os.getenv("ALBUM_ID")
|
| 13 |
|
| 14 |
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 15 |
|
| 16 |
+
def generate_image(prompt: str):
|
| 17 |
+
"""Generates an image using Hugging Face's API"""
|
|
|
|
| 18 |
response = requests.post(HF_API_URL, headers=HEADERS, json={"inputs": prompt})
|
| 19 |
+
|
| 20 |
if response.status_code == 200:
|
| 21 |
image_data = response.content
|
| 22 |
+
image_path = "/tmp/generated_image.png"
|
| 23 |
+
|
| 24 |
with open(image_path, "wb") as f:
|
| 25 |
f.write(image_data)
|
| 26 |
|
| 27 |
+
return image_path
|
| 28 |
else:
|
| 29 |
return f"Error generating image: {response.text}"
|
| 30 |
|
| 31 |
+
def upload_to_chevereto(image_path: str):
|
| 32 |
+
"""Uploads the generated image to Chevereto and returns the URL"""
|
| 33 |
+
with open(image_path, "rb") as img_file:
|
| 34 |
+
files = {"source": img_file}
|
| 35 |
+
data = {"key": CHEVERETO_API_KEY, "format": "json", "album": CHEVERETO_ALBUM_ID}
|
| 36 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
response = requests.post(CHEVERETO_API_URL, files=files, data=data)
|
| 38 |
|
| 39 |
if response.status_code == 200:
|
|
|
|
| 41 |
else:
|
| 42 |
return f"Error uploading image: {response.text}"
|
| 43 |
|
| 44 |
+
def generate_and_upload(prompt: str):
|
| 45 |
+
"""Combines image generation + upload and returns Chevereto URL"""
|
| 46 |
+
img_path = generate_image(prompt)
|
| 47 |
+
if "Error" in img_path:
|
| 48 |
+
return img_path
|
| 49 |
+
return upload_to_chevereto(img_path)
|
| 50 |
|
| 51 |
+
# Gradio UI
|
| 52 |
+
with gr.Blocks() as app:
|
| 53 |
+
gr.Markdown("<h1 style='text-align: center; color: red;'>🚀 Star Trek AI Image Generator</h1>")
|
| 54 |
+
with gr.Row():
|
| 55 |
+
prompt_input = gr.Textbox(label="Enter a prompt:", placeholder="A futuristic spaceship...")
|
| 56 |
+
generate_button = gr.Button("Generate & Upload")
|
| 57 |
+
|
| 58 |
+
image_output = gr.Image(label="Generated Image")
|
| 59 |
+
url_output = gr.Textbox(label="Image URL", interactive=False)
|
| 60 |
|
| 61 |
+
generate_button.click(
|
| 62 |
+
fn=generate_and_upload,
|
| 63 |
+
inputs=prompt_input,
|
| 64 |
+
outputs=[image_output, url_output]
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
app.launch(server_port=7860)
|
|
|