File size: 3,288 Bytes
76bfb75
bddf29f
 
2d6a87c
 
61e6b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443053b
 
2d6a87c
 
443053b
bddf29f
e4d07f2
 
 
 
 
443053b
61e6b62
3fca7f2
 
61e6b62
443053b
e4d07f2
 
bddf29f
 
 
 
1a5796d
bddf29f
 
 
76bfb75
 
bddf29f
76bfb75
e4d07f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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)
        
        return result_df  # Return the processed DataFrame
        
    except Exception as e:
        return str(e)  # Return the error message

# 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"),  # File upload input
    outputs="dataframe",  # Display the output as a DataFrame
    title="Excel File Uploader",
    description="Upload an Excel file to see the first few rows."
)

# Launch the interface
if __name__ == "__main__":
    interface.launch()