Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,10 +16,9 @@ os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
|
|
| 16 |
rep_client = replicate.Client()
|
| 17 |
|
| 18 |
# Set your OpenAI API key
|
| 19 |
-
OPENAI_API_KEY = "sk-
|
| 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",
|
|
@@ -29,23 +28,12 @@ predefined_prompts = [
|
|
| 29 |
"Damaged railway platform"
|
| 30 |
]
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"role": "system",
|
| 38 |
-
"content": "The assistant is knowledgeable about rail defects and can answer questions related to them.",
|
| 39 |
-
},
|
| 40 |
-
{
|
| 41 |
-
"role": "user",
|
| 42 |
-
"content": question,
|
| 43 |
-
}
|
| 44 |
-
],
|
| 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,61 +45,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_data = {
|
| 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_data
|
| 94 |
-
)
|
| 95 |
-
prediction.wait()
|
| 96 |
-
|
| 97 |
-
if prediction.status == "succeeded":
|
| 98 |
-
image_url = prediction.output[0]
|
| 99 |
-
print(f"Generated Image URL: {image_url}") # Debugging line to check the URL
|
| 100 |
-
try:
|
| 101 |
-
response = requests.get(image_url)
|
| 102 |
-
response.raise_for_status() # Check for HTTP errors
|
| 103 |
-
img = Image.open(io.BytesIO(response.content))
|
| 104 |
-
images.append(img)
|
| 105 |
-
except requests.exceptions.RequestException as e:
|
| 106 |
-
print(f"Error fetching image from URL: {e}")
|
| 107 |
-
images.append(None)
|
| 108 |
-
else:
|
| 109 |
-
print(f"Prediction failed: {prediction.status}")
|
| 110 |
-
images.append(None)
|
| 111 |
-
|
| 112 |
-
return images
|
| 113 |
-
|
| 114 |
-
# Function to generate images from prompts
|
| 115 |
def generate_images(prompts):
|
| 116 |
images = []
|
| 117 |
for prompt in prompts:
|
|
@@ -129,87 +66,42 @@ def generate_images(prompts):
|
|
| 129 |
images.append(f"Error: {str(e)}")
|
| 130 |
return images
|
| 131 |
|
| 132 |
-
def process_railway_defects(prompt, number_of_images):
|
| 133 |
-
variations = generate_variations(prompt, number_of_images)
|
| 134 |
-
images = generate_images(variations)
|
| 135 |
-
return images
|
| 136 |
-
|
| 137 |
-
def download_images_as_zip(images):
|
| 138 |
-
zip_buffer = io.BytesIO()
|
| 139 |
-
with zipfile.ZipFile(zip_buffer, 'w') as zf:
|
| 140 |
-
for i, img in enumerate(images):
|
| 141 |
-
if img is not None:
|
| 142 |
-
img_buffer = io.BytesIO()
|
| 143 |
-
img.save(img_buffer, format='PNG')
|
| 144 |
-
img_buffer.seek(0)
|
| 145 |
-
zf.writestr(f'image_{i + 1}.png', img_buffer.read())
|
| 146 |
-
zip_buffer.seek(0)
|
| 147 |
-
return zip_buffer
|
| 148 |
-
|
| 149 |
# UI creation
|
| 150 |
with gr.Blocks() as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
with gr.Tabs("Prompt Input"):
|
| 152 |
-
with gr.Tab("
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
submit_button_dropdown.click(
|
| 164 |
-
fn=on_submit_click_dropdown,
|
| 165 |
-
inputs=[prompt_input, number_input_dropdown],
|
| 166 |
-
outputs=image_outputs_dropdown
|
| 167 |
)
|
| 168 |
|
| 169 |
with gr.Tab("Custom Defect"):
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
submit_button_custom = gr.Button("Generate")
|
| 174 |
image_outputs_custom = gr.Gallery()
|
| 175 |
|
| 176 |
-
def on_submit_click_custom(custom_prompt, number_of_images):
|
| 177 |
-
images = process_railway_defects(custom_prompt, number_of_images)
|
| 178 |
-
return images
|
| 179 |
-
|
| 180 |
submit_button_custom.click(
|
| 181 |
-
fn=
|
| 182 |
inputs=[custom_prompt_input, number_input_custom],
|
| 183 |
outputs=image_outputs_custom
|
| 184 |
)
|
| 185 |
-
|
| 186 |
-
with gr.Tab("Inpaint Defect"):
|
| 187 |
-
with gr.Row():
|
| 188 |
-
image_input = gr.Image(label="Upload Image")
|
| 189 |
-
inpaint_prompt_input = gr.Textbox(label="Defect Description")
|
| 190 |
-
number_input_inpaint = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 191 |
-
submit_button_inpaint = gr.Button("Inpaint Defect")
|
| 192 |
-
inpainted_image_output = gr.Gallery()
|
| 193 |
-
download_button = gr.Button("Download Images as Zip")
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
def on_download_click(images):
|
| 200 |
-
zip_buffer = download_images_as_zip(images)
|
| 201 |
-
return zip_buffer
|
| 202 |
-
|
| 203 |
-
submit_button_inpaint.click(
|
| 204 |
-
fn=on_submit_click_inpaint,
|
| 205 |
-
inputs=[image_input, inpaint_prompt_input, number_input_inpaint],
|
| 206 |
-
outputs=inpainted_image_output
|
| 207 |
-
)
|
| 208 |
-
|
| 209 |
-
download_button.click(
|
| 210 |
-
fn=on_download_click,
|
| 211 |
-
inputs=inpainted_image_output,
|
| 212 |
-
outputs=gr.File(label="Download Zip")
|
| 213 |
-
)
|
| 214 |
|
| 215 |
-
|
|
|
|
|
|
| 16 |
rep_client = replicate.Client()
|
| 17 |
|
| 18 |
# Set your OpenAI API key
|
| 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",
|
|
|
|
| 28 |
"Damaged railway platform"
|
| 29 |
]
|
| 30 |
|
| 31 |
+
def handle_feedback(feedback, sentiment):
|
| 32 |
+
if sentiment == "like":
|
| 33 |
+
return "Thank you for your positive feedback!"
|
| 34 |
+
else:
|
| 35 |
+
return "Sorry to hear that. We are trying to improve based on your feedback."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
def generate_variations(base_prompt, number_of_variations):
|
| 38 |
locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
|
| 39 |
sizes = ["small", "medium", "large", "tiny", "huge"]
|
|
|
|
| 45 |
size = random.choice(sizes)
|
| 46 |
weather = random.choice(weather_conditions)
|
| 47 |
|
| 48 |
+
full_prompt = f"{base_prompt}, with a {size} defect {location}, observed {weather}."
|
|
|
|
|
|
|
| 49 |
variations.append(full_prompt)
|
| 50 |
return variations
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def generate_images(prompts):
|
| 53 |
images = []
|
| 54 |
for prompt in prompts:
|
|
|
|
| 66 |
images.append(f"Error: {str(e)}")
|
| 67 |
return images
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# UI creation
|
| 70 |
with gr.Blocks() as app:
|
| 71 |
+
feedback_input = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...")
|
| 72 |
+
like_button = gr.Button(value="👍 Like")
|
| 73 |
+
dislike_button = gr.Button(value="👎 Dislike")
|
| 74 |
+
feedback_result = gr.Textbox(label="System Response", interactive=False)
|
| 75 |
+
refresh_button = gr.Button("Refresh Page")
|
| 76 |
+
|
| 77 |
with gr.Tabs("Prompt Input"):
|
| 78 |
+
with gr.Tab("Generate Images"):
|
| 79 |
+
prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a defect prompt")
|
| 80 |
+
number_input = gr.Number(label="Number of images", value=1, minimum=1, maximum=10)
|
| 81 |
+
generate_button = gr.Button("Generate")
|
| 82 |
+
gallery = gr.Gallery(label="Generated Images")
|
| 83 |
+
|
| 84 |
+
generate_button.click(
|
| 85 |
+
fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
|
| 86 |
+
inputs=[prompt_input, number_input],
|
| 87 |
+
outputs=gallery
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
with gr.Tab("Custom Defect"):
|
| 91 |
+
custom_prompt_input = gr.Textbox(label="Custom Defect")
|
| 92 |
+
number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 93 |
+
submit_button_custom = gr.Button("Generate")
|
|
|
|
| 94 |
image_outputs_custom = gr.Gallery()
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
submit_button_custom.click(
|
| 97 |
+
fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
|
| 98 |
inputs=[custom_prompt_input, number_input_custom],
|
| 99 |
outputs=image_outputs_custom
|
| 100 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
like_button.click(lambda x: handle_feedback(x, "like"), inputs=feedback_input, outputs=feedback_result)
|
| 103 |
+
dislike_button.click(lambda x: handle_feedback(x, "dislike"), inputs=feedback_input, outputs=feedback_result)
|
| 104 |
+
refresh_button.click(lambda: gr.update(reload_browser=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
app.launch()
|