Update app.py
Browse files
app.py
CHANGED
@@ -109,8 +109,6 @@ def conversation(qa_chain, message, history):
|
|
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(
|
@@ -121,208 +119,154 @@ def demo():
|
|
121 |
|
122 |
# Custom CSS for advanced layout
|
123 |
custom_css = """
|
124 |
-
#
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
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 |
-
#
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
# Left Column - Control Panel
|
201 |
with gr.Column(scale=1):
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
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 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
label="Select AI Model",
|
225 |
-
value=list_llm_simple[0],
|
226 |
-
type="index"
|
227 |
)
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
value=0.5,
|
235 |
-
step=0.1,
|
236 |
-
label="Analysis Precision"
|
237 |
-
)
|
238 |
-
slider_maxtokens = gr.Slider(
|
239 |
-
minimum=128,
|
240 |
-
maximum=9192,
|
241 |
-
value=4096,
|
242 |
-
step=128,
|
243 |
-
label="Response Length"
|
244 |
-
)
|
245 |
-
slider_topk = gr.Slider(
|
246 |
-
minimum=1,
|
247 |
-
maximum=10,
|
248 |
-
value=3,
|
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 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
with gr.
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
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 |
-
|
321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
322 |
|
323 |
-
|
324 |
-
""
|
325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
# Event Handlers
|
328 |
db_btn.click(
|
|
|
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 |
def demo():
|
113 |
"""Main demo application with enhanced layout"""
|
114 |
theme = gr.themes.Default(
|
|
|
119 |
|
120 |
# Custom CSS for advanced layout
|
121 |
custom_css = """
|
122 |
+
.container {background: #ffffff; padding: 1rem; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);}
|
123 |
+
.header {text-align: center; margin-bottom: 2rem;}
|
124 |
+
.header h1 {color: #1a365d; font-size: 2.5rem; margin-bottom: 0.5rem;}
|
125 |
+
.header p {color: #4a5568; font-size: 1.2rem;}
|
126 |
+
.section {margin-bottom: 1.5rem; padding: 1rem; background: #f8fafc; border-radius: 8px;}
|
127 |
+
.control-panel {margin-bottom: 1rem;}
|
128 |
+
.chat-area {background: white; padding: 1rem; border-radius: 8px;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
"""
|
130 |
|
131 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
132 |
vector_db = gr.State()
|
133 |
qa_chain = gr.State()
|
134 |
|
135 |
+
# Header
|
136 |
+
gr.HTML(
|
137 |
+
"""
|
138 |
+
<div class="header">
|
139 |
+
<h1>MetroAssist AI</h1>
|
140 |
+
<p>Expert System for Metrology Report Analysis</p>
|
141 |
+
</div>
|
142 |
+
"""
|
143 |
+
)
|
144 |
|
145 |
+
with gr.Row():
|
146 |
+
# Left Column - Controls
|
|
|
147 |
with gr.Column(scale=1):
|
148 |
+
gr.Markdown("## Document Processing")
|
149 |
+
|
150 |
+
# File Upload Section
|
151 |
+
with gr.Column(elem_classes="section"):
|
152 |
+
gr.Markdown("### 📄 Upload Documents")
|
153 |
+
document = gr.Files(
|
154 |
+
label="Metrology Reports (PDF)",
|
155 |
+
file_count="multiple",
|
156 |
+
file_types=["pdf"]
|
157 |
+
)
|
158 |
+
db_btn = gr.Button("Process Documents")
|
159 |
+
db_progress = gr.Textbox(
|
160 |
+
value="Ready for documents",
|
161 |
+
label="Processing Status"
|
162 |
+
)
|
163 |
+
|
164 |
+
# Model Selection Section
|
165 |
+
with gr.Column(elem_classes="section"):
|
166 |
+
gr.Markdown("### 🤖 Model Configuration")
|
167 |
+
llm_btn = gr.Radio(
|
168 |
+
choices=list_llm_simple,
|
169 |
+
label="Select AI Model",
|
170 |
+
value=list_llm_simple[0],
|
171 |
+
type="index"
|
172 |
+
)
|
173 |
|
174 |
+
with gr.Accordion("Advanced Settings", open=False):
|
175 |
+
slider_temperature = gr.Slider(
|
176 |
+
minimum=0.01,
|
177 |
+
maximum=1.0,
|
178 |
+
value=0.5,
|
179 |
+
step=0.1,
|
180 |
+
label="Analysis Precision"
|
|
|
|
|
|
|
|
|
|
|
181 |
)
|
182 |
+
slider_maxtokens = gr.Slider(
|
183 |
+
minimum=128,
|
184 |
+
maximum=9192,
|
185 |
+
value=4096,
|
186 |
+
step=128,
|
187 |
+
label="Response Length"
|
|
|
|
|
|
|
188 |
)
|
189 |
+
slider_topk = gr.Slider(
|
190 |
+
minimum=1,
|
191 |
+
maximum=10,
|
192 |
+
value=3,
|
193 |
+
step=1,
|
194 |
+
label="Analysis Diversity"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
)
|
196 |
+
|
197 |
+
qachain_btn = gr.Button("Initialize Assistant")
|
198 |
+
llm_progress = gr.Textbox(
|
199 |
+
value="Not initialized",
|
200 |
+
label="Assistant Status"
|
201 |
+
)
|
202 |
|
203 |
# Right Column - Chat Interface
|
204 |
with gr.Column(scale=2):
|
205 |
+
gr.Markdown("## Interactive Analysis")
|
206 |
+
|
207 |
+
# Features Section
|
208 |
+
with gr.Row():
|
209 |
+
with gr.Column():
|
210 |
+
gr.Markdown(
|
211 |
+
"""
|
212 |
+
### 📊 Capabilities
|
213 |
+
- Calibration Analysis
|
214 |
+
- Standards Compliance
|
215 |
+
- Uncertainty Evaluation
|
216 |
+
"""
|
217 |
+
)
|
218 |
+
with gr.Column():
|
219 |
+
gr.Markdown(
|
220 |
+
"""
|
221 |
+
### 💡 Best Practices
|
222 |
+
- Ask specific questions
|
223 |
+
- Include measurement context
|
224 |
+
- Specify standards
|
225 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
+
# Chat Interface
|
229 |
+
with gr.Column(elem_classes="chat-area"):
|
230 |
+
chatbot = gr.Chatbot(
|
231 |
+
height=400,
|
232 |
+
label="Analysis Conversation"
|
233 |
+
)
|
234 |
+
with gr.Row():
|
235 |
+
msg = gr.Textbox(
|
236 |
+
placeholder="Ask about your metrology report...",
|
237 |
+
label="Query"
|
238 |
+
)
|
239 |
+
submit_btn = gr.Button("Send")
|
240 |
+
clear_btn = gr.ClearButton(
|
241 |
+
[msg, chatbot],
|
242 |
+
value="Clear"
|
243 |
+
)
|
244 |
|
245 |
+
# References Section
|
246 |
+
with gr.Accordion("Document References", open=False):
|
247 |
+
with gr.Row():
|
248 |
+
with gr.Column():
|
249 |
+
doc_source1 = gr.Textbox(label="Reference 1", lines=2)
|
250 |
+
source1_page = gr.Number(label="Page")
|
251 |
+
with gr.Column():
|
252 |
+
doc_source2 = gr.Textbox(label="Reference 2", lines=2)
|
253 |
+
source2_page = gr.Number(label="Page")
|
254 |
+
with gr.Column():
|
255 |
+
doc_source3 = gr.Textbox(label="Reference 3", lines=2)
|
256 |
+
source3_page = gr.Number(label="Page")
|
257 |
+
|
258 |
+
# Footer
|
259 |
+
gr.Markdown(
|
260 |
+
"""
|
261 |
+
---
|
262 |
+
### About MetroAssist AI
|
263 |
+
|
264 |
+
A specialized tool for metrology professionals, providing advanced analysis
|
265 |
+
of calibration certificates, measurement data, and technical standards compliance.
|
266 |
+
|
267 |
+
**Version 1.0** | © 2024 MetroAssist AI
|
268 |
+
"""
|
269 |
+
)
|
270 |
|
271 |
# Event Handlers
|
272 |
db_btn.click(
|