Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,12 +12,13 @@ import zipfile
|
|
| 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",
|
|
@@ -44,6 +45,7 @@ def ask_rail_defect_question(question, model_name='ft:gpt-3.5-turbo-0125:persona
|
|
| 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,8 +57,10 @@ def generate_variations(base_prompt, number_of_variations):
|
|
| 55 |
size = random.choice(sizes)
|
| 56 |
weather = random.choice(weather_conditions)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
return variations
|
| 61 |
|
| 62 |
def image_to_data_url(image):
|
|
@@ -65,9 +69,11 @@ def image_to_data_url(image):
|
|
| 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,6 +87,7 @@ def inpaint_defect(image, prompt, num_images=1):
|
|
| 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,8 +98,11 @@ def inpaint_defect(image, prompt, num_images=1):
|
|
| 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:
|
|
@@ -104,6 +114,8 @@ def generate_images(prompts):
|
|
| 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
|
|
@@ -134,8 +146,12 @@ with gr.Blocks() as app:
|
|
| 134 |
submit_button_dropdown = gr.Button("Generate")
|
| 135 |
image_outputs_dropdown = gr.Gallery()
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
submit_button_dropdown.click(
|
| 138 |
-
fn=
|
| 139 |
inputs=[prompt_input, number_input_dropdown],
|
| 140 |
outputs=image_outputs_dropdown
|
| 141 |
)
|
|
@@ -147,8 +163,12 @@ with gr.Blocks() as app:
|
|
| 147 |
submit_button_custom = gr.Button("Generate")
|
| 148 |
image_outputs_custom = gr.Gallery()
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
submit_button_custom.click(
|
| 151 |
-
fn=
|
| 152 |
inputs=[custom_prompt_input, number_input_custom],
|
| 153 |
outputs=image_outputs_custom
|
| 154 |
)
|
|
@@ -162,48 +182,25 @@ with gr.Blocks() as app:
|
|
| 162 |
inpainted_image_output = gr.Gallery()
|
| 163 |
download_button = gr.Button("Download Images as Zip")
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
submit_button_inpaint.click(
|
| 166 |
-
fn=
|
| 167 |
inputs=[image_input, inpaint_prompt_input, number_input_inpaint],
|
| 168 |
outputs=inpainted_image_output
|
| 169 |
)
|
| 170 |
|
| 171 |
download_button.click(
|
| 172 |
-
fn=
|
| 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()
|
|
|
|
| 12 |
|
| 13 |
# Set API tokens
|
| 14 |
os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
|
|
|
|
|
|
|
|
|
|
| 15 |
# Initialize the Replicate client
|
| 16 |
rep_client = replicate.Client()
|
| 17 |
|
| 18 |
+
# Set your OpenAI API key
|
| 19 |
+
OPENAI_API_KEY = "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"
|
| 20 |
+
openai.api_key = OPENAI_API_KEY
|
| 21 |
+
|
| 22 |
# Predefined prompts for the dropdown
|
| 23 |
predefined_prompts = [
|
| 24 |
"Missing bolts on railway track",
|
|
|
|
| 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 |
size = random.choice(sizes)
|
| 58 |
weather = random.choice(weather_conditions)
|
| 59 |
|
| 60 |
+
# Enhance the base prompt with the GPT model
|
| 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):
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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:
|
|
|
|
| 114 |
prediction.wait()
|
| 115 |
if prediction.status == "succeeded" and prediction.output:
|
| 116 |
images.append(prediction.output[0])
|
| 117 |
+
else:
|
| 118 |
+
images.append("Failed to generate image.")
|
| 119 |
except Exception as e:
|
| 120 |
images.append(f"Error: {str(e)}")
|
| 121 |
return images
|
|
|
|
| 146 |
submit_button_dropdown = gr.Button("Generate")
|
| 147 |
image_outputs_dropdown = gr.Gallery()
|
| 148 |
|
| 149 |
+
def on_submit_click_dropdown(prompt, number_of_images):
|
| 150 |
+
images = process_railway_defects(prompt, number_of_images)
|
| 151 |
+
return images
|
| 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 |
)
|
|
|
|
| 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=on_submit_click_custom,
|
| 172 |
inputs=[custom_prompt_input, number_input_custom],
|
| 173 |
outputs=image_outputs_custom
|
| 174 |
)
|
|
|
|
| 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 |
download_button.click(
|
| 200 |
+
fn=on_download_click,
|
| 201 |
inputs=inpainted_image_output,
|
| 202 |
outputs=gr.File(label="Download Zip")
|
| 203 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|
| 206 |
app.launch()
|