Spaces:
Sleeping
Sleeping
added gradio app
Browse files- app.py +107 -0
- requirements.txt +3 -1
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# filepath: /Users/tristanhearn/code/maximum-submatrix-sum/app.py
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from algorithms import brute_submatrix_max, fft_submatrix_max, kidane_max_submatrix
|
| 8 |
+
|
| 9 |
+
# Function to generate a random 10x10 matrix with numbers having 1 decimal place
|
| 10 |
+
def generate_random_matrix(rows=10, cols=10):
|
| 11 |
+
# Generate random floats between -10 and 10 with 1 decimal place
|
| 12 |
+
matrix = np.round(np.random.uniform(-10, 10, size=(rows, cols)), 1)
|
| 13 |
+
# Convert to Pandas DataFrame
|
| 14 |
+
df = pd.DataFrame(matrix)
|
| 15 |
+
return df
|
| 16 |
+
|
| 17 |
+
# Function to process the matrix and find the maximum submatrix sum
|
| 18 |
+
def process_matrix(matrix_df, algorithm):
|
| 19 |
+
try:
|
| 20 |
+
# Convert input to numpy array
|
| 21 |
+
matrix_array = matrix_df.values.astype(float)
|
| 22 |
+
|
| 23 |
+
# Select the appropriate algorithm
|
| 24 |
+
if algorithm == "Brute Force":
|
| 25 |
+
loc, max_sum, time_taken = brute_submatrix_max(matrix_array)
|
| 26 |
+
elif algorithm == "FFT":
|
| 27 |
+
loc, max_sum, time_taken = fft_submatrix_max(matrix_array)
|
| 28 |
+
else: # Kidane method
|
| 29 |
+
loc, max_sum, time_taken = kidane_max_submatrix(matrix_array)
|
| 30 |
+
print(f"Using algorithm: {algorithm}")
|
| 31 |
+
|
| 32 |
+
# Format the result message
|
| 33 |
+
result_message = (
|
| 34 |
+
f"Algorithm used: {algorithm}\n"
|
| 35 |
+
f"Maximum Submatrix Sum: {max_sum:.2f}\n"
|
| 36 |
+
f"Time taken: {time_taken:.6f} seconds\n"
|
| 37 |
+
f"Submatrix location: Rows {loc[0].start} to {loc[0].stop-1}, Columns {loc[1].start} to {loc[1].stop-1}"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Create a styled DataFrame with highlighted cells
|
| 41 |
+
df = pd.DataFrame(matrix_array)
|
| 42 |
+
|
| 43 |
+
# Create a mask for the maximum submatrix
|
| 44 |
+
mask = pd.DataFrame(np.zeros_like(matrix_array, dtype=bool))
|
| 45 |
+
mask.iloc[loc[0], loc[1]] = True
|
| 46 |
+
|
| 47 |
+
# Apply background color based on the mask
|
| 48 |
+
def highlight_max_submatrix(val):
|
| 49 |
+
color = 'background-color: #90EE90' # Light green
|
| 50 |
+
default = ''
|
| 51 |
+
return np.where(mask, color, default)
|
| 52 |
+
|
| 53 |
+
# Style the DataFrame with the highlighting
|
| 54 |
+
styled_df = df.style.apply(highlight_max_submatrix, axis=None)
|
| 55 |
+
|
| 56 |
+
# Attempt to render styled DataFrame to HTML using to_html, fallback on string conversion if necessary
|
| 57 |
+
try:
|
| 58 |
+
html_output = styled_df.to_html()
|
| 59 |
+
except Exception as e:
|
| 60 |
+
html_output = str(styled_df)
|
| 61 |
+
return html_output, result_message
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Error in process_matrix: {e}")
|
| 64 |
+
return matrix_df, str(e)
|
| 65 |
+
|
| 66 |
+
# Initialize Gradio interface
|
| 67 |
+
with gr.Blocks(title="Maximum Submatrix Sum Calculator") as app:
|
| 68 |
+
gr.Markdown("# Maximum Submatrix Sum Calculator")
|
| 69 |
+
gr.Markdown("Edit the matrix below or use the random generator, then select an algorithm to find the maximum sum submatrix.")
|
| 70 |
+
|
| 71 |
+
random_matrix_btn = gr.Button("Generate New Random Matrix")
|
| 72 |
+
|
| 73 |
+
# Use a dataframe component for the matrix input/output
|
| 74 |
+
matrix_display = gr.Dataframe(
|
| 75 |
+
value=generate_random_matrix(),
|
| 76 |
+
interactive=True,
|
| 77 |
+
label="Matrix (cells in max submatrix will be highlighted in green)"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
highlighted_matrix = gr.HTML(label="Highlighted Matrix")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
algorithm = gr.Radio(
|
| 84 |
+
["Brute Force", "FFT", "Kidane"],
|
| 85 |
+
value="FFT",
|
| 86 |
+
label="Algorithm"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
submit_btn = gr.Button("Find Maximum Submatrix Sum", variant="primary")
|
| 91 |
+
|
| 92 |
+
result_text = gr.Textbox(label="Results", lines=3)
|
| 93 |
+
|
| 94 |
+
# Event handlers
|
| 95 |
+
random_matrix_btn.click(generate_random_matrix, outputs=[matrix_display])
|
| 96 |
+
submit_btn.click(
|
| 97 |
+
process_matrix,
|
| 98 |
+
inputs=[matrix_display, algorithm],
|
| 99 |
+
outputs=[highlighted_matrix, result_text]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Print a message before launching
|
| 103 |
+
print("Launching Gradio app for Maximum Submatrix Sum Calculator...")
|
| 104 |
+
|
| 105 |
+
# Run the app
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
app.launch(show_error=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
numpy
|
| 2 |
scipy
|
| 3 |
-
pytest
|
|
|
|
|
|
|
|
|
| 1 |
numpy
|
| 2 |
scipy
|
| 3 |
+
pytest
|
| 4 |
+
gradio
|
| 5 |
+
pandas
|