PuristanLabs1 commited on
Commit
d66c64b
·
verified ·
1 Parent(s): 01c7fea

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -110
app.py DELETED
@@ -1,110 +0,0 @@
1
- import gradio as gr
2
- from together import Together
3
- from PIL import Image
4
- import requests
5
- from io import BytesIO
6
- import os
7
- from datetime import datetime
8
-
9
- # Retrieve API key from Hugging Face Secrets
10
- API_KEY = os.getenv("MY_API_KEY")
11
- if not API_KEY:
12
- print("Error: API_KEY not found. Please check your Hugging Face secrets.")
13
- client = Together(api_key=API_KEY)
14
-
15
- # Ensure directory for saving user-generated images
16
- IMAGE_FOLDER = "./user_images"
17
- os.makedirs(IMAGE_FOLDER, exist_ok=True)
18
-
19
- # Function to generate an image and save it
20
- def generate_and_save_image(prompt):
21
- try:
22
- if not prompt.strip():
23
- print("Empty prompt received.")
24
- return None, None # Avoid empty prompts
25
-
26
- print(f"Generating image for: {prompt}")
27
-
28
- # Generate image with Together AI API
29
- response = client.images.generate(
30
- prompt=prompt,
31
- model="black-forest-labs/FLUX.1-schnell", # Model name
32
- steps=4
33
- )
34
- print(f"Response: {response}")
35
-
36
- # Fetch the image from the returned URL
37
- image_url = response.data[0].url
38
- print(f"Image URL: {image_url}")
39
- response_image = requests.get(image_url)
40
- image = Image.open(BytesIO(response_image.content))
41
-
42
- # Save the image locally
43
- timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
44
- image_path = os.path.join(IMAGE_FOLDER, f"{timestamp}.png")
45
- image.save(image_path)
46
- print(f"Image successfully saved at: {image_path}")
47
-
48
- return image, image_path # Return the generated image and its path
49
- except Exception as e:
50
- print("Error during image generation or saving:", e)
51
- return None, None
52
-
53
- # Function to get all saved images for the gallery
54
- def get_gallery_images():
55
- try:
56
- images = []
57
- for filename in sorted(os.listdir(IMAGE_FOLDER)):
58
- if filename.endswith(".png"):
59
- images.append(os.path.join(IMAGE_FOLDER, filename))
60
- print(f"Gallery images: {images}")
61
- return images
62
- except Exception as e:
63
- print("Error fetching gallery images:", e)
64
- return []
65
-
66
- # Function to zip all images and provide for download
67
- def download_gallery():
68
- from shutil import make_archive
69
- zip_path = os.path.join(IMAGE_FOLDER, "gallery")
70
- make_archive(zip_path, 'zip', IMAGE_FOLDER)
71
- return f"{zip_path}.zip"
72
-
73
- # Gradio Interface Setup
74
- with gr.Blocks() as app:
75
- gr.Markdown("## Real-Time AI Image Generator 🚀")
76
- gr.Markdown("Start typing a prompt below to generate images in real-time with Together AI's FLUX model. Your creations will be displayed below in the gallery.")
77
-
78
- with gr.Row():
79
- prompt_box = gr.Textbox(label="Enter your prompt", placeholder="A horse on the moon")
80
- image_output = gr.Image(label="Generated Image")
81
-
82
- # Add a gallery section
83
- with gr.Row():
84
- gallery = gr.Gallery(label="Generated with Love by You", height="auto")
85
-
86
- # Add a download button for the gallery
87
- with gr.Row():
88
- download_button = gr.File(label="Download Your Gallery")
89
-
90
- def process_and_update_gallery(prompt):
91
- image, image_path = generate_and_save_image(prompt)
92
- gallery_images = get_gallery_images()
93
- logging.debug(f"Updating gallery with {len(gallery_images)} images")
94
- return image, gallery_images
95
-
96
- # Trigger zip file creation for download
97
- def prepare_download():
98
- return download_gallery()
99
-
100
- prompt_box.change(
101
- fn=process_and_update_gallery,
102
- inputs=prompt_box,
103
- outputs=[image_output, gallery]
104
- )
105
-
106
- # Bind download button to zip creation
107
- download_button.change(fn=prepare_download, inputs=[], outputs=download_button)
108
-
109
- # Launch the Gradio app
110
- app.launch()