dhruvsaxena11 commited on
Commit
74003ac
·
verified ·
1 Parent(s): 3e99fcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -63,10 +63,10 @@ def plot_sensor_data(df):
63
  plt.close(fig)
64
  return Image.open(fig_path)
65
 
66
- pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
67
 
68
- # Generate recommendations
69
- def analyze_data(image, plot_path):
70
  try:
71
  # Ensure image is a PIL object
72
  if isinstance(image, np.ndarray):
@@ -75,15 +75,25 @@ def analyze_data(image, plot_path):
75
  # Damage analysis
76
  damage_output = pipe(inputs=image)[0]['generated_text'] if image else "No image uploaded."
77
 
78
- # Graph analysis
79
- graph_analysis = pipe(inputs=plot_path)[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
80
 
81
  # Recommendations
82
- recommendations = f"Recommendations based on analysis:\n\n1. {graph_analysis}\n\n2. {damage_output}"
83
- return recommendations, plot_path, graph_analysis
84
 
85
  except Exception as e:
86
- return f"Error occurred: {str(e)}", None, None
87
 
88
  # Gradio UI
89
  with gr.Blocks(css=".output-text { font-family: 'Arial'; color: #222; font-size: 1rem; }") as app:
@@ -93,10 +103,9 @@ with gr.Blocks(css=".output-text { font-family: 'Arial'; color: #222; font-size:
93
  with gr.Row():
94
  display_graph = gr.Image(value=plot_sensor_data(sensor_data), type="pil", label="Sensor Data Over Time")
95
  recommendations = gr.Textbox(label="Analysis & Recommendations", placeholder="Insights will appear here...")
96
- graph_insights = gr.Textbox(label="Graph Insights", placeholder="Graph insights will appear here...")
97
  data_table = gr.Dataframe(sensor_data, label="Generated Sensor Data (Table View)", row_count=(10, "fixed"), interactive=False)
98
 
99
- # Realistic colors and UI layout for a polished look
100
- car_image.change(fn=analyze_data, inputs=[car_image, display_graph], outputs=[recommendations, display_graph, graph_insights])
101
 
102
  app.launch()
 
63
  plt.close(fig)
64
  return Image.open(fig_path)
65
 
66
+ pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
67
 
68
+ # Generate recommendations and detect anomalies without model
69
+ def analyze_data(image, df):
70
  try:
71
  # Ensure image is a PIL object
72
  if isinstance(image, np.ndarray):
 
75
  # Damage analysis
76
  damage_output = pipe(inputs=image)[0]['generated_text'] if image else "No image uploaded."
77
 
78
+ # Anomaly detection based on sensor thresholds
79
+ anomalies = []
80
+ thresholds = {"engine_temp": 100, "oil_pressure": 25, "tire_pressure": 28, "battery_voltage": 11.5}
81
+
82
+ if any(df["engine_temp"] > thresholds["engine_temp"]):
83
+ anomalies.append("Engine temperature is above normal.")
84
+ if any(df["oil_pressure"] < thresholds["oil_pressure"]):
85
+ anomalies.append("Oil pressure is below normal.")
86
+ if any(df["tire_pressure"] < thresholds["tire_pressure"]):
87
+ anomalies.append("Tire pressure is below normal.")
88
+ if any(df["battery_voltage"] < thresholds["battery_voltage"]):
89
+ anomalies.append("Battery voltage is below normal.")
90
 
91
  # Recommendations
92
+ recommendations = f"Recommendations based on analysis:\n\n1. {'; '.join(anomalies)}\n\n2. {damage_output}"
93
+ return recommendations, plot_sensor_data(df)
94
 
95
  except Exception as e:
96
+ return f"Error occurred: {str(e)}", None
97
 
98
  # Gradio UI
99
  with gr.Blocks(css=".output-text { font-family: 'Arial'; color: #222; font-size: 1rem; }") as app:
 
103
  with gr.Row():
104
  display_graph = gr.Image(value=plot_sensor_data(sensor_data), type="pil", label="Sensor Data Over Time")
105
  recommendations = gr.Textbox(label="Analysis & Recommendations", placeholder="Insights will appear here...")
 
106
  data_table = gr.Dataframe(sensor_data, label="Generated Sensor Data (Table View)", row_count=(10, "fixed"), interactive=False)
107
 
108
+ # Set up Gradio interaction
109
+ car_image.change(fn=analyze_data, inputs=[car_image, sensor_data], outputs=[recommendations, display_graph])
110
 
111
  app.launch()