radub23
commited on
Commit
·
1384d3c
1
Parent(s):
aefec06
Revert to original code that worked for manual upload with minimal changes
Browse files
app.py
CHANGED
|
@@ -46,64 +46,20 @@ def detect_warning_lamp(image, history: list[tuple[str, str]], system_message):
|
|
| 46 |
return history
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
# Print debug info
|
| 50 |
-
print(f"Image type: {type(image)}")
|
| 51 |
-
|
| 52 |
# Convert PIL image to FastAI compatible format
|
| 53 |
img = PILImage(image)
|
| 54 |
-
print(f"Converted to PILImage: {type(img)}")
|
| 55 |
|
| 56 |
# Get model prediction
|
| 57 |
-
print("Running prediction...")
|
| 58 |
pred_class, pred_idx, probs = learn_inf.predict(img)
|
| 59 |
|
| 60 |
-
# Print debug info about prediction results
|
| 61 |
-
print(f"Prediction class type: {type(pred_class)}, value: {pred_class}")
|
| 62 |
-
print(f"Prediction index type: {type(pred_idx)}, value: {pred_idx}")
|
| 63 |
-
print(f"Probabilities type: {type(probs)}, shape: {probs.shape if hasattr(probs, 'shape') else 'no shape'}")
|
| 64 |
-
|
| 65 |
-
# Safely convert tensors to Python types
|
| 66 |
-
try:
|
| 67 |
-
# Handle pred_class (could be string or tensor)
|
| 68 |
-
if hasattr(pred_class, 'item'):
|
| 69 |
-
pred_class_str = str(pred_class.item())
|
| 70 |
-
else:
|
| 71 |
-
pred_class_str = str(pred_class)
|
| 72 |
-
|
| 73 |
-
# Handle pred_idx (convert tensor to int)
|
| 74 |
-
if hasattr(pred_idx, 'item'):
|
| 75 |
-
pred_idx_int = pred_idx.item()
|
| 76 |
-
else:
|
| 77 |
-
pred_idx_int = int(pred_idx)
|
| 78 |
-
|
| 79 |
-
# Get confidence score for predicted class
|
| 80 |
-
if hasattr(probs[pred_idx_int], 'item'):
|
| 81 |
-
confidence = probs[pred_idx_int].item()
|
| 82 |
-
else:
|
| 83 |
-
confidence = float(probs[pred_idx_int])
|
| 84 |
-
|
| 85 |
-
print(f"Converted values - class: {pred_class_str}, index: {pred_idx_int}, confidence: {confidence}")
|
| 86 |
-
except Exception as conversion_error:
|
| 87 |
-
print(f"Error during tensor conversion: {conversion_error}")
|
| 88 |
-
raise
|
| 89 |
-
|
| 90 |
# Format the prediction results
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
# Add probabilities for all classes
|
| 94 |
response += "\n\nProbabilities for all classes:"
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
for i, cls in enumerate(learn_inf.dls.vocab):
|
| 98 |
-
try:
|
| 99 |
-
if hasattr(probs[i], 'item'):
|
| 100 |
-
prob_value = probs[i].item()
|
| 101 |
-
else:
|
| 102 |
-
prob_value = float(probs[i])
|
| 103 |
-
response += f"\n- {cls}: {prob_value:.2%}"
|
| 104 |
-
except Exception as prob_error:
|
| 105 |
-
print(f"Error processing probability for class {cls}: {prob_error}")
|
| 106 |
-
response += f"\n- {cls}: Error"
|
| 107 |
|
| 108 |
# Update chat history
|
| 109 |
history.append((None, response))
|
|
@@ -111,8 +67,6 @@ def detect_warning_lamp(image, history: list[tuple[str, str]], system_message):
|
|
| 111 |
except Exception as e:
|
| 112 |
error_msg = f"Error processing image: {str(e)}"
|
| 113 |
print(f"Exception in detect_warning_lamp: {e}")
|
| 114 |
-
import traceback
|
| 115 |
-
traceback.print_exc()
|
| 116 |
history.append((None, error_msg))
|
| 117 |
return history
|
| 118 |
|
|
|
|
| 46 |
return history
|
| 47 |
|
| 48 |
try:
|
|
|
|
|
|
|
|
|
|
| 49 |
# Convert PIL image to FastAI compatible format
|
| 50 |
img = PILImage(image)
|
|
|
|
| 51 |
|
| 52 |
# Get model prediction
|
|
|
|
| 53 |
pred_class, pred_idx, probs = learn_inf.predict(img)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Format the prediction results
|
| 56 |
+
confidence = float(probs[pred_idx]) # Convert to float for better formatting
|
| 57 |
+
response = f"Detected Warning Lamp: {pred_class}\nConfidence: {confidence:.2%}"
|
| 58 |
|
| 59 |
# Add probabilities for all classes
|
| 60 |
response += "\n\nProbabilities for all classes:"
|
| 61 |
+
for i, (cls, prob) in enumerate(zip(learn_inf.dls.vocab, probs)):
|
| 62 |
+
response += f"\n- {cls}: {float(prob):.2%}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# Update chat history
|
| 65 |
history.append((None, response))
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
error_msg = f"Error processing image: {str(e)}"
|
| 69 |
print(f"Exception in detect_warning_lamp: {e}")
|
|
|
|
|
|
|
| 70 |
history.append((None, error_msg))
|
| 71 |
return history
|
| 72 |
|