Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 295 |
-
|
|
|
|
|
|
|
| 296 |
class_names.reverse()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
-
#
|
| 299 |
-
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
|
| 330 |
-
|
| 331 |
|
| 332 |
-
|
| 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 |
|