Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,29 @@
|
|
1 |
import os
|
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
import uuid
|
5 |
-
import os
|
6 |
|
7 |
# Hugging Face API
|
8 |
-
HF_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large"
|
9 |
HF_API_KEY = os.getenv("HF_TOKEN")
|
10 |
|
11 |
# Chevereto API
|
12 |
CHEVERETO_API_URL = os.getenv("API_URL")
|
13 |
CHEVERETO_API_KEY = os.getenv("API_KEY")
|
14 |
CHEVERETO_ALBUM_ID = os.getenv("ALBUM_ID")
|
15 |
-
|
16 |
-
"""Generates a random filename with the given extension"""
|
17 |
-
return str(uuid.uuid4()) + extension # Example: 'b3d50c6d-bfdb-4c4b-b8c4-3a2e3c3c07d9.png'
|
18 |
-
# Set your app's secret API key here (you can also load it from the environment)
|
19 |
-
EXPECTED_API_KEY = os.getenv("API_PASS") # or use a hardcoded key for now
|
20 |
|
21 |
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
22 |
|
|
|
|
|
|
|
23 |
def validate_api_key(api_key: str) -> bool:
|
24 |
-
"""Validates the provided API key"""
|
25 |
return api_key == EXPECTED_API_KEY
|
26 |
|
27 |
-
def generate_image(prompt: str):
|
28 |
-
|
29 |
-
response = requests.post(
|
30 |
|
31 |
if response.status_code == 200:
|
32 |
image_data = response.content
|
@@ -36,15 +33,15 @@ def generate_image(prompt: str):
|
|
36 |
with open(image_path, "wb") as f:
|
37 |
f.write(image_data)
|
38 |
|
|
|
39 |
return image_path
|
40 |
else:
|
41 |
return f"Error generating image: {response.text}"
|
42 |
|
43 |
def upload_to_chevereto(image_path: str):
|
44 |
-
"""Uploads the generated image to Chevereto and returns the URL"""
|
45 |
with open(image_path, "rb") as img_file:
|
46 |
files = {"source": img_file}
|
47 |
-
data = {"key": CHEVERETO_API_KEY, "format": "json", "album_id": CHEVERETO_ALBUM_ID,"expiration": "PT10M"}
|
48 |
|
49 |
response = requests.post(CHEVERETO_API_URL, files=files, data=data)
|
50 |
|
@@ -53,23 +50,23 @@ def upload_to_chevereto(image_path: str):
|
|
53 |
else:
|
54 |
return f"Error uploading image: {response.text}"
|
55 |
|
56 |
-
def generate_and_upload(prompt: str, api_key: str):
|
57 |
-
"""Validates API key, generates image, and uploads it to Chevereto"""
|
58 |
if not validate_api_key(api_key):
|
59 |
-
return "Invalid or missing API key", ""
|
60 |
|
61 |
-
img_path = generate_image(prompt)
|
62 |
if "Error" in img_path:
|
63 |
-
return img_path, ""
|
64 |
|
65 |
url = upload_to_chevereto(img_path)
|
66 |
-
return img_path, url
|
67 |
|
68 |
# Gradio UI
|
69 |
with gr.Blocks() as app:
|
70 |
-
gr.Markdown("<h1 style='text-align: center; color: red;'>🚀
|
71 |
with gr.Row():
|
72 |
prompt_input = gr.Textbox(label="Enter a prompt:", placeholder="A futuristic spaceship...")
|
|
|
73 |
api_key_input = gr.Textbox(label="Enter your API key:", type="password", placeholder="Your secret API key")
|
74 |
generate_button = gr.Button("Generate & Upload")
|
75 |
|
@@ -78,7 +75,7 @@ with gr.Blocks() as app:
|
|
78 |
|
79 |
generate_button.click(
|
80 |
fn=generate_and_upload,
|
81 |
-
inputs=[prompt_input, api_key_input],
|
82 |
outputs=[image_output, url_output]
|
83 |
)
|
84 |
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
import requests
|
4 |
import gradio as gr
|
5 |
import uuid
|
|
|
6 |
|
7 |
# Hugging Face API
|
|
|
8 |
HF_API_KEY = os.getenv("HF_TOKEN")
|
9 |
|
10 |
# Chevereto API
|
11 |
CHEVERETO_API_URL = os.getenv("API_URL")
|
12 |
CHEVERETO_API_KEY = os.getenv("API_KEY")
|
13 |
CHEVERETO_ALBUM_ID = os.getenv("ALBUM_ID")
|
14 |
+
EXPECTED_API_KEY = os.getenv("API_PASS")
|
|
|
|
|
|
|
|
|
15 |
|
16 |
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
17 |
|
18 |
+
def generate_random_filename(extension: str) -> str:
|
19 |
+
return str(uuid.uuid4()) + extension
|
20 |
+
|
21 |
def validate_api_key(api_key: str) -> bool:
|
|
|
22 |
return api_key == EXPECTED_API_KEY
|
23 |
|
24 |
+
def generate_image(prompt: str, model_name: str):
|
25 |
+
hf_api_url = f"https://api-inference.huggingface.co/models/{model_name}"
|
26 |
+
response = requests.post(hf_api_url, headers=HEADERS, json={"inputs": prompt})
|
27 |
|
28 |
if response.status_code == 200:
|
29 |
image_data = response.content
|
|
|
33 |
with open(image_path, "wb") as f:
|
34 |
f.write(image_data)
|
35 |
|
36 |
+
time.sleep(10) # 10-sekundowa przerwa
|
37 |
return image_path
|
38 |
else:
|
39 |
return f"Error generating image: {response.text}"
|
40 |
|
41 |
def upload_to_chevereto(image_path: str):
|
|
|
42 |
with open(image_path, "rb") as img_file:
|
43 |
files = {"source": img_file}
|
44 |
+
data = {"key": CHEVERETO_API_KEY, "format": "json", "album_id": CHEVERETO_ALBUM_ID, "expiration": "PT10M"}
|
45 |
|
46 |
response = requests.post(CHEVERETO_API_URL, files=files, data=data)
|
47 |
|
|
|
50 |
else:
|
51 |
return f"Error uploading image: {response.text}"
|
52 |
|
53 |
+
def generate_and_upload(prompt: str, model_name: str, api_key: str):
|
|
|
54 |
if not validate_api_key(api_key):
|
55 |
+
return "Invalid or missing API key", ""
|
56 |
|
57 |
+
img_path = generate_image(prompt, model_name)
|
58 |
if "Error" in img_path:
|
59 |
+
return img_path, ""
|
60 |
|
61 |
url = upload_to_chevereto(img_path)
|
62 |
+
return img_path, url
|
63 |
|
64 |
# Gradio UI
|
65 |
with gr.Blocks() as app:
|
66 |
+
gr.Markdown("<h1 style='text-align: center; color: red;'>🚀 SD API on Tasia Space 🚀</h1>")
|
67 |
with gr.Row():
|
68 |
prompt_input = gr.Textbox(label="Enter a prompt:", placeholder="A futuristic spaceship...")
|
69 |
+
model_input = gr.Textbox(label="Enter model name:", placeholder="stabilityai/stable-diffusion-3.5-large")
|
70 |
api_key_input = gr.Textbox(label="Enter your API key:", type="password", placeholder="Your secret API key")
|
71 |
generate_button = gr.Button("Generate & Upload")
|
72 |
|
|
|
75 |
|
76 |
generate_button.click(
|
77 |
fn=generate_and_upload,
|
78 |
+
inputs=[prompt_input, model_input, api_key_input],
|
79 |
outputs=[image_output, url_output]
|
80 |
)
|
81 |
|