-
Notifications
You must be signed in to change notification settings - Fork 0
/
python
130 lines (106 loc) · 3.24 KB
/
python
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load data from database
data = pd.read_csv('user_data.csv') # Assume you've exported the data to a CSV
# Data preprocessing
# Example: Assuming we are predicting user engagement based on user data
features = data.drop('engagement', axis=1)
target = data['engagement']
# Standardizing the features
scaler = StandardScaler()
features_scaled = scaler.fit_transform(features)
# Splitting the data
X_train, X_test, y_train, y_test = train_test_split(features_scaled, target, test_size=0.2, random_state=42)
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Define the model
model = Sequential([
Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
Dense(64, activation='relu'),
Dense(1, activation='sigmoid') # Assuming binary classification
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2)
# Evaluate the model
model.evaluate(X_test, y_test)
# Save the model
model.save('user_engagement_model.h5')
from fastapi import FastAPI, Request
import tensorflow as tf
import numpy as np
from pydantic import BaseModel
# Load the model
model = tf.keras.models.load_model('user_engagement_model.h5')
# Initialize FastAPI app
app = FastAPI()
class UserData(BaseModel):
feature1: float
feature2: float
# Add other features as per your dataset
@app.post("/predict")
async def predict(data: UserData):
input_data = np.array([[data.feature1, data.feature2, ...]]) # Add all features
prediction = model.predict(input_data)
return {"prediction": prediction[0][0]}
# Run the API with `uvicorn app:app --reload`
import { useState } from 'react';
import axios from 'axios';
const Search = () => {
const [query, setQuery] = useState('');
const [results, setResults] = useState([]);
const handleSearch = async () => {
const response = await axios.post('http://localhost:8000/predict', {
feature1: value1,
feature2: value2,
// Add other features
});
setResults(response.data.prediction);
};
return (
<div>
<input
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Search"
/>
<button onClick={handleSearch}>Search</button>
<div>
{results && <p>Prediction: {results}</p>}
</div>
</div>
);
};
export default Search;
in pages/visualization.js
import { useEffect, useState } from 'react';
import { Bar } from 'react-chartjs-2';
const Visualization = () => {
const [data, setData] = useState({});
useEffect(() => {
const fetchData = async () => {
const response = await axios.get('http://localhost:8000/data');
setData(response.data);
};
fetchData();
}, []);
const chartData = {
labels: data.labels,
datasets: [
{
label: 'User Engagement',
data: data.values,
backgroundColor: 'rgba(75, 192, 192, 0.6)',
},
],
};
return (
<div>
<Bar data={chartData} />
</div>
);
};
export default Visualization;