martynka commited on
Commit
dd27f57
·
verified ·
1 Parent(s): e6414e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -42
app.py CHANGED
@@ -1,46 +1,39 @@
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:
@@ -48,22 +41,27 @@ def upload_to_chevereto(img_path: str) -> str:
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)
 
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)