Spaces:
Build error
Build error
Update app2.py
Browse files
app2.py
CHANGED
|
@@ -19,7 +19,6 @@ rep_client = replicate.Client()
|
|
| 19 |
OPENAI_API_KEY = "sk-proj-5iy4bwrqAW8GpguiEawaT3BlbkFJ8p88lLSjOCeDbxWsAOlr"
|
| 20 |
openai.api_key = OPENAI_API_KEY
|
| 21 |
|
| 22 |
-
# Predefined prompts for the dropdown
|
| 23 |
predefined_prompts = [
|
| 24 |
"Missing bolts on railway track",
|
| 25 |
"Cracks on railway track",
|
|
@@ -45,7 +44,6 @@ def ask_rail_defect_question(question, model_name='ft:gpt-3.5-turbo-0125:persona
|
|
| 45 |
)
|
| 46 |
return response.choices[0].message['content']
|
| 47 |
|
| 48 |
-
# Function to generate variations enhanced by the GPT model
|
| 49 |
def generate_variations(base_prompt, number_of_variations):
|
| 50 |
locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
|
| 51 |
sizes = ["small", "medium", "large", "tiny", "huge"]
|
|
@@ -57,52 +55,10 @@ def generate_variations(base_prompt, number_of_variations):
|
|
| 57 |
size = random.choice(sizes)
|
| 58 |
weather = random.choice(weather_conditions)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
enhanced_prompt = base_prompt
|
| 62 |
-
full_prompt = f"{enhanced_prompt}, with a {size} defect {location}, observed {weather}."
|
| 63 |
variations.append(full_prompt)
|
| 64 |
return variations
|
| 65 |
|
| 66 |
-
def image_to_data_url(image):
|
| 67 |
-
buffered = io.BytesIO()
|
| 68 |
-
image.save(buffered, format="PNG")
|
| 69 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 70 |
-
return f"data:image/png;base64,{img_str}"
|
| 71 |
-
|
| 72 |
-
# Function to inpaint images
|
| 73 |
-
def inpaint_defect(image, prompt, num_images=1):
|
| 74 |
-
if isinstance(image, np.ndarray):
|
| 75 |
-
image = Image.fromarray(image)
|
| 76 |
-
|
| 77 |
-
image_data_url = image_to_data_url(image)
|
| 78 |
-
images = []
|
| 79 |
-
|
| 80 |
-
for _ in range(num_images):
|
| 81 |
-
input = {
|
| 82 |
-
"input_image": image_data_url,
|
| 83 |
-
"instruction_text": prompt,
|
| 84 |
-
"scheduler": "K_EULER_ANCESTRAL",
|
| 85 |
-
"num_outputs": 1,
|
| 86 |
-
"guidance_scale": 7.5,
|
| 87 |
-
"num_inference_steps": 100,
|
| 88 |
-
"image_guidance_scale": 1.5
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
prediction = rep_client.predictions.create(
|
| 92 |
-
version="10e63b0e6361eb23a0374f4d9ee145824d9d09f7a31dcd70803193ebc7121430",
|
| 93 |
-
input=input
|
| 94 |
-
)
|
| 95 |
-
prediction.wait()
|
| 96 |
-
if prediction.status == "succeeded":
|
| 97 |
-
image_url = prediction.output[0]
|
| 98 |
-
response = requests.get(image_url)
|
| 99 |
-
img = Image.open(io.BytesIO(response.content))
|
| 100 |
-
images.append(img)
|
| 101 |
-
else:
|
| 102 |
-
images.append(None)
|
| 103 |
-
return images
|
| 104 |
-
|
| 105 |
-
# Function to generate images from prompts
|
| 106 |
def generate_images(prompts):
|
| 107 |
images = []
|
| 108 |
for prompt in prompts:
|
|
@@ -120,87 +76,41 @@ def generate_images(prompts):
|
|
| 120 |
images.append(f"Error: {str(e)}")
|
| 121 |
return images
|
| 122 |
|
| 123 |
-
def process_railway_defects(prompt, number_of_images):
|
| 124 |
-
variations = generate_variations(prompt, number_of_images)
|
| 125 |
-
images = generate_images(variations)
|
| 126 |
-
return images
|
| 127 |
-
|
| 128 |
-
def download_images_as_zip(images):
|
| 129 |
-
zip_buffer = io.BytesIO()
|
| 130 |
-
with zipfile.ZipFile(zip_buffer, 'w') as zf:
|
| 131 |
-
for i, img in enumerate(images):
|
| 132 |
-
img_buffer = io.BytesIO()
|
| 133 |
-
img.save(img_buffer, format='PNG')
|
| 134 |
-
img_buffer.seek(0)
|
| 135 |
-
zf.writestr(f'image_{i + 1}.png', img_buffer.read())
|
| 136 |
-
zip_buffer.seek(0)
|
| 137 |
-
return zip_buffer
|
| 138 |
-
|
| 139 |
# UI creation
|
| 140 |
with gr.Blocks() as app:
|
| 141 |
with gr.Tabs("Prompt Input"):
|
| 142 |
-
with gr.Tab("
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
submit_button_dropdown.click(
|
| 154 |
-
fn=on_submit_click_dropdown,
|
| 155 |
-
inputs=[prompt_input, number_input_dropdown],
|
| 156 |
-
outputs=image_outputs_dropdown
|
| 157 |
)
|
| 158 |
|
| 159 |
with gr.Tab("Custom Defect"):
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
submit_button_custom = gr.Button("Generate")
|
| 164 |
image_outputs_custom = gr.Gallery()
|
| 165 |
|
| 166 |
-
def on_submit_click_custom(custom_prompt, number_of_images):
|
| 167 |
-
images = process_railway_defects(custom_prompt, number_of_images)
|
| 168 |
-
return images
|
| 169 |
-
|
| 170 |
submit_button_custom.click(
|
| 171 |
-
fn=
|
| 172 |
inputs=[custom_prompt_input, number_input_custom],
|
| 173 |
outputs=image_outputs_custom
|
| 174 |
)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
submit_button_inpaint = gr.Button("Inpaint Defect")
|
| 182 |
-
inpainted_image_output = gr.Gallery()
|
| 183 |
-
download_button = gr.Button("Download Images as Zip")
|
| 184 |
-
|
| 185 |
-
def on_submit_click_inpaint(image, inpaint_prompt, number_of_images):
|
| 186 |
-
inpainted_images = inpaint_defect(image, inpaint_prompt, num_images=number_of_images)
|
| 187 |
-
return inpainted_images
|
| 188 |
-
|
| 189 |
-
def on_download_click(images):
|
| 190 |
-
zip_buffer = download_images_as_zip(images)
|
| 191 |
-
return zip_buffer
|
| 192 |
-
|
| 193 |
-
submit_button_inpaint.click(
|
| 194 |
-
fn=on_submit_click_inpaint,
|
| 195 |
-
inputs=[image_input, inpaint_prompt_input, number_input_inpaint],
|
| 196 |
-
outputs=inpainted_image_output
|
| 197 |
-
)
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
inputs=inpainted_image_output,
|
| 202 |
-
outputs=gr.File(label="Download Zip")
|
| 203 |
-
)
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|
| 206 |
app.launch()
|
|
|
|
| 19 |
OPENAI_API_KEY = "sk-proj-5iy4bwrqAW8GpguiEawaT3BlbkFJ8p88lLSjOCeDbxWsAOlr"
|
| 20 |
openai.api_key = OPENAI_API_KEY
|
| 21 |
|
|
|
|
| 22 |
predefined_prompts = [
|
| 23 |
"Missing bolts on railway track",
|
| 24 |
"Cracks on railway track",
|
|
|
|
| 44 |
)
|
| 45 |
return response.choices[0].message['content']
|
| 46 |
|
|
|
|
| 47 |
def generate_variations(base_prompt, number_of_variations):
|
| 48 |
locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
|
| 49 |
sizes = ["small", "medium", "large", "tiny", "huge"]
|
|
|
|
| 55 |
size = random.choice(sizes)
|
| 56 |
weather = random.choice(weather_conditions)
|
| 57 |
|
| 58 |
+
full_prompt = f"{base_prompt}, with a {size} defect {location}, observed {weather}."
|
|
|
|
|
|
|
| 59 |
variations.append(full_prompt)
|
| 60 |
return variations
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def generate_images(prompts):
|
| 63 |
images = []
|
| 64 |
for prompt in prompts:
|
|
|
|
| 76 |
images.append(f"Error: {str(e)}")
|
| 77 |
return images
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# UI creation
|
| 80 |
with gr.Blocks() as app:
|
| 81 |
with gr.Tabs("Prompt Input"):
|
| 82 |
+
with gr.Tab("Generate Images"):
|
| 83 |
+
prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a defect prompt")
|
| 84 |
+
number_input = gr.Number(label="Number of images", value=1, minimum=1, maximum=10)
|
| 85 |
+
generate_button = gr.Button("Generate")
|
| 86 |
+
gallery = gr.Gallery(label="Generated Images")
|
| 87 |
+
|
| 88 |
+
generate_button.click(
|
| 89 |
+
fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
|
| 90 |
+
inputs=[prompt_input, number_input],
|
| 91 |
+
outputs=gallery
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
with gr.Tab("Custom Defect"):
|
| 95 |
+
custom_prompt_input = gr.Textbox(label="Custom Defect")
|
| 96 |
+
number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 97 |
+
submit_button_custom = gr.Button("Generate")
|
|
|
|
| 98 |
image_outputs_custom = gr.Gallery()
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
submit_button_custom.click(
|
| 101 |
+
fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
|
| 102 |
inputs=[custom_prompt_input, number_input_custom],
|
| 103 |
outputs=image_outputs_custom
|
| 104 |
)
|
| 105 |
+
|
| 106 |
+
feedback_input = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...")
|
| 107 |
+
feedback_button = gr.Button("Submit Feedback")
|
| 108 |
+
feedback_result = gr.Textbox(label="System Response", interactive=False)
|
| 109 |
+
refresh_button = gr.Button("Refresh Page")
|
| 110 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
feedback_button.click(lambda x: ask_rail_defect_question(x), inputs=feedback_input, outputs=feedback_result)
|
| 113 |
+
refresh_button.click(lambda: gr.update(reload_browser=True))
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
app.launch()
|