Spaces:
Sleeping
Sleeping
Download app.py from FuurythumbsCPT/FD_CSV: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/spaces/FuurythumbsCPT/FD_CSV/resolve/main/app.py
- Command line
-
hf download hf://spaces/FuurythumbsCPT/FD_CSV/app.py
-
curl -L -o app.py https://huggingface.co/spaces/FuurythumbsCPT/FD_CSV/resolve/main/app.py
3 kB
| '''import gradio as gr | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.ensemble import RandomForestClassifier | |
| import joblib | |
| # Load training data | |
| data = pd.read_csv("fraud_data.csv") | |
| # Preprocessing | |
| X = data.drop("is_fraud", axis=1) | |
| y = data["is_fraud"] | |
| X = pd.get_dummies(X) | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Train model | |
| model = RandomForestClassifier() | |
| model.fit(X_train, y_train) | |
| # Save model and columns | |
| joblib.dump(model, "model.pkl") | |
| joblib.dump(X.columns, "columns.pkl") | |
| # Load model and columns | |
| model = joblib.load("model.pkl") | |
| columns = joblib.load("columns.pkl") | |
| def predict(file): | |
| df = pd.read_csv(file.name) | |
| input_df = pd.get_dummies(df) | |
| input_df = input_df.reindex(columns=columns, fill_value=0) | |
| preds = model.predict(input_df) | |
| df["prediction"] = preds | |
| return df | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.File(label="Upload a CSV File with Transactions"), | |
| outputs="dataframe", | |
| title="Fraud Detection with CSV Upload", | |
| description="**Upload a CSV file** with columns like **'amount', 'country', 'device', 'transaction_time'**. The model will predict fraud (**1 = fraud**, **0 = not fraud**)." | |
| ) | |
| iface.launch()''' | |
| import gradio as gr | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.ensemble import RandomForestClassifier | |
| import joblib | |
| # Load training data | |
| data = pd.read_csv("fraud_data.csv") | |
| # Preprocessing | |
| X = data.drop("is_fraud", axis=1) | |
| y = data["is_fraud"] | |
| X = pd.get_dummies(X) | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Train model | |
| model = RandomForestClassifier() | |
| model.fit(X_train, y_train) | |
| # Save model and columns | |
| joblib.dump(model, "model.pkl") | |
| joblib.dump(X.columns, "columns.pkl") | |
| # Load model and columns | |
| model = joblib.load("model.pkl") | |
| columns = joblib.load("columns.pkl") | |
| def predict(file): | |
| df = pd.read_csv(file.name) | |
| input_df = pd.get_dummies(df) | |
| input_df = input_df.reindex(columns=columns, fill_value=0) | |
| preds = model.predict(input_df) | |
| df["prediction"] = preds | |
| return df | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Fraud Detection with CSV Upload") | |
| gr.Markdown("**Upload a CSV file** with columns like **'amount', 'country', 'device', 'transaction_time'**. The model will predict fraud (**1 = fraud**, **0 = not fraud**).") | |
| file_input = gr.File(label="Upload a CSV File with Transactions") | |
| output_df = gr.Dataframe() | |
| submit = gr.Button("Predict Fraud") | |
| submit.click(fn=predict, inputs=file_input, outputs=output_df) | |
| # Embed Dialogflow bot using raw HTML | |
| gr.HTML(""" | |
| <df-messenger | |
| intent="WELCOME" | |
| chat-title="AI_teacher" | |
| agent-id="24215fc6-555e-4ebe-88b0-130a096887ac" | |
| language-code="en" | |
| ></df-messenger> | |
| <script src="https://www.gstatic.com/dialogflow-console/fast/messenger/bootstrap.js?v=1"></script> | |
| """) | |
| demo.launch() |