Nymbo commited on
Commit
f4ba679
·
verified ·
1 Parent(s): 7ed5d16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -45
app.py CHANGED
@@ -3,8 +3,9 @@ from PIL import Image
3
  import requests
4
  import io
5
  import base64
 
6
 
7
- # List of models to process
8
  models = [
9
  "dreamlike-art/dreamlike-photoreal-2.0",
10
  "stabilityai/stable-diffusion-xl-base-1.0",
@@ -69,59 +70,75 @@ models = [
69
  "Yntec/epiCPhotoGasm",
70
  ]
71
 
72
- # Initialize a dictionary to track models and their status
73
- model_results = {"success": {}, "failed": []}
 
74
 
75
- def generate_images(prompt):
76
- output_images = []
77
- failed_models = []
78
- for model_name in models:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  try:
80
- # Attempt to load and generate an image with the model
81
- model = gr.Interface.load(f"models/{model_name}")
82
- img_path = model.predict(prompt)
 
 
83
 
84
- # Fetch image
85
- response = requests.get(img_path, stream=True)
86
- if response.status_code == 200:
87
- img = Image.open(io.BytesIO(response.content))
88
- output_images.append((model_name, img))
89
- else:
90
- failed_models.append(model_name)
91
  except Exception as e:
92
- print(f"Error with model {model_name}: {e}")
93
- failed_models.append(model_name)
94
 
95
- return output_images, failed_models
 
96
 
97
- def app_interface(prompt):
98
- output_images, failed_models = generate_images(prompt)
99
- images_html = ""
100
- for model_name, img in output_images:
101
- # Convert images for display in Gradio
102
- img_buffer = io.BytesIO()
103
- img.save(img_buffer, format="PNG")
104
- img_str = base64.b64encode(img_buffer.getvalue()).decode()
105
- images_html += f"""
106
- <div>
107
- <strong>{model_name}</strong>
108
- <img src='data:image/png;base64,{img_str}' style="max-width: 200px;" />
109
- </div>
110
- """
111
-
112
- failed_html = "<ul>" + "".join(f"<li>{model}</li>" for model in failed_models) + "</ul>"
113
- return images_html, f"<h3>Failed Models:</h3>{failed_html}"
114
 
115
- # Define Gradio UI
116
- with gr.Blocks() as app:
117
  with gr.Row():
118
  with gr.Column():
119
- prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt")
120
- generate_button = gr.Button("Generate Images")
121
- with gr.Column():
122
- output_html = gr.HTML(label="Generated Images")
123
- failed_html = gr.HTML(label="Failed Models")
124
 
125
- generate_button.click(app_interface, inputs=[prompt_input], outputs=[output_html, failed_html])
 
 
126
 
127
  app.launch()
 
3
  import requests
4
  import io
5
  import base64
6
+ import threading
7
 
8
+ # List of models
9
  models = [
10
  "dreamlike-art/dreamlike-photoreal-2.0",
11
  "stabilityai/stable-diffusion-xl-base-1.0",
 
70
  "Yntec/epiCPhotoGasm",
71
  ]
72
 
73
+ # Load models
74
+ loaded_models = []
75
+ available_models = []
76
 
77
+ for model in models:
78
+ try:
79
+ loaded_models.append(gr.load(f'models/{model}'))
80
+ available_models.append(model)
81
+ except Exception as e:
82
+ print(f"Model {model} could not be loaded: {e}")
83
+
84
+ print(loaded_models)
85
+ print(available_models)
86
+
87
+ def generate_image(prompt, model_index):
88
+ try:
89
+ model = loaded_models[model_index]
90
+ out_img = model(prompt)
91
+ print(out_img)
92
+ return out_img
93
+ except Exception as e:
94
+ print(e)
95
+ return None
96
+
97
+ def run_all_models(prompt):
98
+ out_box = []
99
+ out_html = ""
100
+
101
+ for i, model in enumerate(loaded_models):
102
  try:
103
+ out_img = generate_image(prompt, i)
104
+ if out_img:
105
+ raw = Image.open(out_img)
106
+ raw = raw.convert('RGB')
107
+ out_box.append(raw)
108
 
109
+ # Convert image to base64 for HTML display
110
+ buffered = io.BytesIO()
111
+ raw.save(buffered, format="PNG")
112
+ img_str = base64.b64encode(buffered.getvalue()).decode()
113
+ img_tag = f"<img src='data:image/png;base64,{img_str}'/>"
114
+ out_html += f"<div class='img_class'><a href='https://huggingface.co/models/{available_models[i]}'>{available_models[i]}</a><br>{img_tag}</div>"
 
115
  except Exception as e:
116
+ print(e)
117
+ out_html += f"<div class='img_class'>{available_models[i]}: {str(e)}</div>"
118
 
119
+ html_out = f"<div class='grid_class'>{out_html}</div>"
120
+ return out_box, html_out
121
 
122
+ css = """
123
+ .grid_class {
124
+ display: flex;
125
+ flex-wrap: wrap;
126
+ gap: 10px;
127
+ }
128
+ .img_class {
129
+ min-width: 200px;
130
+ margin: 5px;
131
+ }
132
+ """
 
 
 
 
 
 
133
 
134
+ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as app:
 
135
  with gr.Row():
136
  with gr.Column():
137
+ inp = gr.Textbox(label="Prompt")
138
+ btn = gr.Button("Generate Images")
 
 
 
139
 
140
+ out_html = gr.HTML()
141
+ outp = gr.Gallery()
142
+ btn.click(run_all_models, [inp], [outp, out_html])
143
 
144
  app.launch()