Rahatara commited on
Commit
4c90f15
·
verified ·
1 Parent(s): f5778b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from transformers import pipeline
13
+
14
+ # Set API tokens
15
+ os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
16
+ # Initialize the Replicate client
17
+ rep_client = replicate.Client()
18
+
19
+ # Set your OpenAI API key
20
+ OPENAI_API_KEY = "sk-proj-5iy4bwrqAW8GpguiEawaT3BlbkFJ8p88lLSjOCeDbxWsAOlr"
21
+ openai.api_key = OPENAI_API_KEY
22
+
23
+ # Load sentiment analysis model
24
+ sentiment_analysis = pipeline('sentiment-analysis')
25
+
26
+ predefined_prompts = [
27
+ "Missing bolts on railway track",
28
+ "Cracks on railway track",
29
+ "Overgrown vegetation near railway track",
30
+ "Broken railings on railway bridge",
31
+ "Debris on railway track",
32
+ "Damaged railway platform"
33
+ ]
34
+
35
+ def analyze_feedback(feedback):
36
+ result = sentiment_analysis(feedback)[0]
37
+ sentiment = result['label']
38
+ score = result['score']
39
+ if sentiment == "POSITIVE":
40
+ return f"Thank you for your positive feedback! Your satisfaction score is {score}."
41
+ else:
42
+ return f"Sorry to hear that. We are trying to improve based on your feedback. Your dissatisfaction score is {score}."
43
+
44
+ def generate_variations(base_prompt, number_of_variations):
45
+ locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
46
+ sizes = ["small", "medium", "large", "tiny", "huge"]
47
+ weather_conditions = ["under cold conditions", "during hot weather", "in dry weather", "in humid conditions", "under varying temperatures"]
48
+
49
+ variations = []
50
+ for _ in range(number_of_variations):
51
+ location = random.choice(locations)
52
+ size = random.choice(sizes)
53
+ weather = random.choice(weather_conditions)
54
+
55
+ full_prompt = f"{base_prompt}, with a {size} defect {location}, observed {weather}."
56
+ variations.append(full_prompt)
57
+ return variations
58
+
59
+ def generate_images(prompts):
60
+ images = []
61
+ for prompt in prompts:
62
+ try:
63
+ prediction = rep_client.predictions.create(
64
+ version="ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4",
65
+ input={"prompt": prompt, "scheduler": "K_EULER"}
66
+ )
67
+ prediction.wait()
68
+ if prediction.status == "succeeded" and prediction.output:
69
+ images.append(prediction.output[0])
70
+ else:
71
+ images.append("Failed to generate image.")
72
+ except Exception as e:
73
+ images.append(f"Error: {str(e)}")
74
+ return images
75
+
76
+ # UI creation
77
+ with gr.Blocks() as app:
78
+ with gr.Tabs("Prompt Input"):
79
+ with gr.Tab("Generate Images"):
80
+ prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a defect prompt")
81
+ number_input = gr.Number(label="Number of images", value=1, minimum=1, maximum=10)
82
+ generate_button = gr.Button("Generate")
83
+ gallery = gr.Gallery(label="Generated Images")
84
+
85
+ generate_button.click(
86
+ fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
87
+ inputs=[prompt_input, number_input],
88
+ outputs=gallery
89
+ )
90
+
91
+ with gr.Tab("Custom Defect"):
92
+ custom_prompt_input = gr.Textbox(label="Custom Defect")
93
+ number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10)
94
+ submit_button_custom = gr.Button("Generate")
95
+ image_outputs_custom = gr.Gallery()
96
+
97
+ submit_button_custom.click(
98
+ fn=lambda prompt, num: generate_images(generate_variations(prompt, num)),
99
+ inputs=[custom_prompt_input, number_input_custom],
100
+ outputs=image_outputs_custom
101
+ )
102
+
103
+ feedback_input = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...")
104
+ like_button = gr.Button(value="👍 Like")
105
+ dislike_button = gr.Button(value="👎 Dislike")
106
+ feedback_result = gr.Textbox(label="System Response", interactive=False)
107
+ refresh_button = gr.Button("Refresh Page")
108
+
109
+
110
+ like_button.click(lambda x: analyze_feedback(x), inputs=feedback_input, outputs=feedback_result)
111
+ dislike_button.click(lambda x: analyze_feedback(x), inputs=feedback_input, outputs=feedback_result)
112
+ refresh_button.click(lambda: gr.update(reload_browser=True))
113
+
114
+ if __name__ == "__main__":
115
+ app.launch()