tonyliu404 commited on
Commit
9550b46
·
verified ·
1 Parent(s): 930c3b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -33
app.py CHANGED
@@ -17,6 +17,7 @@ import json
17
  import matplotlib.pyplot as plt
18
  from matplotlib.colors import LinearSegmentedColormap
19
  import textwrap
 
20
 
21
  st.set_page_config(
22
  page_title="Food Chain",
@@ -291,45 +292,84 @@ def display_dishes_in_grid(dishes, cols=3):
291
  st.sidebar.write(dish.replace("_", " ").capitalize())
292
 
293
  def display_prediction_graph(class_names, confidences):
294
- #reversing them so graph displays highest predictions at the top
295
- confidences.reverse()
 
 
296
  class_names.reverse()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
- #display as a graph
299
- norm = plt.Normalize(min(confidences), max(confidences))
300
- cmap = LinearSegmentedColormap.from_list("grey_orange", ["#808080", "#FFA500"]) #color map grey to orange
 
 
 
 
 
 
 
 
301
 
302
- fig, ax = plt.subplots(figsize=(12, 6))
303
- bars = ax.barh(class_names, confidences, color=cmap(norm(confidences)))
304
-
305
- fig.patch.set_alpha(0) # Transparent background
306
- ax.set_facecolor('none')
307
-
308
- min_width = 0.07 * ax.get_xlim()[1] # 7% of the x-axis range
309
- # Add labels inside the bars, aligned to the right
310
- for bar in bars:
311
- original_width = bar.get_width()
312
- width = original_width
313
- if width < min_width:
314
- width = min_width
315
- ax.text(width - 0.02, bar.get_y() + bar.get_height()/2, f'{original_width:.1f}%',
316
- va='center', ha='right', color='white', fontweight='bold', fontsize=16)
317
-
318
- ax.set_xticklabels([]) #remove x label
319
-
320
- # Wrapping labels
321
- max_label_width = 10
322
- labels = ax.get_yticklabels()
323
- wrapped_labels = [textwrap.fill(label.get_text(), width=max_label_width) for label in labels] # Wrap the labels if they exceed the max width
324
- ax.set_yticklabels(wrapped_labels, fontsize=16, color='white')
325
 
326
- #no borders
327
- for spine in ax.spines.values():
328
- spine.set_visible(False)
329
 
330
- ax.set_title(class_names[-1], color='white', fontsize=24, fontweight='bold', ha='left', x=0.5)
331
 
332
- st.pyplot(fig) # Display the plot
333
 
334
  # #Streamlit
335
 
 
17
  import matplotlib.pyplot as plt
18
  from matplotlib.colors import LinearSegmentedColormap
19
  import textwrap
20
+ import plotly.graph_objects as go
21
 
22
  st.set_page_config(
23
  page_title="Food Chain",
 
292
  st.sidebar.write(dish.replace("_", " ").capitalize())
293
 
294
  def display_prediction_graph(class_names, confidences):
295
+ # Create a list of labels and values from the predictions dictionary
296
+ values = [round(value, 2) for value in confidences]
297
+
298
+ # Determine the top prediction
299
  class_names.reverse()
300
+ values.reverse()
301
+ top_prediction = class_names[0]
302
+
303
+ # Create a horizontal bar chart
304
+ fig = go.Figure(go.Bar(
305
+ x=values,
306
+ y=class_names,
307
+ orientation='h',
308
+ marker=dict(color='orange'),
309
+ text=values, # Display values on the bars
310
+ textposition='outside' # Position the text outside the bars
311
+ ))
312
+
313
+ # Update layout for better appearance
314
+ fig.update_layout(
315
+ title=f"Prediction: {top_prediction}",
316
+ margin=dict(l=20, r=20, t=60, b=20),
317
+ xaxis=dict(
318
+ showgrid=False, # No grid lines for the x-axis
319
+ ticks='', # No x-axis ticks
320
+ showticklabels=False # No x-axis tick labels
321
+ ),
322
+ yaxis=dict(
323
+ showgrid=False # No grid lines for the y-axis
324
+ ),
325
+ plot_bgcolor='rgba(0,0,0,0)', # No background color for the plot area
326
+ paper_bgcolor='rgba(0,0,0,0)', # No background color for the paper area
327
+ font=dict() # Default font color
328
+ )
329
 
330
+ # Display the chart in Streamlit
331
+ st.plotly_chart(fig)
332
+
333
+ # def display_prediction_graph(class_names, confidences):
334
+ # #reversing them so graph displays highest predictions at the top
335
+ # confidences.reverse()
336
+ # class_names.reverse()
337
+
338
+ # #display as a graph
339
+ # norm = plt.Normalize(min(confidences), max(confidences))
340
+ # cmap = LinearSegmentedColormap.from_list("grey_orange", ["#808080", "#FFA500"]) #color map grey to orange
341
 
342
+ # fig, ax = plt.subplots(figsize=(12, 6))
343
+ # bars = ax.barh(class_names, confidences, color=cmap(norm(confidences)))
344
+
345
+ # fig.patch.set_alpha(0) # Transparent background
346
+ # ax.set_facecolor('none')
347
+
348
+ # min_width = 0.07 * ax.get_xlim()[1] # 7% of the x-axis range
349
+ # # Add labels inside the bars, aligned to the right
350
+ # for bar in bars:
351
+ # original_width = bar.get_width()
352
+ # width = original_width
353
+ # if width < min_width:
354
+ # width = min_width
355
+ # ax.text(width - 0.02, bar.get_y() + bar.get_height()/2, f'{original_width:.1f}%',
356
+ # va='center', ha='right', color='white', fontweight='bold', fontsize=16)
357
+
358
+ # ax.set_xticklabels([]) #remove x label
359
+
360
+ # # Wrapping labels
361
+ # max_label_width = 10
362
+ # labels = ax.get_yticklabels()
363
+ # wrapped_labels = [textwrap.fill(label.get_text(), width=max_label_width) for label in labels] # Wrap the labels if they exceed the max width
364
+ # ax.set_yticklabels(wrapped_labels, fontsize=16, color='white')
365
 
366
+ # #no borders
367
+ # for spine in ax.spines.values():
368
+ # spine.set_visible(False)
369
 
370
+ # ax.set_title(class_names[-1], color='white', fontsize=24, fontweight='bold', ha='left', x=0.5)
371
 
372
+ # st.pyplot(fig) # Display the plot
373
 
374
  # #Streamlit
375