Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from flask import Flask, request, jsonify, render_template
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 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.environ.get(HF_TOKEN") # Set your Hugging Face API key
|
| 10 |
+
|
| 11 |
+
# Chevereto Configuration
|
| 12 |
+
CHEVERETO_API_URL = os.environ.get("API_URL")
|
| 13 |
+
CHEVERETO_API_KEY = os.environ.get("API_KEY")
|
| 14 |
+
CHEVERETO_ALBUM_ID = os.environ.get("ALBUM_ID")
|
| 15 |
+
|
| 16 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 17 |
+
|
| 18 |
+
def generate_image_and_upload(prompt: str) -> str:
|
| 19 |
+
"""Generates an image using Hugging Face API and uploads it to Chevereto."""
|
| 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 = "static/generated_image.png"
|
| 26 |
+
|
| 27 |
+
with open(image_path, "wb") as f:
|
| 28 |
+
f.write(image_data)
|
| 29 |
+
|
| 30 |
+
return upload_to_chevereto(image_path) # Upload and return URL
|
| 31 |
+
else:
|
| 32 |
+
return f"Error generating image: {response.text}"
|
| 33 |
+
|
| 34 |
+
def upload_to_chevereto(img_path: str) -> str:
|
| 35 |
+
"""Uploads an image to Chevereto and returns the public image URL."""
|
| 36 |
+
|
| 37 |
+
with open(img_path, "rb") as image_file:
|
| 38 |
+
files = {"source": image_file}
|
| 39 |
+
data = {
|
| 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:
|
| 47 |
+
return response.json().get("image", {}).get("url")
|
| 48 |
+
else:
|
| 49 |
+
return f"Error uploading image: {response.text}"
|
| 50 |
+
|
| 51 |
+
@app.route("/", methods=["GET"])
|
| 52 |
+
def index():
|
| 53 |
+
"""Renders the Star Trek-themed UI."""
|
| 54 |
+
return render_template("index.html")
|
| 55 |
+
|
| 56 |
+
@app.route("/generate", methods=["POST"])
|
| 57 |
+
def generate():
|
| 58 |
+
"""API Endpoint: Generate an image from a text prompt and upload it."""
|
| 59 |
+
data = request.get_json()
|
| 60 |
+
prompt = data.get("prompt", "")
|
| 61 |
+
|
| 62 |
+
if not prompt:
|
| 63 |
+
return jsonify({"error": "Missing prompt"}), 400
|
| 64 |
+
|
| 65 |
+
image_url = generate_image_and_upload(prompt)
|
| 66 |
+
return jsonify({"image_url": image_url})
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
app.run(host="0.0.0.0", port=7680)
|