Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import replicate
|
| 3 |
import os
|
| 4 |
-
from huggingface_hub import InferenceClient
|
| 5 |
import random
|
| 6 |
import openai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Set API tokens
|
| 9 |
os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
|
|
@@ -13,8 +17,6 @@ rep_client = replicate.Client()
|
|
| 13 |
# Set your OpenAI API key
|
| 14 |
OPENAI_API_KEY = "sk-proj-5iy4bwrqAW8GpguiEawaT3BlbkFJ8p88lLSjOCeDbxWsAOlr"
|
| 15 |
openai.api_key = OPENAI_API_KEY
|
| 16 |
-
# Initialize the Replicate client
|
| 17 |
-
rep_client = replicate.Client()
|
| 18 |
|
| 19 |
# Predefined prompts for the dropdown
|
| 20 |
predefined_prompts = [
|
|
@@ -26,9 +28,7 @@ predefined_prompts = [
|
|
| 26 |
"Damaged railway platform"
|
| 27 |
]
|
| 28 |
|
| 29 |
-
|
| 30 |
def ask_rail_defect_question(question, model_name='ft:gpt-3.5-turbo-0125:personal::99NsSAeQ'):
|
| 31 |
-
openai.api_key = OPENAI_API_KEY
|
| 32 |
response = openai.ChatCompletion.create(
|
| 33 |
model=model_name,
|
| 34 |
messages=[
|
|
@@ -63,6 +63,41 @@ def generate_variations(base_prompt, number_of_variations):
|
|
| 63 |
variations.append(full_prompt)
|
| 64 |
return variations
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Function to generate images from prompts
|
| 67 |
def generate_images(prompts):
|
| 68 |
images = []
|
|
@@ -85,8 +120,6 @@ def process_railway_defects(prompt, number_of_images):
|
|
| 85 |
variations = generate_variations(prompt, number_of_images)
|
| 86 |
images = generate_images(variations)
|
| 87 |
return images
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
# UI creation
|
| 92 |
with gr.Blocks() as app:
|
|
@@ -102,8 +135,6 @@ with gr.Blocks() as app:
|
|
| 102 |
images = process_railway_defects(prompt, number_of_images)
|
| 103 |
return images
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
submit_button_dropdown.click(
|
| 108 |
fn=on_submit_click_dropdown,
|
| 109 |
inputs=[prompt_input, number_input_dropdown],
|
|
@@ -118,8 +149,8 @@ with gr.Blocks() as app:
|
|
| 118 |
image_outputs_custom = gr.Gallery()
|
| 119 |
|
| 120 |
def on_submit_click_custom(custom_prompt, number_of_images):
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
submit_button_custom.click(
|
| 125 |
fn=on_submit_click_custom,
|
|
@@ -127,5 +158,22 @@ with gr.Blocks() as app:
|
|
| 127 |
outputs=image_outputs_custom
|
| 128 |
)
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if __name__ == "__main__":
|
| 131 |
-
app.launch()
|
|
|
|
| 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 |
|
| 12 |
# Set API tokens
|
| 13 |
os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
|
|
|
|
| 17 |
# Set your OpenAI API key
|
| 18 |
OPENAI_API_KEY = "sk-proj-5iy4bwrqAW8GpguiEawaT3BlbkFJ8p88lLSjOCeDbxWsAOlr"
|
| 19 |
openai.api_key = OPENAI_API_KEY
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Predefined prompts for the dropdown
|
| 22 |
predefined_prompts = [
|
|
|
|
| 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=[
|
|
|
|
| 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):
|
| 74 |
+
if isinstance(image, np.ndarray):
|
| 75 |
+
image = Image.fromarray(image)
|
| 76 |
+
|
| 77 |
+
image_data_url = image_to_data_url(image)
|
| 78 |
+
|
| 79 |
+
input = {
|
| 80 |
+
"image": image_data_url,
|
| 81 |
+
"prompt": prompt,
|
| 82 |
+
"scheduler": "K_EULER_ANCESTRAL",
|
| 83 |
+
"num_outputs": 1,
|
| 84 |
+
"guidance_scale": 7.5,
|
| 85 |
+
"num_inference_steps": 100,
|
| 86 |
+
"image_guidance_scale": 1.5
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
prediction = rep_client.predictions.create(
|
| 90 |
+
version="30c1d0b916a6f8efce20493f5d61ee27491ab2a60437c13c588468b9810ec23f",
|
| 91 |
+
input = input
|
| 92 |
+
)
|
| 93 |
+
prediction.wait()
|
| 94 |
+
if prediction.status == "succeeded":
|
| 95 |
+
image_url = prediction.output[0]
|
| 96 |
+
response = requests.get(image_url)
|
| 97 |
+
image = Image.open(io.BytesIO(response.content))
|
| 98 |
+
return image
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
# Function to generate images from prompts
|
| 102 |
def generate_images(prompts):
|
| 103 |
images = []
|
|
|
|
| 120 |
variations = generate_variations(prompt, number_of_images)
|
| 121 |
images = generate_images(variations)
|
| 122 |
return images
|
|
|
|
|
|
|
| 123 |
|
| 124 |
# UI creation
|
| 125 |
with gr.Blocks() as app:
|
|
|
|
| 135 |
images = process_railway_defects(prompt, number_of_images)
|
| 136 |
return images
|
| 137 |
|
|
|
|
|
|
|
| 138 |
submit_button_dropdown.click(
|
| 139 |
fn=on_submit_click_dropdown,
|
| 140 |
inputs=[prompt_input, number_input_dropdown],
|
|
|
|
| 149 |
image_outputs_custom = gr.Gallery()
|
| 150 |
|
| 151 |
def on_submit_click_custom(custom_prompt, number_of_images):
|
| 152 |
+
images = process_railway_defects(custom_prompt, number_of_images)
|
| 153 |
+
return images
|
| 154 |
|
| 155 |
submit_button_custom.click(
|
| 156 |
fn=on_submit_click_custom,
|
|
|
|
| 158 |
outputs=image_outputs_custom
|
| 159 |
)
|
| 160 |
|
| 161 |
+
with gr.Tab("Inpaint Defect"):
|
| 162 |
+
with gr.Row():
|
| 163 |
+
image_input = gr.Image(label="Upload Image")
|
| 164 |
+
inpaint_prompt_input = gr.Textbox(label="Defect Description")
|
| 165 |
+
submit_button_inpaint = gr.Button("Inpaint Defect")
|
| 166 |
+
inpainted_image_output = gr.Image()
|
| 167 |
+
|
| 168 |
+
def on_submit_click_inpaint(image, inpaint_prompt):
|
| 169 |
+
inpainted_image = inpaint_defect(image, inpaint_prompt)
|
| 170 |
+
return inpainted_image
|
| 171 |
+
|
| 172 |
+
submit_button_inpaint.click(
|
| 173 |
+
fn=on_submit_click_inpaint,
|
| 174 |
+
inputs=[image_input, inpaint_prompt_input],
|
| 175 |
+
outputs=inpainted_image_output
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
if __name__ == "__main__":
|
| 179 |
+
app.launch()
|