Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import replicate
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import openai
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import requests
|
| 9 |
+
import io
|
| 10 |
+
import base64
|
| 11 |
+
import zipfile
|
| 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 = "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",
|
| 25 |
+
"Overgrown vegetation near railway track",
|
| 26 |
+
"Broken railings on railway bridge",
|
| 27 |
+
"Debris 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"]
|
| 40 |
+
weather_conditions = ["under cold conditions", "during hot weather", "in dry weather", "in humid conditions", "under varying temperatures"]
|
| 41 |
+
|
| 42 |
+
variations = []
|
| 43 |
+
for _ in range(number_of_variations):
|
| 44 |
+
location = random.choice(locations)
|
| 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:
|
| 55 |
+
try:
|
| 56 |
+
prediction = rep_client.predictions.create(
|
| 57 |
+
version="ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4",
|
| 58 |
+
input={"prompt": prompt, "scheduler": "K_EULER"}
|
| 59 |
+
)
|
| 60 |
+
prediction.wait()
|
| 61 |
+
if prediction.status == "succeeded" and prediction.output:
|
| 62 |
+
images.append(prediction.output[0])
|
| 63 |
+
else:
|
| 64 |
+
images.append("Failed to generate image.")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
images.append(f"Error: {str(e)}")
|
| 67 |
+
return images
|
| 68 |
+
|
| 69 |
+
def inpaint_defect(image, prompt, num_images=1):
|
| 70 |
+
if isinstance(image, np.ndarray):
|
| 71 |
+
image = Image.fromarray(image)
|
| 72 |
+
|
| 73 |
+
image_data_url = image_to_data_url(image)
|
| 74 |
+
images = []
|
| 75 |
+
|
| 76 |
+
for _ in range(num_images):
|
| 77 |
+
input = {
|
| 78 |
+
"input_image": image_data_url,
|
| 79 |
+
"instruction_text": prompt,
|
| 80 |
+
"scheduler": "K_EULER_ANCESTRAL",
|
| 81 |
+
"num_outputs": 1,
|
| 82 |
+
"guidance_scale": 7.5,
|
| 83 |
+
"num_inference_steps": 100,
|
| 84 |
+
"image_guidance_scale": 1.5
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
prediction = rep_client.predictions.create(
|
| 88 |
+
version="10e63b0e6361eb23a0374f4d9ee145824d9d09f7a31dcd70803193ebc7121430",
|
| 89 |
+
input=input
|
| 90 |
+
)
|
| 91 |
+
prediction.wait()
|
| 92 |
+
if prediction.status == "succeeded":
|
| 93 |
+
image_url = prediction.output[0]
|
| 94 |
+
response = requests.get(image_url)
|
| 95 |
+
img = Image.open(io.BytesIO(response.content))
|
| 96 |
+
images.append(img)
|
| 97 |
+
else:
|
| 98 |
+
images.append(None)
|
| 99 |
+
return images
|
| 100 |
+
|
| 101 |
+
def image_to_data_url(image):
|
| 102 |
+
buffered = io.BytesIO()
|
| 103 |
+
image.save(buffered, format="PNG")
|
| 104 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 105 |
+
return f"data:image/png;base64,{img_str}"
|
| 106 |
+
|
| 107 |
+
# UI creation
|
| 108 |
+
with gr.Blocks() as app:
|
| 109 |
+
feedback_input = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...")
|
| 110 |
+
like_button = gr.Button(value="👍 Like")
|
| 111 |
+
dislike_button = gr.Button(value="👎 Dislike")
|
| 112 |
+
feedback_result = gr.Textbox(label="System Response", interactive=False)
|
| 113 |
+
refresh_button = gr.Button("Refresh Page")
|
| 114 |
+
|
| 115 |
+
with gr.Tabs("Prompt Input"):
|
| 116 |
+
with gr.Tab("Generate Images"):
|
| 117 |
+
prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a defect prompt")
|
| 118 |
+
number_input = gr.Number(label="Number of images", value=1, minimum=1, maximum=10)
|
| 119 |
+
generate_button = gr.Button("Generate")
|
| 120 |
+
gallery = gr.Gallery(label="Generated Images")
|
| 121 |
+
|
| 122 |
+
generate_button.click(
|
| 123 |
+
fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
|
| 124 |
+
inputs=[prompt_input, number_input],
|
| 125 |
+
outputs=gallery
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
with gr.Tab("Inpaint Defect"):
|
| 129 |
+
image_input = gr.Image(label="Upload Image")
|
| 130 |
+
inpaint_prompt_input = gr.Textbox(label="Describe the defect")
|
| 131 |
+
number_input_inpaint = gr.Number(label="Number of images", value=1, minimum=1, maximum=10)
|
| 132 |
+
inpaint_button = gr.Button("Inpaint Defect")
|
| 133 |
+
gallery_inpaint = gr.Gallery(label="Inpainted Images")
|
| 134 |
+
|
| 135 |
+
inpaint_button.click(
|
| 136 |
+
fn=lambda img, prompt, num: inpaint_defect(img, prompt, num),
|
| 137 |
+
inputs=[image_input, inpaint_prompt_input, number_input_inpaint],
|
| 138 |
+
outputs=gallery_inpaint
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
like_button.click(lambda x: handle_feedback(x, "like"), inputs=feedback_input, outputs=feedback_result)
|
| 142 |
+
dislike_button.click(lambda x: handle_feedback(x, "dislike"), inputs=feedback_input, outputs=feedback_result)
|
| 143 |
+
refresh_button.click(lambda: gr.update(reload_browser=True))
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
app.launch()
|