Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 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"]
|
| 49 |
+
sizes = ["small", "medium", "large", "tiny", "huge"]
|
| 50 |
+
weather_conditions = ["under cold conditions", "during hot weather", "in dry weather", "in humid conditions", "under varying temperatures"]
|
| 51 |
+
|
| 52 |
+
variations = []
|
| 53 |
+
for _ in range(number_of_variations):
|
| 54 |
+
location = random.choice(locations)
|
| 55 |
+
size = random.choice(sizes)
|
| 56 |
+
weather = random.choice(weather_conditions)
|
| 57 |
+
|
| 58 |
+
full_prompt = f"{base_prompt}, with a {size} defect {location}, observed {weather}."
|
| 59 |
+
variations.append(full_prompt)
|
| 60 |
+
return variations
|
| 61 |
+
|
| 62 |
+
def generate_images(prompts):
|
| 63 |
+
images = []
|
| 64 |
+
for prompt in prompts:
|
| 65 |
+
try:
|
| 66 |
+
prediction = rep_client.predictions.create(
|
| 67 |
+
version="ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4",
|
| 68 |
+
input={"prompt": prompt, "scheduler": "K_EULER"}
|
| 69 |
+
)
|
| 70 |
+
prediction.wait()
|
| 71 |
+
if prediction.status == "succeeded" and prediction.output:
|
| 72 |
+
images.append(prediction.output[0])
|
| 73 |
+
else:
|
| 74 |
+
images.append("Failed to generate image.")
|
| 75 |
+
except Exception as e:
|
| 76 |
+
images.append(f"Error: {str(e)}")
|
| 77 |
+
return images
|
| 78 |
+
|
| 79 |
+
# UI creation
|
| 80 |
+
with gr.Blocks() as app:
|
| 81 |
+
with gr.Tabs("Prompt Input"):
|
| 82 |
+
with gr.Tab("Generate Images"):
|
| 83 |
+
prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a defect prompt")
|
| 84 |
+
number_input = gr.Number(label="Number of images", value=1, minimum=1, maximum=10)
|
| 85 |
+
generate_button = gr.Button("Generate")
|
| 86 |
+
gallery = gr.Gallery(label="Generated Images")
|
| 87 |
+
|
| 88 |
+
generate_button.click(
|
| 89 |
+
fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
|
| 90 |
+
inputs=[prompt_input, number_input],
|
| 91 |
+
outputs=gallery
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Tab("Custom Defect"):
|
| 95 |
+
custom_prompt_input = gr.Textbox(label="Custom Defect")
|
| 96 |
+
number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
|
| 97 |
+
submit_button_custom = gr.Button("Generate")
|
| 98 |
+
image_outputs_custom = gr.Gallery()
|
| 99 |
+
|
| 100 |
+
submit_button_custom.click(
|
| 101 |
+
fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
|
| 102 |
+
inputs=[custom_prompt_input, number_input_custom],
|
| 103 |
+
outputs=image_outputs_custom
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
feedback_input = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...")
|
| 107 |
+
feedback_button = gr.Button("Submit Feedback")
|
| 108 |
+
feedback_result = gr.Textbox(label="System Response", interactive=False)
|
| 109 |
+
refresh_button = gr.Button("Refresh Page")
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
feedback_button.click(lambda x: ask_rail_defect_question(x), inputs=feedback_input, outputs=feedback_result)
|
| 113 |
+
refresh_button.click(lambda: gr.update(reload_browser=True))
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
app.launch()
|