Rahatara commited on
Commit
f813718
1 Parent(s): b15a843

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_8TlgofGX8rjeBL28vn0VBR93CWOUfvg4NbLS0"
10
+ # Initialize the Replicate client
11
+ rep_client = replicate.Client()
12
+
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 = [
21
+ "Missing bolts on railway track",
22
+ "Cracks on railway track",
23
+ "Overgrown vegetation near railway track",
24
+ "Broken railings on railway bridge",
25
+ "Debris on railway track",
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=[
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
+ # Function to generate variations enhanced by the GPT model
48
+ def generate_variations(base_prompt, number_of_variations):
49
+ locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
50
+ sizes = ["small", "medium", "large", "tiny", "huge"]
51
+ weather_conditions = ["under cold conditions", "during hot weather", "in dry weather", "in humid conditions", "under varying temperatures"]
52
+
53
+ variations = []
54
+ for _ in range(number_of_variations):
55
+ location = random.choice(locations)
56
+ size = random.choice(sizes)
57
+ weather = random.choice(weather_conditions)
58
+
59
+ # Enhance the base prompt with the GPT model
60
+ enhanced_prompt = ask_rail_defect_question(base_prompt)
61
+
62
+ full_prompt = f"{enhanced_prompt}, with a {size} defect {location}, observed {weather}."
63
+ variations.append(full_prompt)
64
+ return variations
65
+
66
+ # Function to generate images from prompts
67
+ def generate_images(prompts):
68
+ images = []
69
+ for prompt in prompts:
70
+ try:
71
+ prediction = rep_client.predictions.create(
72
+ version="ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4",
73
+ input={"prompt": prompt, "scheduler": "K_EULER"}
74
+ )
75
+ prediction.wait()
76
+ if prediction.status == "succeeded" and prediction.output:
77
+ images.append(prediction.output[0])
78
+ else:
79
+ images.append("Failed to generate image.")
80
+ except Exception as e:
81
+ images.append(f"Error: {str(e)}")
82
+ return images
83
+
84
+ 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:
93
+ with gr.Tabs("Prompt Input"):
94
+ with gr.Tab("Current Defects"):
95
+ with gr.Row():
96
+ prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a prompt")
97
+ number_input_dropdown = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
98
+ submit_button_dropdown = gr.Button("Generate")
99
+ image_outputs_dropdown = gr.Gallery()
100
+
101
+ def on_submit_click_dropdown(prompt, number_of_images):
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],
110
+ outputs=image_outputs_dropdown
111
+ )
112
+
113
+ with gr.Tab("Custom Defect"):
114
+ with gr.Row():
115
+ custom_prompt_input = gr.Textbox(label="Custom Defect")
116
+ number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
117
+ submit_button_custom = gr.Button("Generate")
118
+ image_outputs_custom = gr.Gallery()
119
+
120
+ def on_submit_click_custom(custom_prompt, number_of_images):
121
+ images = process_railway_defects(custom_prompt, number_of_images)
122
+ return images
123
+
124
+ submit_button_custom.click(
125
+ fn=on_submit_click_custom,
126
+ inputs=[custom_prompt_input, number_input_custom],
127
+ outputs=image_outputs_custom
128
+ )
129
+
130
+ if __name__ == "__main__":
131
+ app.launch()