Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,6 +56,7 @@ except Exception as e:
|
|
| 56 |
tokenizer = model = None
|
| 57 |
|
| 58 |
def generate_with_logprobs(prompt, max_new_tokens=100):
|
|
|
|
| 59 |
if tokenizer is None or model is None:
|
| 60 |
return "[Model not loaded]", -10.0
|
| 61 |
inputs = tokenizer(prompt, return_tensors="pt")
|
|
@@ -138,11 +139,13 @@ iot_sim = IoTSimulator()
|
|
| 138 |
iot_history = [] # store recent readings for prediction
|
| 139 |
|
| 140 |
# ----------------------------------------------------------------------
|
| 141 |
-
# Helper: update risk with feedback
|
|
|
|
| 142 |
# ----------------------------------------------------------------------
|
| 143 |
last_task_category = None
|
| 144 |
|
| 145 |
def feedback(thumbs_up: bool):
|
|
|
|
| 146 |
global last_task_category
|
| 147 |
if last_task_category is None:
|
| 148 |
return "No previous analysis to rate."
|
|
@@ -153,6 +156,7 @@ def feedback(thumbs_up: bool):
|
|
| 153 |
# Async handlers for each tab
|
| 154 |
# ----------------------------------------------------------------------
|
| 155 |
async def handle_text(task_type, prompt):
|
|
|
|
| 156 |
global last_task_category
|
| 157 |
last_task_category = task_type
|
| 158 |
try:
|
|
@@ -196,6 +200,7 @@ async def handle_text(task_type, prompt):
|
|
| 196 |
return {"error": str(e)}
|
| 197 |
|
| 198 |
async def handle_image(prompt):
|
|
|
|
| 199 |
global last_task_category
|
| 200 |
last_task_category = "image"
|
| 201 |
if image_pipe is None:
|
|
@@ -239,6 +244,7 @@ async def handle_image(prompt):
|
|
| 239 |
return {"error": str(e)}, None
|
| 240 |
|
| 241 |
async def handle_audio(audio_file):
|
|
|
|
| 242 |
global last_task_category
|
| 243 |
last_task_category = "audio"
|
| 244 |
if audio_pipe is None:
|
|
@@ -287,6 +293,7 @@ async def handle_audio(audio_file):
|
|
| 287 |
return {"error": str(e)}
|
| 288 |
|
| 289 |
async def read_iot_sensors(fault_type):
|
|
|
|
| 290 |
global last_task_category, iot_history
|
| 291 |
last_task_category = "iot"
|
| 292 |
iot_sim.set_fault(fault_type if fault_type != "none" else None)
|
|
@@ -313,7 +320,7 @@ async def read_iot_sensors(fault_type):
|
|
| 313 |
# Run diagnostician
|
| 314 |
diag_result = await robotics_diagnostician.analyze(event)
|
| 315 |
|
| 316 |
-
# Simple failure prediction
|
| 317 |
prediction = None
|
| 318 |
if len(iot_history) >= 5:
|
| 319 |
temps = [h['temperature'] for h in iot_history[-5:]]
|
|
@@ -369,7 +376,7 @@ with gr.Blocks(title="ARF v4 – AI Reliability Lab", theme="soft") as demo:
|
|
| 369 |
diag_display = gr.JSON(label="Diagnosis")
|
| 370 |
pred_display = gr.JSON(label="Failure Prediction")
|
| 371 |
|
| 372 |
-
# Feedback row
|
| 373 |
with gr.Row():
|
| 374 |
feedback_up = gr.Button("👍 Correct")
|
| 375 |
feedback_down = gr.Button("👎 Incorrect")
|
|
|
|
| 56 |
tokenizer = model = None
|
| 57 |
|
| 58 |
def generate_with_logprobs(prompt, max_new_tokens=100):
|
| 59 |
+
"""Generate text and return (generated_text, avg_log_prob)."""
|
| 60 |
if tokenizer is None or model is None:
|
| 61 |
return "[Model not loaded]", -10.0
|
| 62 |
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
| 139 |
iot_history = [] # store recent readings for prediction
|
| 140 |
|
| 141 |
# ----------------------------------------------------------------------
|
| 142 |
+
# Helper: update risk with feedback (global state – shared across users)
|
| 143 |
+
# For per‑session risk, use gr.State instead of globals.
|
| 144 |
# ----------------------------------------------------------------------
|
| 145 |
last_task_category = None
|
| 146 |
|
| 147 |
def feedback(thumbs_up: bool):
|
| 148 |
+
"""Handle user feedback to update Beta priors."""
|
| 149 |
global last_task_category
|
| 150 |
if last_task_category is None:
|
| 151 |
return "No previous analysis to rate."
|
|
|
|
| 156 |
# Async handlers for each tab
|
| 157 |
# ----------------------------------------------------------------------
|
| 158 |
async def handle_text(task_type, prompt):
|
| 159 |
+
"""Handle text generation and analysis."""
|
| 160 |
global last_task_category
|
| 161 |
last_task_category = task_type
|
| 162 |
try:
|
|
|
|
| 200 |
return {"error": str(e)}
|
| 201 |
|
| 202 |
async def handle_image(prompt):
|
| 203 |
+
"""Handle image generation and quality analysis."""
|
| 204 |
global last_task_category
|
| 205 |
last_task_category = "image"
|
| 206 |
if image_pipe is None:
|
|
|
|
| 244 |
return {"error": str(e)}, None
|
| 245 |
|
| 246 |
async def handle_audio(audio_file):
|
| 247 |
+
"""Handle audio transcription and quality analysis."""
|
| 248 |
global last_task_category
|
| 249 |
last_task_category = "audio"
|
| 250 |
if audio_pipe is None:
|
|
|
|
| 293 |
return {"error": str(e)}
|
| 294 |
|
| 295 |
async def read_iot_sensors(fault_type):
|
| 296 |
+
"""Read simulated IoT sensors, run diagnostics, and predict failure."""
|
| 297 |
global last_task_category, iot_history
|
| 298 |
last_task_category = "iot"
|
| 299 |
iot_sim.set_fault(fault_type if fault_type != "none" else None)
|
|
|
|
| 320 |
# Run diagnostician
|
| 321 |
diag_result = await robotics_diagnostician.analyze(event)
|
| 322 |
|
| 323 |
+
# Simple failure prediction (linear extrapolation)
|
| 324 |
prediction = None
|
| 325 |
if len(iot_history) >= 5:
|
| 326 |
temps = [h['temperature'] for h in iot_history[-5:]]
|
|
|
|
| 376 |
diag_display = gr.JSON(label="Diagnosis")
|
| 377 |
pred_display = gr.JSON(label="Failure Prediction")
|
| 378 |
|
| 379 |
+
# Feedback row (shared across all users – for demo purposes)
|
| 380 |
with gr.Row():
|
| 381 |
feedback_up = gr.Button("👍 Correct")
|
| 382 |
feedback_down = gr.Button("👎 Incorrect")
|