Chamin09 commited on
Commit
5761d1b
·
verified ·
1 Parent(s): 54bd053

Create app_bk.py

Browse files
Files changed (1) hide show
  1. interface/app_bk.py +185 -0
interface/app_bk.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # interface/app.py
2
+ import gradio as gr
3
+ from typing import List, Dict
4
+ from .handlers import process_inputs
5
+ from .display import format_results
6
+ from .utils import validate_inputs
7
+ from config.settings import config
8
+ from .handlers import ProcessingHandler
9
+ from .utils import InputValidator
10
+
11
+ def create_interface():
12
+ """Create the Gradio interface"""
13
+ handler = ProcessingHandler()
14
+
15
+ # Create interface components
16
+ with gr.Blocks(title="Image Inspection analysis") as app:
17
+ gr.Markdown("# Image Inspection analysis System")
18
+
19
+ with gr.Row():
20
+ # Input components
21
+ with gr.Column():
22
+ query = gr.Textbox(
23
+ label="What would you like to analyze?",
24
+ placeholder="e.g., Check safety issues in pump systems",
25
+ lines=3
26
+ )
27
+
28
+ constraints = gr.Textbox(
29
+ label="Any specific constraints? (optional)",
30
+ placeholder="e.g., Exclude routine maintenance issues",
31
+ lines=2
32
+ )
33
+
34
+ top_k = gr.Slider(
35
+ minimum=1,
36
+ maximum=10,
37
+ value=5,
38
+ step=1,
39
+ label="Number of top results to show"
40
+ )
41
+
42
+ report_format = gr.Radio(
43
+ choices=["summary", "detailed"],
44
+ value="summary",
45
+ label="Report Format"
46
+ )
47
+
48
+ images = gr.File(
49
+ file_count="multiple",
50
+ label="Upload Images",
51
+ file_types=["image"]
52
+ )
53
+
54
+ submit_btn = gr.Button("Analyze", variant="primary")
55
+
56
+ # Right column - Outputs
57
+ with gr.Column():
58
+ with gr.Tab("Results"):
59
+ analysis_status = gr.Markdown("Ready for analysis...")
60
+
61
+ results_box = gr.Markdown(
62
+ visible=False,
63
+ label="Analysis Results"
64
+ )
65
+
66
+ selected_images = gr.Gallery(
67
+ label="Selected Relevant Images",
68
+ visible=False,
69
+ columns=2,
70
+ height=400
71
+ )
72
+
73
+ confidence_scores = gr.Json(
74
+ label="Confidence Scores",
75
+ visible=False
76
+ )
77
+
78
+ with gr.Tab("Processing Details"):
79
+ processing_status = gr.JSON(
80
+ label="Processing Steps",
81
+ visible=False
82
+ )
83
+
84
+ with gr.Tab("Errors"):
85
+ error_box = gr.Markdown(
86
+ visible=False
87
+ )
88
+
89
+ # Second: Helper functions for UI updates
90
+ def update_ui_on_error(error_msg):
91
+ return {
92
+ results_box: gr.update(visible=True, value=error_msg),
93
+ selected_images: gr.update(visible=False),
94
+ confidence_scores: gr.update(visible=False),
95
+ processing_status: gr.update(visible=True, value={'status': 'error'})
96
+ }
97
+
98
+ def update_ui_on_success(results):
99
+ return {
100
+ results_box: gr.update(visible=True, value=results['content']),
101
+ selected_images: gr.update(visible=True, value=results['images']),
102
+ confidence_scores: gr.update(visible=True, value=results['scores']),
103
+ processing_status: gr.update(visible=True, value={'status': 'success'})
104
+ }
105
+
106
+ def validate_and_process(query, constraints, images, top_k, report_format):
107
+ # Validate inputs
108
+ is_valid, error_message = InputValidator.validate_inputs(
109
+ query, constraints, images
110
+ )
111
+
112
+ if not is_valid:
113
+ return update_ui_on_error(error_message)
114
+
115
+ # If valid, proceed with processing
116
+ return handler.process_inputs(
117
+ query, constraints, images, top_k, report_format
118
+ )
119
+
120
+ # Single submit button with combined functionality
121
+ #submit_btn = gr.Button("Analyze", variant="primary")
122
+
123
+ # # Connect submit button to both clear and process
124
+ # submit_btn.click(
125
+ # fn=lambda: [
126
+ # gr.Markdown.update(visible=True, value="Validating inputs......"),
127
+ # gr.Markdown.update(visible=False),
128
+ # gr.Gallery.update(visible=False),
129
+ # gr.Json.update(visible=False),
130
+ # gr.JSON.update(visible=False)
131
+ # ],
132
+ # inputs=None,
133
+ # outputs=[analysis_status, results_box, selected_images,
134
+ # confidence_scores, processing_status]
135
+ # ).then( # Chain the processing after clearing
136
+ # fn=validate_and_process,
137
+ # inputs=[query, constraints, images, top_k, report_format],
138
+ # outputs=[results_box, selected_images, confidence_scores, processing_status],
139
+ # #show_progress=True
140
+ # ).then(
141
+ # # Third: Update UI based on results
142
+ # fn=lambda results: update_ui_on_success(results) if results['status'] == 'success'
143
+ # else update_ui_on_error(results['error']),
144
+ # inputs=[processing_status],
145
+ # outputs=[results_box, selected_images, confidence_scores, processing_status]
146
+ # )
147
+ # Update the click handler
148
+ submit_btn.click(
149
+ # First clear/reset outputs
150
+ fn=lambda: (
151
+ "Processing...", # for results_box
152
+ None, # for selected_images
153
+ None, # for confidence_scores
154
+ {"status": "processing"} # for processing_status
155
+ ),
156
+ inputs=None,
157
+ outputs=[
158
+ results_box,
159
+ selected_images,
160
+ confidence_scores,
161
+ processing_status
162
+ ]
163
+ ).then( # Then process inputs
164
+ fn=handler.process_inputs,
165
+ inputs=[
166
+ query,
167
+ constraints,
168
+ images,
169
+ top_k,
170
+ report_format
171
+ ],
172
+ outputs=[
173
+ results_box,
174
+ selected_images,
175
+ confidence_scores,
176
+ processing_status
177
+ ]
178
+ )
179
+
180
+ return app
181
+
182
+ # Launch the interface
183
+ if __name__ == "__main__":
184
+ interface = create_interface()
185
+ interface.launch()