Update app.py
Browse files
app.py
CHANGED
@@ -108,79 +108,126 @@ def conversation(qa_chain, message, history):
|
|
108 |
new_history = history + [(message, response_answer)]
|
109 |
return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
|
110 |
|
|
|
|
|
|
|
111 |
def demo():
|
112 |
-
"""Main demo application"""
|
113 |
theme = gr.themes.Default(
|
114 |
primary_hue="indigo",
|
115 |
secondary_hue="blue",
|
116 |
neutral_hue="slate",
|
117 |
)
|
118 |
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
vector_db = gr.State()
|
121 |
qa_chain = gr.State()
|
122 |
|
123 |
-
# Header
|
124 |
-
gr.
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
|
133 |
-
#
|
134 |
-
gr.
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
value="Waiting for documents...",
|
167 |
-
label="Status"
|
168 |
-
)
|
169 |
-
|
170 |
-
gr.Markdown("### 🤖 Analysis Engine Configuration")
|
171 |
-
gr.Markdown("Select and configure the AI model to best meet your needs.")
|
172 |
-
|
173 |
-
with gr.Row():
|
174 |
-
llm_btn = gr.Radio(
|
175 |
-
choices=list_llm_simple,
|
176 |
-
label="Available AI Models",
|
177 |
-
value=list_llm_simple[0],
|
178 |
-
type="index"
|
179 |
-
)
|
180 |
-
|
181 |
-
with gr.Row():
|
182 |
-
with gr.Accordion("Advanced Analysis Parameters", open=False):
|
183 |
-
with gr.Row():
|
184 |
slider_temperature = gr.Slider(
|
185 |
minimum=0.01,
|
186 |
maximum=1.0,
|
@@ -188,7 +235,6 @@ def demo():
|
|
188 |
step=0.1,
|
189 |
label="Analysis Precision"
|
190 |
)
|
191 |
-
with gr.Row():
|
192 |
slider_maxtokens = gr.Slider(
|
193 |
minimum=128,
|
194 |
maximum=9192,
|
@@ -196,7 +242,6 @@ def demo():
|
|
196 |
step=128,
|
197 |
label="Response Length"
|
198 |
)
|
199 |
-
with gr.Row():
|
200 |
slider_topk = gr.Slider(
|
201 |
minimum=1,
|
202 |
maximum=10,
|
@@ -204,90 +249,82 @@ def demo():
|
|
204 |
step=1,
|
205 |
label="Analysis Diversity"
|
206 |
)
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
value="Waiting for initialization...",
|
213 |
-
label="Assistant Status"
|
214 |
-
)
|
215 |
-
|
216 |
-
with gr.Column(scale=200):
|
217 |
-
gr.Markdown(
|
218 |
-
"""
|
219 |
-
### 💬 Step 2: Expert Consultation and Analysis
|
220 |
-
Ask questions about your metrology reports. The assistant will provide detailed technical analyses.
|
221 |
-
|
222 |
-
**Suggested questions:**
|
223 |
-
- Analyze the calibration results of this instrument
|
224 |
-
- Verify compliance with technical standards
|
225 |
-
- Identify critical points in measurements
|
226 |
-
- Compare results with specified limits
|
227 |
-
- Evaluate measurement uncertainty
|
228 |
-
- Assess calibration intervals
|
229 |
-
"""
|
230 |
-
)
|
231 |
-
chatbot = gr.Chatbot(
|
232 |
-
height=505,
|
233 |
-
label="Metrology Analysis"
|
234 |
-
)
|
235 |
-
|
236 |
-
with gr.Accordion("Source Document References", open=False):
|
237 |
-
with gr.Row():
|
238 |
-
doc_source1 = gr.Textbox(
|
239 |
-
label="Technical Reference 1",
|
240 |
-
lines=2
|
241 |
-
)
|
242 |
-
source1_page = gr.Number(label="Page")
|
243 |
-
with gr.Row():
|
244 |
-
doc_source2 = gr.Textbox(
|
245 |
-
label="Technical Reference 2",
|
246 |
-
lines=2
|
247 |
)
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
)
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
# Footer
|
269 |
-
gr.
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
- Verification of compliance with technical standards
|
282 |
-
- Decision support in metrological processes
|
283 |
-
- Uncertainty analysis and measurement traceability
|
284 |
-
- Quality control and measurement system analysis
|
285 |
-
|
286 |
-
*Version 1.0 - Updated 2024*
|
287 |
-
"""
|
288 |
-
)
|
289 |
|
290 |
-
# Event
|
291 |
db_btn.click(
|
292 |
initialize_database,
|
293 |
inputs=[document],
|
|
|
108 |
new_history = history + [(message, response_answer)]
|
109 |
return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
|
110 |
|
111 |
+
|
112 |
+
# [Previous imports remain the same...]
|
113 |
+
|
114 |
def demo():
|
115 |
+
"""Main demo application with enhanced layout"""
|
116 |
theme = gr.themes.Default(
|
117 |
primary_hue="indigo",
|
118 |
secondary_hue="blue",
|
119 |
neutral_hue="slate",
|
120 |
)
|
121 |
|
122 |
+
# Custom CSS for advanced layout
|
123 |
+
custom_css = """
|
124 |
+
#app-header {
|
125 |
+
text-align: center;
|
126 |
+
padding: 2rem;
|
127 |
+
background: linear-gradient(to right, #1a365d, #2c5282);
|
128 |
+
color: white;
|
129 |
+
margin-bottom: 2rem;
|
130 |
+
border-radius: 0 0 1rem 1rem;
|
131 |
+
}
|
132 |
+
#app-header h1 {
|
133 |
+
font-size: 2.5rem;
|
134 |
+
margin-bottom: 0.5rem;
|
135 |
+
color: white;
|
136 |
+
}
|
137 |
+
#app-header p {
|
138 |
+
font-size: 1.2rem;
|
139 |
+
opacity: 0.9;
|
140 |
+
}
|
141 |
+
.container {
|
142 |
+
max-width: 1400px;
|
143 |
+
margin: 0 auto;
|
144 |
+
padding: 0 1rem;
|
145 |
+
}
|
146 |
+
.features-grid {
|
147 |
+
display: grid;
|
148 |
+
grid-template-columns: repeat(2, 1fr);
|
149 |
+
gap: 1rem;
|
150 |
+
margin-bottom: 2rem;
|
151 |
+
}
|
152 |
+
.feature-card {
|
153 |
+
background: #f8fafc;
|
154 |
+
padding: 1.5rem;
|
155 |
+
border-radius: 0.5rem;
|
156 |
+
border: 1px solid #e2e8f0;
|
157 |
+
}
|
158 |
+
.section-title {
|
159 |
+
font-size: 1.5rem;
|
160 |
+
color: #1a365d;
|
161 |
+
margin-bottom: 1rem;
|
162 |
+
padding-bottom: 0.5rem;
|
163 |
+
border-bottom: 2px solid #e2e8f0;
|
164 |
+
}
|
165 |
+
.control-panel {
|
166 |
+
background: #f8fafc;
|
167 |
+
padding: 1.5rem;
|
168 |
+
border-radius: 0.5rem;
|
169 |
+
margin-bottom: 1rem;
|
170 |
+
}
|
171 |
+
.chat-container {
|
172 |
+
background: white;
|
173 |
+
border-radius: 0.5rem;
|
174 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
175 |
+
}
|
176 |
+
.reference-panel {
|
177 |
+
background: #f8fafc;
|
178 |
+
padding: 1rem;
|
179 |
+
border-radius: 0.5rem;
|
180 |
+
margin-top: 1rem;
|
181 |
+
}
|
182 |
+
"""
|
183 |
+
|
184 |
+
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
185 |
vector_db = gr.State()
|
186 |
qa_chain = gr.State()
|
187 |
|
188 |
+
# Enhanced Header
|
189 |
+
with gr.Row(elem_id="app-header"):
|
190 |
+
with gr.Column():
|
191 |
+
gr.HTML(
|
192 |
+
"""
|
193 |
+
<h1>MetroAssist AI</h1>
|
194 |
+
<p>Expert System for Metrology Report Analysis</p>
|
195 |
+
"""
|
196 |
+
)
|
197 |
|
198 |
+
# Main Content Container
|
199 |
+
with gr.Row(equal_height=True):
|
200 |
+
# Left Column - Control Panel
|
201 |
+
with gr.Column(scale=1):
|
202 |
+
with gr.Group(visible=True) as control_panel:
|
203 |
+
gr.Markdown("## Document Processing", elem_classes="section-title")
|
204 |
+
|
205 |
+
# File Upload Section
|
206 |
+
with gr.Box(elem_classes="control-panel"):
|
207 |
+
gr.Markdown("### 📄 Upload Documents")
|
208 |
+
document = gr.Files(
|
209 |
+
label="Metrology Reports (PDF)",
|
210 |
+
file_count="multiple",
|
211 |
+
file_types=["pdf"],
|
212 |
+
)
|
213 |
+
db_btn = gr.Button("Process Documents", elem_classes="primary-btn")
|
214 |
+
db_progress = gr.Textbox(
|
215 |
+
value="Ready for documents",
|
216 |
+
label="Processing Status",
|
217 |
+
)
|
218 |
+
|
219 |
+
# Model Selection Section
|
220 |
+
with gr.Box(elem_classes="control-panel"):
|
221 |
+
gr.Markdown("### 🤖 Model Configuration")
|
222 |
+
llm_btn = gr.Radio(
|
223 |
+
choices=list_llm_simple,
|
224 |
+
label="Select AI Model",
|
225 |
+
value=list_llm_simple[0],
|
226 |
+
type="index"
|
227 |
+
)
|
228 |
+
|
229 |
+
# Advanced Parameters
|
230 |
+
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
slider_temperature = gr.Slider(
|
232 |
minimum=0.01,
|
233 |
maximum=1.0,
|
|
|
235 |
step=0.1,
|
236 |
label="Analysis Precision"
|
237 |
)
|
|
|
238 |
slider_maxtokens = gr.Slider(
|
239 |
minimum=128,
|
240 |
maximum=9192,
|
|
|
242 |
step=128,
|
243 |
label="Response Length"
|
244 |
)
|
|
|
245 |
slider_topk = gr.Slider(
|
246 |
minimum=1,
|
247 |
maximum=10,
|
|
|
249 |
step=1,
|
250 |
label="Analysis Diversity"
|
251 |
)
|
252 |
+
|
253 |
+
qachain_btn = gr.Button("Initialize Assistant")
|
254 |
+
llm_progress = gr.Textbox(
|
255 |
+
value="Not initialized",
|
256 |
+
label="Assistant Status"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
)
|
258 |
+
|
259 |
+
# Right Column - Chat Interface
|
260 |
+
with gr.Column(scale=2):
|
261 |
+
with gr.Group() as chat_interface:
|
262 |
+
gr.Markdown("## Interactive Analysis", elem_classes="section-title")
|
263 |
+
|
264 |
+
# Feature Cards
|
265 |
+
with gr.Row(equal_height=True) as feature_grid:
|
266 |
+
with gr.Column():
|
267 |
+
gr.Markdown(
|
268 |
+
"""
|
269 |
+
### 📊 Capabilities
|
270 |
+
- Calibration Analysis
|
271 |
+
- Standards Compliance
|
272 |
+
- Uncertainty Evaluation
|
273 |
+
"""
|
274 |
+
)
|
275 |
+
with gr.Column():
|
276 |
+
gr.Markdown(
|
277 |
+
"""
|
278 |
+
### 💡 Best Practices
|
279 |
+
- Ask specific questions
|
280 |
+
- Include measurement context
|
281 |
+
- Specify standards
|
282 |
+
"""
|
283 |
+
)
|
284 |
+
|
285 |
+
# Chat Interface
|
286 |
+
with gr.Box(elem_classes="chat-container"):
|
287 |
+
chatbot = gr.Chatbot(
|
288 |
+
height=400,
|
289 |
+
label="Analysis Conversation"
|
290 |
)
|
291 |
+
with gr.Row():
|
292 |
+
msg = gr.Textbox(
|
293 |
+
placeholder="Ask about your metrology report...",
|
294 |
+
label="Query",
|
295 |
+
scale=4
|
296 |
+
)
|
297 |
+
submit_btn = gr.Button("Send")
|
298 |
+
clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
|
299 |
+
|
300 |
+
# Reference Panel
|
301 |
+
with gr.Accordion("Document References", open=False, elem_classes="reference-panel"):
|
302 |
+
with gr.Row():
|
303 |
+
with gr.Column():
|
304 |
+
doc_source1 = gr.Textbox(label="Reference 1", lines=2)
|
305 |
+
source1_page = gr.Number(label="Page")
|
306 |
+
with gr.Column():
|
307 |
+
doc_source2 = gr.Textbox(label="Reference 2", lines=2)
|
308 |
+
source2_page = gr.Number(label="Page")
|
309 |
+
with gr.Column():
|
310 |
+
doc_source3 = gr.Textbox(label="Reference 3", lines=2)
|
311 |
+
source3_page = gr.Number(label="Page")
|
312 |
+
|
313 |
# Footer
|
314 |
+
with gr.Row():
|
315 |
+
gr.Markdown(
|
316 |
+
"""
|
317 |
+
---
|
318 |
+
### About MetroAssist AI
|
319 |
+
|
320 |
+
A specialized tool for metrology professionals, providing advanced analysis
|
321 |
+
of calibration certificates, measurement data, and technical standards compliance.
|
322 |
+
|
323 |
+
**Version 1.0** | © 2024 MetroAssist AI
|
324 |
+
"""
|
325 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
+
# Event Handlers
|
328 |
db_btn.click(
|
329 |
initialize_database,
|
330 |
inputs=[document],
|