Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
|
| 8 |
+
# Initialize the data
|
| 9 |
+
data = []
|
| 10 |
+
model = RandomForestClassifier(n_estimators=100)
|
| 11 |
+
|
| 12 |
+
def train_model(data):
|
| 13 |
+
# Prepare training data from past records
|
| 14 |
+
if len(data) < 10:
|
| 15 |
+
return "Not enough data to train the model"
|
| 16 |
+
|
| 17 |
+
df = pd.DataFrame(data)
|
| 18 |
+
X = df['card'].apply(lambda x: 0 if x == 'D' else (1 if x == 'T' else 2)).values.reshape(-1, 1)
|
| 19 |
+
y = df['card'].apply(lambda x: 0 if x == 'D' else (1 if x == 'T' else 2))
|
| 20 |
+
|
| 21 |
+
model.fit(X, y)
|
| 22 |
+
return "Model trained successfully"
|
| 23 |
+
|
| 24 |
+
def predict_next_card(data):
|
| 25 |
+
if len(data) < 10:
|
| 26 |
+
return "Not enough data to make a prediction"
|
| 27 |
+
|
| 28 |
+
df = pd.DataFrame(data)
|
| 29 |
+
X = df['card'].apply(lambda x: 0 if x == 'D' else (1 if x == 'T' else 2)).values.reshape(-1, 1)
|
| 30 |
+
prediction = model.predict(X[-1].reshape(1, -1))
|
| 31 |
+
|
| 32 |
+
if prediction == 0:
|
| 33 |
+
return "D"
|
| 34 |
+
elif prediction == 1:
|
| 35 |
+
return "T"
|
| 36 |
+
else:
|
| 37 |
+
return "X"
|
| 38 |
+
|
| 39 |
+
def generate_card_distribution(data):
|
| 40 |
+
df = pd.DataFrame(data)
|
| 41 |
+
distribution = df['card'].value_counts().sort_index()
|
| 42 |
+
|
| 43 |
+
# Create visualization
|
| 44 |
+
plt.figure(figsize=(6, 4))
|
| 45 |
+
sns.barplot(x=distribution.index, y=distribution.values, palette="viridis")
|
| 46 |
+
plt.title('Card Distribution')
|
| 47 |
+
plt.xlabel('Card Type')
|
| 48 |
+
plt.ylabel('Count')
|
| 49 |
+
plt.tight_layout()
|
| 50 |
+
|
| 51 |
+
# Save the plot
|
| 52 |
+
plt.savefig('/tmp/card_distribution.png')
|
| 53 |
+
plt.close()
|
| 54 |
+
return '/tmp/card_distribution.png'
|
| 55 |
+
|
| 56 |
+
def generate_trends_over_time(data):
|
| 57 |
+
df = pd.DataFrame(data)
|
| 58 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
| 59 |
+
|
| 60 |
+
# Group by time and count occurrences
|
| 61 |
+
trend_data = df.groupby(df['timestamp'].dt.hour)['card'].value_counts().unstack().fillna(0)
|
| 62 |
+
|
| 63 |
+
# Create visualization
|
| 64 |
+
plt.figure(figsize=(10, 6))
|
| 65 |
+
sns.lineplot(data=trend_data, palette="tab10", markers=True)
|
| 66 |
+
plt.title('Trends Over Time')
|
| 67 |
+
plt.xlabel('Hour of Day')
|
| 68 |
+
plt.ylabel('Card Frequency')
|
| 69 |
+
plt.tight_layout()
|
| 70 |
+
|
| 71 |
+
# Save the plot
|
| 72 |
+
plt.savefig('/tmp/trends_over_time.png')
|
| 73 |
+
plt.close()
|
| 74 |
+
return '/tmp/trends_over_time.png'
|
| 75 |
+
|
| 76 |
+
def card_prediction_interface(card_input):
|
| 77 |
+
# Automatically generate timestamp
|
| 78 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 79 |
+
|
| 80 |
+
# Process the input card
|
| 81 |
+
if card_input not in ['D', 'T', 'X']:
|
| 82 |
+
return "Invalid card input. Please enter D, T, or X.", "", "", ""
|
| 83 |
+
|
| 84 |
+
# Add the input and timestamp to the dataset
|
| 85 |
+
new_entry = {"card": card_input, "timestamp": timestamp}
|
| 86 |
+
data.append(new_entry)
|
| 87 |
+
|
| 88 |
+
# Train the model
|
| 89 |
+
train_status = train_model(data)
|
| 90 |
+
|
| 91 |
+
# Predict the next card
|
| 92 |
+
prediction = predict_next_card(data)
|
| 93 |
+
|
| 94 |
+
# Generate visualizations
|
| 95 |
+
card_distribution_fig = generate_card_distribution(data)
|
| 96 |
+
trends_fig = generate_trends_over_time(data)
|
| 97 |
+
|
| 98 |
+
return prediction, train_status, card_distribution_fig, trends_fig
|
| 99 |
+
|
| 100 |
+
# Gradio interface
|
| 101 |
+
inputs = gr.Textbox(label="Card (D, T, or X)", placeholder="Enter D, T, or X")
|
| 102 |
+
outputs = [
|
| 103 |
+
gr.Textbox(label="Prediction"),
|
| 104 |
+
gr.Textbox(label="Training Status"),
|
| 105 |
+
gr.Image(label="Card Distribution"),
|
| 106 |
+
gr.Image(label="Trends Over Time")
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
gr.Interface(
|
| 110 |
+
fn=card_prediction_interface,
|
| 111 |
+
inputs=inputs,
|
| 112 |
+
outputs=outputs,
|
| 113 |
+
title="Enhanced Card Prediction Bot",
|
| 114 |
+
description="An advanced bot that predicts the next card based on historical data and visualizes trends.",
|
| 115 |
+
).launch()
|