Spaces:
Sleeping
Sleeping
Chiragkumar Savani
commited on
Commit
·
47b49e7
1
Parent(s):
c74b3e5
Using csv file as input
Browse files
app.py
CHANGED
@@ -1,94 +1,149 @@
|
|
|
|
1 |
import pandas as pd
|
2 |
-
import
|
|
|
|
|
3 |
from openpyxl.styles import PatternFill
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
def
|
8 |
-
"""
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
colored_pairs = set()
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
wb
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
outputs=gr.File(label="Download Processed Excel File"),
|
90 |
-
title="Excel
|
|
|
91 |
)
|
92 |
|
93 |
if __name__ == "__main__":
|
94 |
-
|
|
|
1 |
+
import os
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
from openpyxl import Workbook, load_workbook
|
6 |
from openpyxl.styles import PatternFill
|
7 |
+
import random
|
8 |
|
9 |
+
def generate_random_light_color():
|
10 |
+
min_brightness = 0.7
|
11 |
+
while True:
|
12 |
+
r, g, b = [random.randint(128, 255) for _ in range(3)]
|
13 |
+
brightness = (r * 0.299 + g * 0.587 + b * 0.114) / 255
|
14 |
+
if brightness >= min_brightness:
|
15 |
+
return f'{r:02X}{g:02X}{b:02X}'
|
16 |
|
17 |
+
def calculate_threshold(value, is_high_price=True):
|
18 |
+
"""
|
19 |
+
Determine the threshold based on the provided ranges.
|
20 |
+
The function checks if it's for High Price or Low Price and returns the respective threshold.
|
21 |
+
"""
|
22 |
+
if 0 <= value <= 200:
|
23 |
+
return 0.20
|
24 |
+
elif 201 <= value <= 500:
|
25 |
+
return 0.70
|
26 |
+
elif 501 <= value <= 1000:
|
27 |
+
return 1.0
|
28 |
+
elif 1001 <= value <= 2000:
|
29 |
+
return 2.0
|
30 |
+
elif 2001 <= value <= 3000:
|
31 |
+
return 3.0
|
32 |
+
elif 3001 <= value <= 4000:
|
33 |
+
return 4.0
|
34 |
+
elif 4001 <= value <= 5000:
|
35 |
+
return 5.0
|
36 |
+
else:
|
37 |
+
return 5.0
|
38 |
+
|
39 |
+
def process_section(ws, start_row, end_row, col_index1, col_index2, output_col_index1, output_col_index2, high_threshold, low_threshold):
|
40 |
colored_pairs = set()
|
41 |
+
|
42 |
+
# Process first column
|
43 |
+
for i in range(end_row, start_row - 1, -1):
|
44 |
+
for j in range(i - 1, start_row - 1, -1):
|
45 |
+
cell_value_i = ws.cell(i, col_index1).value
|
46 |
+
cell_value_j = ws.cell(j, col_index1).value
|
47 |
+
if not (isinstance(cell_value_i, (int, float)) and isinstance(cell_value_j, (int, float))):
|
48 |
+
continue
|
49 |
+
if abs(cell_value_i - cell_value_j) <= high_threshold:
|
50 |
+
if (i, col_index1) not in colored_pairs and (j, col_index1) not in colored_pairs:
|
51 |
+
color = generate_random_light_color()
|
52 |
+
fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
53 |
+
ws.cell(i, col_index1).fill = fill
|
54 |
+
ws.cell(j, col_index1).fill = fill
|
55 |
+
colored_pairs.add((i, col_index1))
|
56 |
+
colored_pairs.add((j, col_index1))
|
57 |
+
|
58 |
+
output_value1 = max(int(cell_value_i), int(cell_value_j)) + 1
|
59 |
+
ws.cell(j, output_col_index1).value = output_value1
|
60 |
+
|
61 |
+
for k in range(j - 1, start_row - 1, -1):
|
62 |
+
cell_value_k = ws.cell(k, col_index1).value
|
63 |
+
if isinstance(cell_value_k, (int, float)) and cell_value_k > output_value1:
|
64 |
+
if ws.cell(k, 6).value > max(cell_value_i, cell_value_j):
|
65 |
+
ws.cell(j, output_col_index1).fill = fill
|
66 |
+
break
|
67 |
+
|
68 |
+
# Process second column
|
69 |
+
for i in range(end_row, start_row - 1, -1):
|
70 |
+
for j in range(i - 1, start_row - 1, -1):
|
71 |
+
cell_value_i = ws.cell(i, col_index2).value
|
72 |
+
cell_value_j = ws.cell(j, col_index2).value
|
73 |
+
if not (isinstance(cell_value_i, (int, float)) and isinstance(cell_value_j, (int, float))):
|
74 |
+
continue
|
75 |
+
if abs(cell_value_i - cell_value_j) <= low_threshold:
|
76 |
+
if (i, col_index2) not in colored_pairs and (j, col_index2) not in colored_pairs:
|
77 |
+
color = generate_random_light_color()
|
78 |
+
fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
79 |
+
ws.cell(i, col_index2).fill = fill
|
80 |
+
ws.cell(j, col_index2).fill = fill
|
81 |
+
colored_pairs.add((i, col_index2))
|
82 |
+
colored_pairs.add((j, col_index2))
|
83 |
+
|
84 |
+
output_value2 = min(int(cell_value_i), int(cell_value_j)) - 1
|
85 |
+
ws.cell(j, output_col_index2).value = output_value2
|
86 |
+
|
87 |
+
for k in range(j - 1, start_row - 1, -1):
|
88 |
+
cell_value_k = ws.cell(k, col_index2).value
|
89 |
+
if isinstance(cell_value_k, (int, float)) and cell_value_k < output_value2:
|
90 |
+
if ws.cell(k, 6).value < min(cell_value_i, cell_value_j):
|
91 |
+
ws.cell(j, output_col_index2).fill = fill
|
92 |
+
break
|
93 |
+
|
94 |
+
def highlight_pairs_with_rgb(input_file):
|
95 |
+
df = pd.read_csv(input_file)
|
96 |
+
df.columns = df.columns.str.strip()
|
97 |
+
|
98 |
+
# Convert to Excel and add three empty columns
|
99 |
+
excel_filename = input_file.replace(".csv", ".xlsx")
|
100 |
+
with pd.ExcelWriter(excel_filename, engine='openpyxl') as writer:
|
101 |
+
# Reorder and create Excel file with empty columns
|
102 |
+
df_new = pd.concat([df.iloc[:, :8], pd.DataFrame(columns=['', '', '']), df.iloc[:, 8:]], axis=1)
|
103 |
+
df_new.to_excel(writer, index=False, sheet_name="Sheet1")
|
104 |
|
105 |
+
wb = load_workbook(excel_filename)
|
106 |
+
ws = wb.active
|
107 |
+
|
108 |
+
# Determine thresholds for High Price (Column 6) and Low Price (Column 7)
|
109 |
+
high_price_col = df["HIGH PRICE"].dropna()
|
110 |
+
low_price_col = df["LOW PRICE"].dropna()
|
111 |
+
|
112 |
+
high_threshold = calculate_threshold(high_price_col.max(), is_high_price=True)
|
113 |
+
low_threshold = calculate_threshold(low_price_col.min(), is_high_price=False)
|
114 |
+
|
115 |
+
last_row = ws.max_row
|
116 |
+
col_index1, col_index2 = 6, 7 # HIGH PRICE and LOW PRICE columns in Excel
|
117 |
+
output_col_index1, output_col_index2 = 9, 10 # Empty columns added earlier
|
118 |
+
|
119 |
+
ws.cell(1, output_col_index1).value = "High Result"
|
120 |
+
ws.cell(1, output_col_index2).value = "Low Result"
|
121 |
+
|
122 |
+
start_row = 2
|
123 |
+
while start_row <= last_row:
|
124 |
+
end_row = start_row
|
125 |
+
while end_row <= last_row and ws.cell(end_row, 1).value is not None:
|
126 |
+
end_row += 1
|
127 |
+
end_row -= 1
|
128 |
+
|
129 |
+
process_section(ws, start_row, end_row, col_index1, col_index2, output_col_index1, output_col_index2, high_threshold, low_threshold)
|
130 |
+
start_row = end_row + 2
|
131 |
+
|
132 |
+
wb.save(excel_filename)
|
133 |
+
return excel_filename
|
134 |
+
|
135 |
+
def gradio_interface(input_file):
|
136 |
+
output_file = highlight_pairs_with_rgb(input_file.name)
|
137 |
+
return output_file
|
138 |
+
|
139 |
+
# Gradio app interface
|
140 |
+
iface = gr.Interface(
|
141 |
+
fn=gradio_interface,
|
142 |
+
inputs=gr.File(label="Upload CSV File (.csv)", file_count="single"),
|
143 |
outputs=gr.File(label="Download Processed Excel File"),
|
144 |
+
title="CSV to Excel Processor with Cell Highlighting",
|
145 |
+
description="Upload a CSV file with stock data, and download a processed Excel file with highlighted cells."
|
146 |
)
|
147 |
|
148 |
if __name__ == "__main__":
|
149 |
+
iface.launch()
|