File size: 1,504 Bytes
21914e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
 
def trend_analysis(df, target_column):
    # Handle categorical variables
    categorical_columns = ['country', 'public/pro', 'language']
    df = pd.get_dummies(df, columns=categorical_columns)
 
    # Split the data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(df.drop(target_column, axis=1), df[target_column], test_size=0.2, random_state=42)
 
    # Train the model
    model = LinearRegression()
    model.fit(X_train, y_train)
 
    # Use the model to make predictions
    df['prediction'] = model.predict(df.drop(target_column, axis=1))
 
    return df
 
def process_csv(file_path):
    try:
        # Read CSV file
        df = pd.read_csv(file_path, error_bad_lines=False)
 
        # Perform trend analysis
        target_column = 'unit price'  # Replace with the name of your target column
        df = trend_analysis(df, target_column)
 
        # Save the processed DataFrame to a new CSV file (optional)
        df.to_csv("processed_data.csv", index=False)
 
        return "Processing completed. Check 'processed_data.csv' for results."
    except Exception as e:
        return f"Error: {str(e)}"
 
iface = gr.Interface(
    fn=process_csv,
    inputs=gr.File(),
    outputs=gr.Textbox(),
    live=True,
)
 
if __name__ == '__main__':
    iface.launch()