Create can_be_used_for_file_upload_app.py
Browse files
can_be_used_for_file_upload_app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 6 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 7 |
+
from sklearn.metrics import (
|
| 8 |
+
accuracy_score, precision_score, recall_score, f1_score,
|
| 9 |
+
confusion_matrix, classification_report
|
| 10 |
+
)
|
| 11 |
+
import seaborn as sns
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
|
| 14 |
+
def train_and_evaluate_model(csv_data=None):
|
| 15 |
+
# Step 1: Generate synthetic dataset if no CSV data is provided
|
| 16 |
+
if csv_data is None:
|
| 17 |
+
np.random.seed(42)
|
| 18 |
+
n_records = 10000
|
| 19 |
+
data = {
|
| 20 |
+
'pe_ratio': np.random.uniform(5, 50, n_records),
|
| 21 |
+
'de_ratio': np.random.uniform(0.1, 3.0, n_records),
|
| 22 |
+
'roe': np.random.uniform(5, 40, n_records),
|
| 23 |
+
'market_cap': np.random.uniform(500, 100000, n_records),
|
| 24 |
+
'dividend_yield': np.random.uniform(0.5, 5.0, n_records),
|
| 25 |
+
'stock_rating': np.random.choice(['Buy', 'Hold', 'Sell'], n_records, p=[0.4, 0.4, 0.2])
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
df = pd.DataFrame(data)
|
| 29 |
+
else:
|
| 30 |
+
# Step 1: If CSV data is provided, use it
|
| 31 |
+
df = pd.read_csv(csv_data.name)
|
| 32 |
+
|
| 33 |
+
# Step 2: Prepare data
|
| 34 |
+
X = df.drop('stock_rating', axis=1)
|
| 35 |
+
y = df['stock_rating']
|
| 36 |
+
|
| 37 |
+
# Step 3: Encode target
|
| 38 |
+
le = LabelEncoder()
|
| 39 |
+
y_encoded = le.fit_transform(y)
|
| 40 |
+
|
| 41 |
+
# Step 4: Train/test split
|
| 42 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 43 |
+
X, y_encoded, test_size=0.3, random_state=42, stratify=y_encoded
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Step 5: Feature scaling
|
| 47 |
+
scaler = StandardScaler()
|
| 48 |
+
X_train_scaled = scaler.fit_transform(X_train)
|
| 49 |
+
X_test_scaled = scaler.transform(X_test)
|
| 50 |
+
|
| 51 |
+
# Step 6: Train model
|
| 52 |
+
model = RandomForestClassifier(random_state=42)
|
| 53 |
+
model.fit(X_train_scaled, y_train)
|
| 54 |
+
|
| 55 |
+
# Step 7: Predict
|
| 56 |
+
y_pred = model.predict(X_test_scaled)
|
| 57 |
+
|
| 58 |
+
# Step 8: Decode labels
|
| 59 |
+
y_test_labels = le.inverse_transform(y_test)
|
| 60 |
+
y_pred_labels = le.inverse_transform(y_pred)
|
| 61 |
+
|
| 62 |
+
# Step 9: Metrics
|
| 63 |
+
acc = accuracy_score(y_test_labels, y_pred_labels)
|
| 64 |
+
prec = precision_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
|
| 65 |
+
rec = recall_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
|
| 66 |
+
f1 = f1_score(y_test_labels, y_pred_labels, average='weighted', zero_division=0)
|
| 67 |
+
|
| 68 |
+
# Step 10: Create Classification Report as DataFrame (with zero_division fix)
|
| 69 |
+
report_dict = classification_report(y_test_labels, y_pred_labels, output_dict=True, zero_division=0)
|
| 70 |
+
report_df = pd.DataFrame(report_dict).transpose().round(2)
|
| 71 |
+
|
| 72 |
+
# Step 11: Plot classification report as table with grid
|
| 73 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 74 |
+
ax.axis('off')
|
| 75 |
+
tbl = ax.table(
|
| 76 |
+
cellText=report_df.values,
|
| 77 |
+
colLabels=report_df.columns,
|
| 78 |
+
rowLabels=report_df.index,
|
| 79 |
+
cellLoc='center',
|
| 80 |
+
loc='center'
|
| 81 |
+
)
|
| 82 |
+
tbl.auto_set_font_size(False)
|
| 83 |
+
tbl.set_fontsize(10)
|
| 84 |
+
tbl.scale(1.2, 1.2)
|
| 85 |
+
for key, cell in tbl.get_celld().items():
|
| 86 |
+
cell.set_linewidth(0.8)
|
| 87 |
+
cr_path = "classification_report.png"
|
| 88 |
+
plt.savefig(cr_path, bbox_inches='tight')
|
| 89 |
+
plt.close()
|
| 90 |
+
|
| 91 |
+
# Step 12: Confusion matrix
|
| 92 |
+
cm = confusion_matrix(y_test_labels, y_pred_labels, labels=le.classes_)
|
| 93 |
+
plt.figure(figsize=(6, 5))
|
| 94 |
+
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
|
| 95 |
+
xticklabels=le.classes_, yticklabels=le.classes_)
|
| 96 |
+
plt.xlabel("Predicted")
|
| 97 |
+
plt.ylabel("Actual")
|
| 98 |
+
plt.title("Confusion Matrix")
|
| 99 |
+
cm_path = "confusion_matrix.png"
|
| 100 |
+
plt.savefig(cm_path, bbox_inches='tight')
|
| 101 |
+
plt.close()
|
| 102 |
+
|
| 103 |
+
# Step 13: Return outputs
|
| 104 |
+
output = f"""
|
| 105 |
+
### ✅ Evaluation Metrics:
|
| 106 |
+
- **Accuracy:** {acc:.2f}
|
| 107 |
+
- **Precision:** {prec:.2f}
|
| 108 |
+
- **Recall:** {rec:.2f}
|
| 109 |
+
- **F1 Score:** {f1:.2f}
|
| 110 |
+
"""
|
| 111 |
+
return output, cr_path, cm_path
|
| 112 |
+
|
| 113 |
+
# Gradio Interface
|
| 114 |
+
with gr.Blocks() as demo:
|
| 115 |
+
gr.Markdown("## 🧠 Stock Rating Prediction Model Evaluation")
|
| 116 |
+
gr.Markdown("You can either upload your own CSV file or use synthetic data to evaluate the model.")
|
| 117 |
+
|
| 118 |
+
# CSV file input for user data
|
| 119 |
+
file_input = gr.File(label="Upload CSV File")
|
| 120 |
+
|
| 121 |
+
eval_btn = gr.Button("Run Model Evaluation")
|
| 122 |
+
output_md = gr.Markdown()
|
| 123 |
+
report_img = gr.Image(type="filepath", label="📊 Classification Report")
|
| 124 |
+
cm_img = gr.Image(type="filepath", label="📉 Confusion Matrix")
|
| 125 |
+
|
| 126 |
+
eval_btn.click(fn=train_and_evaluate_model,
|
| 127 |
+
inputs=[file_input],
|
| 128 |
+
outputs=[output_md, report_img, cm_img])
|
| 129 |
+
|
| 130 |
+
demo.launch()
|