martynka commited on
Commit
568361d
·
verified ·
1 Parent(s): b622977

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -1,6 +1,8 @@
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"
@@ -10,16 +12,26 @@ HF_API_KEY = os.getenv("HF_TOKEN")
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)
@@ -41,8 +53,11 @@ def upload_to_chevereto(image_path: str):
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 both the image path and Chevereto URL"""
 
 
 
46
  img_path = generate_image(prompt)
47
  if "Error" in img_path:
48
  return img_path, "" # Return error message and empty string for URL
@@ -55,6 +70,7 @@ with gr.Blocks() as app:
55
  gr.Markdown("<h1 style='text-align: center; color: red;'>🚀 Star Trek AI Image Generator</h1>")
56
  with gr.Row():
57
  prompt_input = gr.Textbox(label="Enter a prompt:", placeholder="A futuristic spaceship...")
 
58
  generate_button = gr.Button("Generate & Upload")
59
 
60
  image_output = gr.Image(label="Generated Image")
@@ -62,7 +78,7 @@ with gr.Blocks() as app:
62
 
63
  generate_button.click(
64
  fn=generate_and_upload,
65
- inputs=prompt_input,
66
  outputs=[image_output, url_output]
67
  )
68
 
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ import uuid
5
+ import ps
6
 
7
  # Hugging Face API
8
  HF_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3.5-large"
 
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
+ def generate_random_filename(extension: str) -> str:
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
  """Generates an image using Hugging Face's API"""
29
  response = requests.post(HF_API_URL, headers=HEADERS, json={"inputs": prompt})
30
 
31
  if response.status_code == 200:
32
  image_data = response.content
33
+ random_filename = generate_random_filename(".png")
34
+ image_path = f"/tmp/{random_filename}"
35
 
36
  with open(image_path, "wb") as f:
37
  f.write(image_data)
 
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", "" # Return an error message
60
+
61
  img_path = generate_image(prompt)
62
  if "Error" in img_path:
63
  return img_path, "" # Return error message and empty string for URL
 
70
  gr.Markdown("<h1 style='text-align: center; color: red;'>🚀 Star Trek AI Image Generator</h1>")
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
 
76
  image_output = gr.Image(label="Generated Image")
 
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