TaxDirection / app.py
SantanuBanerjee's picture
Update app.py
2bf6af8 verified
raw
history blame
5.01 kB
import gradio as gr
import pandas as pd
def data_pre_processing(file_responses):
# Financial Weights are in per decas and NOT per cents
try:
# Define the columns to be processed
columns = [
'''Your financial allocation for Problem 1:
Mention the percentage of your Tax Amount which you wish the Government would allocate through their annual budget, to implement a specific solution for your 1st problem.''',
'''Your financial allocation for Problem 2:
Mention the percentage of your Tax Amount which you wish the Government would allocate through their annual budget, to implement a solution specifically to your 2nd problem.''',
'''Your financial allocation for Problem 3:
Mention the percentage of your Tax Amount which you wish the Government would allocate through their annual budget, to implement a solution specifically to your 3rd problem.'''
]
# # Convert columns to numeric and fill NaN values with 0
# for col in columns:
# file_responses[col] = pd.to_numeric(file_responses[col], errors='coerce').fillna(0)
# # Calculate the Total Allocation
# file_responses['Total Allocation'] = file_responses[columns].sum(axis=1)
# # Convert the Tax Payment column to numeric
# tax_payment_col = '''How much was your latest Tax payment (in U$D) ?
# Please try to be as accurate as possible:
# Eg.: If your last tax amount was INR 25,785/-; then convert it in U$D and enter only the amount as: 310.
# If you have never paid tax, consider putting in a realistic donation amount which wish to contribute towards helping yourself obtain the desired relief.'''
# file_responses[tax_payment_col] = pd.to_numeric(file_responses[tax_payment_col], errors='coerce').fillna(0)
# # Calculate Financial Token Weights
# for i, col in enumerate(columns, start=1):
# file_responses[f'Financial Token Weight for Problem {i}'] = (
# file_responses[tax_payment_col] * file_responses[col] / file_responses['Total Allocation']
# ).fillna(0)
return file_responses
except Exception as e:
return str(e)
def nlp_pipeline(original_df):
processed_df = data_pre_processing(original_df)
return processed_df
def process_excel(file):
try:
# Ensure the file path is correct
file_path = file.name if hasattr(file, 'name') else file
# Read the Excel file
df = pd.read_excel(file_path)
# Process the DataFrame
result_df = nlp_pipeline(df)
output_file = "OutPut_file.xlsx"
result_df.to_excel(output_file, index=False)
return output_file # Return the processed DataFrame as Excel file
except Exception as e:
return str(e) # Return the error message
example_files = ['#TaxDirection (Responses)_Example1.xlsx', ]
# Define the Gradio interface
interface = gr.Interface(
fn=process_excel, # The function to process the uploaded file
inputs=gr.File(type="filepath", label="Upload Excel File here. \t Be sure to check that the column headings in your upload are the same as in the Example files below. \t (Otherwise there will be Error during the processing)"), # File upload input
examples=example_files, # Add the example files
# outputs=gr.File(label="Download Processed Excel File"), # File download output
outputs=gr.File(label="Download Processed Excel File containing the ** Project Proposals ** for each Location~Problem paired combination"), # File download output
title="Excel File Uploader",
description=(
"<p style='font-weight: bold; font-size: 18px;'>Upload an Excel file to process and download the result "
"or use the example files.\n</p>"
"<p style='font-weight: bold; font-size: 16px;'>\t The processed file will contain the project proposals for each "
"location-problem paired combination.</p>"
) # Solid description with hyperlink
)
# # Launch the interface
# if __name__ == "__main__":
# interface.launch()
# Additional description at the bottom of the page
additional_description = gr.HTML(
"<p style='font-size: 14px; color: gray;'>Note: The example files provided above are for demonstration purposes. "
"Feel free to upload your own Excel files to see the results. If you have any questions, refer to the documentation or "
"contact support.\n</p>"
"<p style='font-weight: bold; font-size: 17px;'>\t For more information, visit "
"<a href='https://santanban.github.io/TaxDirection/' target='_blank'>#TaxDirection weblink</a>.</p>"
)
# Launch the interface with the additional description
demo = gr.Blocks()
with demo:
interface.render()
additional_description.render()
if __name__ == "__main__":
demo.launch()