Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,11 +12,13 @@ import zipfile
|
|
| 12 |
|
| 13 |
# Set API tokens
|
| 14 |
os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
|
| 15 |
-
os.environ["OPENAI_API_KEY"] = "sk-
|
|
|
|
| 16 |
|
| 17 |
# Initialize the Replicate client
|
| 18 |
rep_client = replicate.Client()
|
| 19 |
|
|
|
|
| 20 |
predefined_prompts = [
|
| 21 |
"Missing bolts on railway track",
|
| 22 |
"Cracks on railway track",
|
|
@@ -26,11 +28,21 @@ predefined_prompts = [
|
|
| 26 |
"Damaged railway platform"
|
| 27 |
]
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def generate_variations(base_prompt, number_of_variations):
|
| 36 |
locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
|
|
@@ -43,31 +55,19 @@ def generate_variations(base_prompt, number_of_variations):
|
|
| 43 |
size = random.choice(sizes)
|
| 44 |
weather = random.choice(weather_conditions)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
variations.append(
|
| 48 |
return variations
|
| 49 |
|
| 50 |
-
def
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
version="ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4",
|
| 56 |
-
input={"prompt": prompt, "scheduler": "K_EULER"}
|
| 57 |
-
)
|
| 58 |
-
prediction.wait()
|
| 59 |
-
if prediction.status == "succeeded" and prediction.output:
|
| 60 |
-
images.append(prediction.output[0])
|
| 61 |
-
else:
|
| 62 |
-
images.append("Failed to generate image.")
|
| 63 |
-
except Exception as e:
|
| 64 |
-
images.append(f"Error: {str(e)}")
|
| 65 |
-
return images
|
| 66 |
|
| 67 |
def inpaint_defect(image, prompt, num_images=1):
|
| 68 |
if isinstance(image, np.ndarray):
|
| 69 |
image = Image.fromarray(image)
|
| 70 |
-
|
| 71 |
image_data_url = image_to_data_url(image)
|
| 72 |
images = []
|
| 73 |
|
|
@@ -81,7 +81,6 @@ def inpaint_defect(image, prompt, num_images=1):
|
|
| 81 |
"num_inference_steps": 100,
|
| 82 |
"image_guidance_scale": 1.5
|
| 83 |
}
|
| 84 |
-
|
| 85 |
prediction = rep_client.predictions.create(
|
| 86 |
version="10e63b0e6361eb23a0374f4d9ee145824d9d09f7a31dcd70803193ebc7121430",
|
| 87 |
input=input
|
|
@@ -92,15 +91,38 @@ def inpaint_defect(image, prompt, num_images=1):
|
|
| 92 |
response = requests.get(image_url)
|
| 93 |
img = Image.open(io.BytesIO(response.content))
|
| 94 |
images.append(img)
|
| 95 |
-
else:
|
| 96 |
-
images.append(None)
|
| 97 |
return images
|
| 98 |
|
| 99 |
-
def
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# UI creation
|
| 106 |
with gr.Blocks() as app:
|
|
@@ -110,53 +132,78 @@ with gr.Blocks() as app:
|
|
| 110 |
prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a prompt")
|
| 111 |
number_input_dropdown = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 112 |
submit_button_dropdown = gr.Button("Generate")
|
| 113 |
-
feedback_input = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...")
|
| 114 |
-
like_button = gr.Button(value="👍 Like", visible=True)
|
| 115 |
-
dislike_button = gr.Button(value="👎 Dislike", visible=True)
|
| 116 |
image_outputs_dropdown = gr.Gallery()
|
|
|
|
| 117 |
submit_button_dropdown.click(
|
| 118 |
-
fn=
|
| 119 |
inputs=[prompt_input, number_input_dropdown],
|
| 120 |
-
outputs=
|
| 121 |
)
|
| 122 |
|
| 123 |
with gr.Tab("Custom Defect"):
|
| 124 |
with gr.Row():
|
| 125 |
custom_prompt_input = gr.Textbox(label="Custom Defect")
|
| 126 |
-
number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=
|
| 127 |
submit_button_custom = gr.Button("Generate")
|
| 128 |
-
feedback_input_custom = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...", visible=True)
|
| 129 |
-
like_button_custom = gr.Button(value="👍 Like", visible=True)
|
| 130 |
-
dislike_button_custom = gr.Button(value="👎 Dislike", visible=True)
|
| 131 |
image_outputs_custom = gr.Gallery()
|
|
|
|
| 132 |
submit_button_custom.click(
|
| 133 |
-
fn=
|
| 134 |
inputs=[custom_prompt_input, number_input_custom],
|
| 135 |
-
outputs=
|
| 136 |
)
|
| 137 |
-
|
| 138 |
with gr.Tab("Inpaint Defect"):
|
| 139 |
with gr.Row():
|
| 140 |
image_input = gr.Image(label="Upload Image")
|
| 141 |
inpaint_prompt_input = gr.Textbox(label="Defect Description")
|
| 142 |
-
number_input_inpaint = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=
|
| 143 |
submit_button_inpaint = gr.Button("Inpaint Defect")
|
| 144 |
-
feedback_input_inpaint = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...", visible=True)
|
| 145 |
-
like_button_inpaint = gr.Button(value="👍 Like", visible=True)
|
| 146 |
-
dislike_button_inpaint = gr.Button(value="👎 Dislike", visible=True)
|
| 147 |
inpainted_image_output = gr.Gallery()
|
|
|
|
|
|
|
| 148 |
submit_button_inpaint.click(
|
| 149 |
-
fn=
|
| 150 |
inputs=[image_input, inpaint_prompt_input, number_input_inpaint],
|
| 151 |
-
outputs=
|
| 152 |
)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
-
app.launch()
|
|
|
|
| 12 |
|
| 13 |
# Set API tokens
|
| 14 |
os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
|
| 15 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 16 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 17 |
|
| 18 |
# Initialize the Replicate client
|
| 19 |
rep_client = replicate.Client()
|
| 20 |
|
| 21 |
+
# Predefined prompts for the dropdown
|
| 22 |
predefined_prompts = [
|
| 23 |
"Missing bolts on railway track",
|
| 24 |
"Cracks on railway track",
|
|
|
|
| 28 |
"Damaged railway platform"
|
| 29 |
]
|
| 30 |
|
| 31 |
+
def ask_rail_defect_question(question, model_name='ft:gpt-3.5-turbo-0125:personal::99NsSAeQ'):
|
| 32 |
+
response = openai.ChatCompletion.create(
|
| 33 |
+
model=model_name,
|
| 34 |
+
messages=[
|
| 35 |
+
{
|
| 36 |
+
"role": "system",
|
| 37 |
+
"content": "The assistant is knowledgeable about rail defects and can answer questions related to them.",
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"role": "user",
|
| 41 |
+
"content": question,
|
| 42 |
+
}
|
| 43 |
+
],
|
| 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"]
|
|
|
|
| 55 |
size = random.choice(sizes)
|
| 56 |
weather = random.choice(weather_conditions)
|
| 57 |
|
| 58 |
+
enhanced_prompt = f"{base_prompt}, with a {size} defect {location}, observed {weather}."
|
| 59 |
+
variations.append(enhanced_prompt)
|
| 60 |
return variations
|
| 61 |
|
| 62 |
+
def image_to_data_url(image):
|
| 63 |
+
buffered = io.BytesIO()
|
| 64 |
+
image.save(buffered, format="PNG")
|
| 65 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 66 |
+
return f"data:image/png;base64,{img_str}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def inpaint_defect(image, prompt, num_images=1):
|
| 69 |
if isinstance(image, np.ndarray):
|
| 70 |
image = Image.fromarray(image)
|
|
|
|
| 71 |
image_data_url = image_to_data_url(image)
|
| 72 |
images = []
|
| 73 |
|
|
|
|
| 81 |
"num_inference_steps": 100,
|
| 82 |
"image_guidance_scale": 1.5
|
| 83 |
}
|
|
|
|
| 84 |
prediction = rep_client.predictions.create(
|
| 85 |
version="10e63b0e6361eb23a0374f4d9ee145824d9d09f7a31dcd70803193ebc7121430",
|
| 86 |
input=input
|
|
|
|
| 91 |
response = requests.get(image_url)
|
| 92 |
img = Image.open(io.BytesIO(response.content))
|
| 93 |
images.append(img)
|
|
|
|
|
|
|
| 94 |
return images
|
| 95 |
|
| 96 |
+
def generate_images(prompts):
|
| 97 |
+
images = []
|
| 98 |
+
for prompt in prompts:
|
| 99 |
+
try:
|
| 100 |
+
prediction = rep_client.predictions.create(
|
| 101 |
+
version="ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4",
|
| 102 |
+
input={"prompt": prompt, "scheduler": "K_EULER"}
|
| 103 |
+
)
|
| 104 |
+
prediction.wait()
|
| 105 |
+
if prediction.status == "succeeded" and prediction.output:
|
| 106 |
+
images.append(prediction.output[0])
|
| 107 |
+
except Exception as e:
|
| 108 |
+
images.append(f"Error: {str(e)}")
|
| 109 |
+
return images
|
| 110 |
+
|
| 111 |
+
def process_railway_defects(prompt, number_of_images):
|
| 112 |
+
variations = generate_variations(prompt, number_of_images)
|
| 113 |
+
images = generate_images(variations)
|
| 114 |
+
return images
|
| 115 |
+
|
| 116 |
+
def download_images_as_zip(images):
|
| 117 |
+
zip_buffer = io.BytesIO()
|
| 118 |
+
with zipfile.ZipFile(zip_buffer, 'w') as zf:
|
| 119 |
+
for i, img in enumerate(images):
|
| 120 |
+
img_buffer = io.BytesIO()
|
| 121 |
+
img.save(img_buffer, format='PNG')
|
| 122 |
+
img_buffer.seek(0)
|
| 123 |
+
zf.writestr(f'image_{i + 1}.png', img_buffer.read())
|
| 124 |
+
zip_buffer.seek(0)
|
| 125 |
+
return zip_buffer
|
| 126 |
|
| 127 |
# UI creation
|
| 128 |
with gr.Blocks() as app:
|
|
|
|
| 132 |
prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a prompt")
|
| 133 |
number_input_dropdown = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 134 |
submit_button_dropdown = gr.Button("Generate")
|
|
|
|
|
|
|
|
|
|
| 135 |
image_outputs_dropdown = gr.Gallery()
|
| 136 |
+
|
| 137 |
submit_button_dropdown.click(
|
| 138 |
+
fn=process_railway_defects,
|
| 139 |
inputs=[prompt_input, number_input_dropdown],
|
| 140 |
+
outputs=image_outputs_dropdown
|
| 141 |
)
|
| 142 |
|
| 143 |
with gr.Tab("Custom Defect"):
|
| 144 |
with gr.Row():
|
| 145 |
custom_prompt_input = gr.Textbox(label="Custom Defect")
|
| 146 |
+
number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 147 |
submit_button_custom = gr.Button("Generate")
|
|
|
|
|
|
|
|
|
|
| 148 |
image_outputs_custom = gr.Gallery()
|
| 149 |
+
|
| 150 |
submit_button_custom.click(
|
| 151 |
+
fn=process_railway_defects,
|
| 152 |
inputs=[custom_prompt_input, number_input_custom],
|
| 153 |
+
outputs=image_outputs_custom
|
| 154 |
)
|
| 155 |
+
|
| 156 |
with gr.Tab("Inpaint Defect"):
|
| 157 |
with gr.Row():
|
| 158 |
image_input = gr.Image(label="Upload Image")
|
| 159 |
inpaint_prompt_input = gr.Textbox(label="Defect Description")
|
| 160 |
+
number_input_inpaint = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 161 |
submit_button_inpaint = gr.Button("Inpaint Defect")
|
|
|
|
|
|
|
|
|
|
| 162 |
inpainted_image_output = gr.Gallery()
|
| 163 |
+
download_button = gr.Button("Download Images as Zip")
|
| 164 |
+
|
| 165 |
submit_button_inpaint.click(
|
| 166 |
+
fn=inpaint_defect,
|
| 167 |
inputs=[image_input, inpaint_prompt_input, number_input_inpaint],
|
| 168 |
+
outputs=inpainted_image_output
|
| 169 |
)
|
| 170 |
|
| 171 |
+
download_button.click(
|
| 172 |
+
fn=download_images_as_zip,
|
| 173 |
+
inputs=inpainted_image_output,
|
| 174 |
+
outputs=gr.File(label="Download Zip")
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
with gr.Tab("Feedback"):
|
| 178 |
+
feedback_input = gr.Textbox(label="Your feedback")
|
| 179 |
+
like_button = gr.Button("Like")
|
| 180 |
+
dislike_button = gr.Button("Dislike")
|
| 181 |
+
feedback_response = gr.Text(label="Feedback Response")
|
| 182 |
+
|
| 183 |
+
def handle_feedback(feedback):
|
| 184 |
+
sentiment_response = ask_rail_defect_question(feedback, model_name='ft:gpt-3.5-turbo-0125:personal::99NsSAeQ')
|
| 185 |
+
if "negative" in sentiment_response:
|
| 186 |
+
return "Sorry for your experience, and thanks for your feedback. We are updating our system to generate more satisfactory samples."
|
| 187 |
+
else:
|
| 188 |
+
return "Thanks for your positive feedback!"
|
| 189 |
+
|
| 190 |
+
feedback_input.change(
|
| 191 |
+
fn=handle_feedback,
|
| 192 |
+
inputs=feedback_input,
|
| 193 |
+
outputs=feedback_response
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
like_button.click(
|
| 197 |
+
fn=lambda: "Thanks for your positive feedback!",
|
| 198 |
+
inputs=None,
|
| 199 |
+
outputs=feedback_response
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
dislike_button.click(
|
| 203 |
+
fn=lambda: "Sorry for your experience, and thanks for your feedback. We are updating our system to generate more satisfactory samples.",
|
| 204 |
+
inputs=None,
|
| 205 |
+
outputs=feedback_response
|
| 206 |
+
)
|
| 207 |
|
| 208 |
if __name__ == "__main__":
|
| 209 |
+
app.launch()
|