Erenuo commited on
Commit
bd6bce1
·
1 Parent(s): 2f08b28

Upload initial application files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ saved_models/*.joblib filter=lfs diff=lfs merge=lfs -text
37
+ saved_models/label_encoder.joblib filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ real_estate_intent_dataset.csv
2
+
3
+ venv/
4
+
5
+ __pycache__/
6
+
7
+ evaluation_results/
8
+
client.html ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Intent Recognition Client</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <link rel="preconnect" href="https://fonts.googleapis.com">
9
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
10
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
11
+ <style>
12
+ body {
13
+ font-family: 'Inter', sans-serif;
14
+ }
15
+ .loader {
16
+ border-top-color: #3498db;
17
+ animation: spin 1s linear infinite;
18
+ }
19
+ @keyframes spin {
20
+ to { transform: rotate(360deg); }
21
+ }
22
+ </style>
23
+ </head>
24
+ <body class="bg-gray-100 flex items-center justify-center min-h-screen">
25
+
26
+ <div class="bg-white p-8 rounded-2xl shadow-lg w-full max-w-2xl mx-4">
27
+ <div class="text-center mb-6">
28
+ <h1 class="text-3xl font-bold text-gray-800">Intent Recognition API Client</h1>
29
+ <p class="text-gray-500 mt-2">Enter a sentence to get predictions from the top 3 models.</p>
30
+ </div>
31
+
32
+ <div>
33
+ <label for="sentence" class="block text-sm font-medium text-gray-700 mb-2">Your Sentence:</label>
34
+ <textarea id="sentence" rows="4" class="w-full p-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition" placeholder="e.g., 'Show me 3-bedroom houses in Manchester with a garden'"></textarea>
35
+ </div>
36
+
37
+ <div class="mt-6">
38
+ <button id="predictBtn" class="w-full bg-indigo-600 text-white font-bold py-3 px-4 rounded-lg hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-transform transform hover:scale-105">
39
+ Recognize Intent
40
+ </button>
41
+ </div>
42
+
43
+ <!-- Loading Spinner -->
44
+ <div id="loader" class="hidden mt-6 flex justify-center items-center">
45
+ <div class="loader ease-linear rounded-full border-4 border-t-4 border-gray-200 h-12 w-12"></div>
46
+ </div>
47
+
48
+ <!-- Error Message -->
49
+ <div id="error" class="hidden mt-6 p-4 bg-red-100 text-red-700 rounded-lg"></div>
50
+
51
+
52
+ <!-- Results Section -->
53
+ <div id="results" class="hidden mt-8 border-t pt-6">
54
+ <h2 class="text-2xl font-semibold text-gray-800 mb-4">Predictions</h2>
55
+ <div class="space-y-4">
56
+ <div class="p-4 bg-gray-50 rounded-lg border border-gray-200">
57
+ <h3 class="font-bold text-lg text-indigo-700">🥇 SVM Model</h3>
58
+ <p id="svmResult" class="text-gray-800 text-lg mt-1"></p>
59
+ </div>
60
+ <div class="p-4 bg-gray-50 rounded-lg border border-gray-200">
61
+ <h3 class="font-bold text-lg text-teal-700">🥈 XGBoost Model</h3>
62
+ <p id="xgbResult" class="text-gray-800 text-lg mt-1"></p>
63
+ </div>
64
+ <div class="p-4 bg-gray-50 rounded-lg border border-gray-200">
65
+ <h3 class="font-bold text-lg text-sky-700">🥉 Logistic Regression Model</h3>
66
+ <p id="logregResult" class="text-gray-800 text-lg mt-1"></p>
67
+ </div>
68
+ </div>
69
+ </div>
70
+
71
+ </div>
72
+
73
+ <script>
74
+ const predictBtn = document.getElementById('predictBtn');
75
+ const sentenceInput = document.getElementById('sentence');
76
+ const resultsDiv = document.getElementById('results');
77
+ const svmResultEl = document.getElementById('svmResult');
78
+ const xgbResultEl = document.getElementById('xgbResult');
79
+ const logregResultEl = document.getElementById('logregResult');
80
+ const loader = document.getElementById('loader');
81
+ const errorDiv = document.getElementById('error');
82
+
83
+ predictBtn.addEventListener('click', async () => {
84
+ const text = sentenceInput.value.trim();
85
+ if (!text) {
86
+ alert('Please enter a sentence.');
87
+ return;
88
+ }
89
+
90
+ // Reset UI
91
+ resultsDiv.classList.add('hidden');
92
+ errorDiv.classList.add('hidden');
93
+ loader.classList.remove('hidden');
94
+ predictBtn.disabled = true;
95
+
96
+ try {
97
+ const response = await fetch('http://127.0.0.1:8000/predict', {
98
+ method: 'POST',
99
+ headers: {
100
+ 'Content-Type': 'application/json'
101
+ },
102
+ body: JSON.stringify({ text: text })
103
+ });
104
+
105
+ if (!response.ok) {
106
+ throw new Error(`API Error: ${response.status} ${response.statusText}`);
107
+ }
108
+
109
+ const data = await response.json();
110
+
111
+ // Display results
112
+ svmResultEl.textContent = data.predictions.svm_prediction;
113
+ xgbResultEl.textContent = data.predictions.xgboost_prediction;
114
+ logregResultEl.textContent = data.predictions.logistic_regression_prediction;
115
+ resultsDiv.classList.remove('hidden');
116
+
117
+ } catch (error) {
118
+ console.error('Fetch error:', error);
119
+ errorDiv.textContent = `Failed to connect to the API. Make sure the FastAPI server is running. Error: ${error.message}`;
120
+ errorDiv.classList.remove('hidden');
121
+ } finally {
122
+ // Re-enable UI
123
+ loader.classList.add('hidden');
124
+ predictBtn.disabled = false;
125
+ }
126
+ });
127
+ </script>
128
+
129
+ </body>
130
+ </html>
create_intent_dataset.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import os
3
+
4
+ def create_intent_dataset_from_folder(folder_path, output_csv_filename):
5
+ """
6
+ Reads all .txt files from a specified folder, uses their filenames as
7
+ intent labels, and compiles the contents into a single CSV file.
8
+ """
9
+ all_data = []
10
+
11
+ print(f"Starting to process files from folder: '{folder_path}'...")
12
+
13
+ if not os.path.isdir(folder_path):
14
+ print(f"Error: Folder '{folder_path}' not found. Please make sure the folder exists.")
15
+ return
16
+
17
+ # Loop through each file in the specified directory
18
+ for filename in os.listdir(folder_path):
19
+ # Process only text files
20
+ if filename.endswith(".txt"):
21
+ file_path = os.path.join(folder_path, filename)
22
+ try:
23
+ # Extract the intent from the filename
24
+ # os.path.splitext splits the filename and extension, we take the first part
25
+ intent_label = os.path.splitext(filename)[0]
26
+ print(f"Processing file: '{filename}' -> Intent: '{intent_label}'")
27
+
28
+ # Read each line from the file
29
+ with open(file_path, 'r', encoding='utf-8') as f:
30
+ utterances = f.readlines()
31
+
32
+ # Process each line (utterance) in the file
33
+ for utterance in utterances:
34
+ # Clean up the line by removing leading/trailing whitespace
35
+ cleaned_utterance = utterance.strip()
36
+
37
+ # Ensure the line is not empty before adding it
38
+ if cleaned_utterance:
39
+ # Append the data as a dictionary to list
40
+ all_data.append({
41
+ 'utterance': cleaned_utterance,
42
+ 'intent': intent_label
43
+ })
44
+ except Exception as e:
45
+ print(f"An error occurred while processing '{filename}': {e}")
46
+
47
+ # Convert the list of dictionaries to a pandas DataFrame
48
+ if not all_data:
49
+ print("No data was processed. The output file will not be created.")
50
+ return
51
+
52
+ df = pd.DataFrame(all_data)
53
+
54
+ # Save the DataFrame to a CSV file
55
+ try:
56
+ df.to_csv(output_csv_filename, index=False)
57
+ print(f"\nSuccessfully created dataset with {len(df)} records.")
58
+ print(f"Data saved to '{output_csv_filename}'")
59
+ except Exception as e:
60
+ print(f"An error occurred while saving the CSV file: {e}")
61
+
62
+
63
+ if __name__ == "__main__":
64
+ intents_folder = "Intents"
65
+ output_filename = 'real_estate_intent_dataset.csv'
66
+ create_intent_dataset_from_folder(intents_folder, output_filename)
deploy_requirements.txt ADDED
Binary file (360 Bytes). View file
 
evaluate_models.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
2
+ from transformers import BertTokenizer, BertModel
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ import pandas as pd
6
+ import numpy as np
7
+ import joblib
8
+ import torch
9
+ import nltk
10
+ import os
11
+
12
+ TEST_DATA_PATH = 'test.csv'
13
+ MODELS_DIR = 'saved_models'
14
+ RESULTS_DIR = 'evaluation_results'
15
+
16
+ # Helper Function for Encoders
17
+ def get_bert_embeddings(texts, model, tokenizer):
18
+ """Generates BERT embeddings for a list of texts."""
19
+ model.eval() # Put model in evaluation mode
20
+ embeddings = []
21
+ for text in texts:
22
+ inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)
23
+ with torch.no_grad():
24
+ outputs = model(**inputs)
25
+ # Use the mean of the last hidden state as the sentence embedding
26
+ embeddings.append(outputs.last_hidden_state.mean(dim=1).squeeze().numpy())
27
+ return np.array(embeddings)
28
+
29
+
30
+ def evaluate_all_models():
31
+ """
32
+ Loads all trained models and evaluates them against the test dataset.
33
+ """
34
+ # Create results directory
35
+ if not os.path.exists(RESULTS_DIR):
36
+ os.makedirs(RESULTS_DIR)
37
+ print(f"Created directory: {RESULTS_DIR}")
38
+
39
+ # Load test data
40
+ try:
41
+ test_df = pd.read_csv(TEST_DATA_PATH)
42
+ print(f"\nLoaded test data from '{TEST_DATA_PATH}' ({len(test_df)} rows)")
43
+ except FileNotFoundError:
44
+ print(f"Error: Test data not found at '{TEST_DATA_PATH}'")
45
+ print("Please run the 'split_dataset.py' script first.")
46
+ return
47
+
48
+ # Find all model files and load the single label encoder
49
+ model_files = [f for f in os.listdir(MODELS_DIR) if f.endswith('_model.joblib')]
50
+ if not model_files:
51
+ print(f"Error: No models found in '{MODELS_DIR}' directory.")
52
+ return
53
+
54
+ print(f"Found {len(model_files)} models to evaluate.")
55
+
56
+ try:
57
+ # Load the single, shared label encoder
58
+ le_path = os.path.join(MODELS_DIR, 'label_encoder.joblib')
59
+ le = joblib.load(le_path)
60
+ print(f"Successfully loaded shared label encoder from '{le_path}'")
61
+ except FileNotFoundError:
62
+ print(f"Error: The shared 'label_encoder.joblib' was not found in '{MODELS_DIR}'.")
63
+ print("Please ensure at least one training script has been run to create it.")
64
+ return
65
+
66
+ # Pre-load BERT model and tokenizer to avoid loading them in the loop
67
+ print("\nLoading BERT model (this may take a moment)...")
68
+ bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
69
+ bert_model = BertModel.from_pretrained('bert-base-uncased')
70
+ nltk.download('punkt', quiet=True)
71
+
72
+
73
+ # Loop through and evaluate each model
74
+ for model_file in sorted(model_files):
75
+ model_name = model_file.replace('_model.joblib', '')
76
+ print(f"\n--- Evaluating Model: {model_name} ---")
77
+
78
+ try:
79
+ # Load the model
80
+ model = joblib.load(os.path.join(MODELS_DIR, model_file))
81
+
82
+ # Encode the true labels from the test set using the shared encoder
83
+ y_true = le.transform(test_df['intent'])
84
+ X_test_text = test_df['utterance']
85
+
86
+ # Preprocess test data based on model type
87
+ if 'char_tfidf' in model_name:
88
+ vectorizer_path = os.path.join(MODELS_DIR, 'char_tfidf_vectorizer.joblib')
89
+ vectorizer = joblib.load(vectorizer_path)
90
+ X_test = vectorizer.transform(X_test_text)
91
+
92
+ elif 'tfidf' in model_name:
93
+ vectorizer_path = os.path.join(MODELS_DIR, 'tfidf_vectorizer.joblib')
94
+ vectorizer = joblib.load(vectorizer_path)
95
+ X_test = vectorizer.transform(X_test_text)
96
+
97
+ elif 'bert' in model_name:
98
+ X_test = get_bert_embeddings(X_test_text.tolist(), bert_model, bert_tokenizer)
99
+
100
+ else:
101
+ print(f"Warning: Unknown model type for {model_name}. Skipping.")
102
+ continue
103
+
104
+ # Make predictions
105
+ y_pred = model.predict(X_test)
106
+
107
+ # Generate and print classification report
108
+ print("Classification Report:")
109
+ report = classification_report(y_true, y_pred, target_names=le.classes_)
110
+ print(report)
111
+
112
+ # Generate and save confusion matrix
113
+ cm = confusion_matrix(y_true, y_pred)
114
+ plt.figure(figsize=(10, 8))
115
+ sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=le.classes_, yticklabels=le.classes_)
116
+ plt.title(f'Confusion Matrix - {model_name}')
117
+ plt.ylabel('Actual Intent')
118
+ plt.xlabel('Predicted Intent')
119
+ plt.xticks(rotation=45, ha='right')
120
+ plt.yticks(rotation=0)
121
+ plt.tight_layout()
122
+ cm_path = os.path.join(RESULTS_DIR, f'{model_name}_confusion_matrix.png')
123
+ plt.savefig(cm_path)
124
+ plt.close()
125
+ print(f"Confusion matrix saved to: {cm_path}")
126
+
127
+ except FileNotFoundError as e:
128
+ print(f"Error loading files for {model_name}: {e}. Skipping.")
129
+ except Exception as e:
130
+ print(f"An unexpected error occurred while evaluating {model_name}: {e}. Skipping.")
131
+
132
+
133
+ if __name__ == '__main__':
134
+ evaluate_all_models()
predict_intent.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import torch
3
+ import numpy as np
4
+ from transformers import BertTokenizer, BertModel
5
+ import os
6
+ import warnings
7
+
8
+ # Suppress warnings from XGBoost and other libraries for a cleaner output
9
+ warnings.filterwarnings('ignore', category=UserWarning)
10
+ warnings.filterwarnings('ignore', category=FutureWarning)
11
+
12
+ # --- Configuration ---
13
+ MODELS_DIR = 'saved_models'
14
+ SVM_MODEL_PATH = os.path.join(MODELS_DIR, 'bert_svm_model.joblib')
15
+ XGB_MODEL_PATH = os.path.join(MODELS_DIR, 'bert_xgboost_model.joblib')
16
+ LE_PATH = os.path.join(MODELS_DIR, 'label_encoder.joblib')
17
+
18
+ # --- Helper Function for BERT Embedding ---
19
+ def get_bert_embedding(text, model, tokenizer):
20
+ """Generates a BERT embedding for a single piece of text."""
21
+ # Ensure the model is in evaluation mode
22
+ model.eval()
23
+
24
+ # Tokenize the text and create tensors
25
+ inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)
26
+
27
+ # Get the model output without calculating gradients
28
+ with torch.no_grad():
29
+ outputs = model(**inputs)
30
+
31
+ # Use the mean of the last hidden state as the sentence embedding
32
+ embedding = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
33
+
34
+ # Reshape for a single prediction (required by sklearn models)
35
+ return embedding.reshape(1, -1)
36
+
37
+ # --- Main Application ---
38
+ def run_intent_recognizer():
39
+ """
40
+ Loads all necessary components and runs the interactive prediction loop.
41
+ """
42
+ # 1. Load all required files
43
+ print("Loading models and encoders... This may take a moment.")
44
+ try:
45
+ model_svm = joblib.load(SVM_MODEL_PATH)
46
+ model_xgb = joblib.load(XGB_MODEL_PATH)
47
+ label_encoder = joblib.load(LE_PATH)
48
+
49
+ # Load BERT components
50
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
51
+ bert_model = BertModel.from_pretrained('bert-base-uncased')
52
+
53
+ print("All models loaded successfully! 🚀")
54
+ except FileNotFoundError as e:
55
+ print(f"\nError: A required file was not found: {e}")
56
+ print("Please make sure you have run all the necessary training scripts and that the following files exist:")
57
+ print(f"- {SVM_MODEL_PATH}")
58
+ print(f"- {XGB_MODEL_PATH}")
59
+ print(f"- {LE_PATH}")
60
+ return
61
+
62
+ # 2. Start the interactive prediction loop
63
+ print("\nEnter a sentence to recognize its intent. Type 'exit' to close.")
64
+ while True:
65
+ sentence = input("\nYour sentence: ")
66
+ if sentence.lower() == 'exit':
67
+ print("Goodbye! 👋")
68
+ break
69
+ if not sentence.strip():
70
+ print("Please enter a valid sentence.")
71
+ continue
72
+
73
+ # Generate embedding for the input sentence
74
+ embedding = get_bert_embedding(sentence, bert_model, tokenizer)
75
+
76
+ # --- SVM Prediction ---
77
+ pred_svm_idx = model_svm.predict(embedding)[0]
78
+ pred_svm_label = label_encoder.inverse_transform([pred_svm_idx])[0]
79
+ # Get confidence scores (probabilities)
80
+ # For SVM, we need to enable probability=True during training for this,
81
+ # but we can use decision_function as a proxy for confidence.
82
+ # For simplicity, we'll just show the prediction. If probabilities are needed,
83
+ # the SVM model needs to be retrained with probability=True.
84
+ # For now, we get probabilities if available.
85
+ try:
86
+ confidence_svm = np.max(model_svm.predict_proba(embedding)) * 100
87
+ print(f"\n🤖 SVM Model Prediction:")
88
+ print(f" Intent: {pred_svm_label}")
89
+ print(f" Confidence: {confidence_svm:.2f}%")
90
+ except AttributeError:
91
+ print(f"\n🤖 SVM Model Prediction:")
92
+ print(f" Intent: {pred_svm_label}")
93
+ print(f" (Confidence scores not available for this SVM model)")
94
+
95
+
96
+ # --- XGBoost Prediction ---
97
+ pred_xgb_idx = model_xgb.predict(embedding)[0]
98
+ pred_xgb_label = label_encoder.inverse_transform([pred_xgb_idx])[0]
99
+ confidence_xgb = np.max(model_xgb.predict_proba(embedding)) * 100
100
+
101
+ print(f"\n🚀 XGBoost Model Prediction:")
102
+ print(f" Intent: {pred_xgb_label}")
103
+ print(f" Confidence: {confidence_xgb:.2f}%")
104
+ print("-" * 30)
105
+
106
+
107
+ if __name__ == '__main__':
108
+ run_intent_recognizer()
prediction_api.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi.middleware.cors import CORSMiddleware
2
+ from transformers import BertTokenizer, BertModel
3
+ from pydantic import BaseModel
4
+ from fastapi import FastAPI
5
+ import warnings
6
+ import uvicorn
7
+ import joblib
8
+ import torch
9
+ import os
10
+
11
+ # --- Configuration ---
12
+ # Suppress warnings for a cleaner output
13
+ warnings.filterwarnings('ignore', category=UserWarning)
14
+ warnings.filterwarnings('ignore', category=FutureWarning)
15
+
16
+ # Define paths to saved models and encoders
17
+ MODELS_DIR = 'saved_models'
18
+ SVM_MODEL_PATH = os.path.join(MODELS_DIR, 'bert_svm_model.joblib')
19
+ XGB_MODEL_PATH = os.path.join(MODELS_DIR, 'bert_xgboost_model.joblib')
20
+ LOGREG_MODEL_PATH = os.path.join(MODELS_DIR, 'bert_logreg_model.joblib')
21
+ LE_PATH = os.path.join(MODELS_DIR, 'label_encoder.joblib')
22
+
23
+ # --- Pydantic Model for Request Body ---
24
+ class IntentRequest(BaseModel):
25
+ text: str
26
+
27
+ # --- FastAPI App Initialization ---
28
+ app = FastAPI(
29
+ title="Real Estate Intent Recognition API",
30
+ description="An API to predict the intent of a sentence using three different machine learning models.",
31
+ version="1.0.0"
32
+ )
33
+
34
+ # --- Add CORS Middleware ---
35
+ # This is the new section that fixes the error.
36
+ # It allows web pages from any origin to connect to this API.
37
+ app.add_middleware(
38
+ CORSMiddleware,
39
+ allow_origins=["*"], # Allows all origins
40
+ allow_credentials=True,
41
+ allow_methods=["*"], # Allows all methods (GET, POST, etc.)
42
+ allow_headers=["*"], # Allows all headers
43
+ )
44
+
45
+
46
+ # --- Global variables to hold loaded models ---
47
+ # These will be loaded once at startup
48
+ models = {}
49
+
50
+ # --- Helper Function for BERT Embedding ---
51
+ def get_bert_embedding(text: str, model, tokenizer):
52
+ """Generates a BERT embedding for a single piece of text."""
53
+ model.eval()
54
+ inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)
55
+ with torch.no_grad():
56
+ outputs = model(**inputs)
57
+ embedding = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
58
+ return embedding.reshape(1, -1)
59
+
60
+ # --- API Events ---
61
+ @app.on_event("startup")
62
+ def load_models():
63
+ """
64
+ Load all machine learning models and encoders into memory at startup.
65
+ This is efficient as it's done only once.
66
+ """
67
+ print("Loading models and encoders at startup...")
68
+ try:
69
+ models['svm'] = joblib.load(SVM_MODEL_PATH)
70
+ models['xgb'] = joblib.load(XGB_MODEL_PATH)
71
+ models['logreg'] = joblib.load(LOGREG_MODEL_PATH)
72
+ models['label_encoder'] = joblib.load(LE_PATH)
73
+ models['bert_tokenizer'] = BertTokenizer.from_pretrained('bert-base-uncased')
74
+ models['bert_model'] = BertModel.from_pretrained('bert-base-uncased')
75
+ print("All models loaded successfully! 🚀")
76
+ except FileNotFoundError as e:
77
+ print(f"FATAL ERROR: A required model file was not found: {e}")
78
+ # In a real application, you might want to handle this more gracefully
79
+ # For now, we'll exit if a model is missing.
80
+ exit()
81
+
82
+ # --- API Endpoints ---
83
+ @app.get("/", summary="Root endpoint", description="A simple hello world endpoint to confirm the API is running.")
84
+ def read_root():
85
+ return {"message": "Welcome to the Intent Recognition API. Please use the /predict endpoint to get started."}
86
+
87
+ @app.post("/predict", summary="Predict Intent", description="Takes a sentence and returns the predicted intent from three different models.")
88
+ def predict_intent(request: IntentRequest):
89
+ """
90
+ Predicts the intent of the input text using the pre-loaded models.
91
+ """
92
+ # Generate embedding for the input sentence
93
+ embedding = get_bert_embedding(request.text, models['bert_model'], models['bert_tokenizer'])
94
+
95
+ # --- Get predictions from each model ---
96
+ pred_svm_idx = models['svm'].predict(embedding)[0]
97
+ pred_xgb_idx = models['xgb'].predict(embedding)[0]
98
+ pred_logreg_idx = models['logreg'].predict(embedding)[0]
99
+
100
+ # --- Inverse transform the predictions to get the original labels ---
101
+ label_encoder = models['label_encoder']
102
+ pred_svm_label = label_encoder.inverse_transform([pred_svm_idx])[0]
103
+ pred_xgb_label = label_encoder.inverse_transform([pred_xgb_idx])[0]
104
+ pred_logreg_label = label_encoder.inverse_transform([pred_logreg_idx])[0]
105
+
106
+ # --- Return the results in a JSON response ---
107
+ return {
108
+ "input_text": request.text,
109
+ "predictions": {
110
+ "svm_prediction": pred_svm_label,
111
+ "xgboost_prediction": pred_xgb_label,
112
+ "logistic_regression_prediction": pred_logreg_label
113
+ }
114
+ }
115
+
116
+
117
+ if __name__ == '__main__':
118
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ pandas
2
+ scikit-learn
3
+ optuna
4
+ torch
5
+ transformers
6
+ numpy
7
+ xgboost
8
+ fastapi
9
+ uvicorn[standard]
saved_models/bert_logreg_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f131ee3cd6cf20f38c021cc2fe72b1097e50ac5a6018e0d9b10c65eb56030668
3
+ size 31691
saved_models/bert_naive_bayes_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fd8e3d3c8853de61985fe1a9915fb085b1bfc450f5032a66566d1c7f925a41d6
3
+ size 62335
saved_models/bert_svm_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e7a4c434fe8d72b180112de87bbd81b4e52bf48edc4658280b7d765b9e725d08
3
+ size 2499183
saved_models/bert_xgboost_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:18c63c0aceb4accbc23b09a9216eb4b929cb168eccddac6116b45abd022801e0
3
+ size 3456073
saved_models/char_tfidf_logreg_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f7f272ffb2065c633f0ae2ac4bac2a3e68747bb08f280904da84f1fe0b4f0a33
3
+ size 80971
saved_models/char_tfidf_naive_bayes_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:22f720552866df8012d99bf75eb871fcc2031b0c8290ea92e19d0fb998644f25
3
+ size 160863
saved_models/char_tfidf_svm_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b482b07110cc8f5817fbb216b94d56c024610fa5e0699fcaf33fbff4758d3d22
3
+ size 999679
saved_models/char_tfidf_vectorizer.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:80b40d5a801d617cf6eb2e21d96ff094d83cc9ea468aaadee4ef5cb19c7b5fbb
3
+ size 67979
saved_models/label_encoder.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:70ceb4dac7af9a6addbcfa4dbe81962d25ad010985c4ddfe6d09e9aad9fd7aff
3
+ size 586
saved_models/tfidf_logreg_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a182175b92db67ee63c32a4732f09d912d871ca24b22c4ff6cc7f135a70066e
3
+ size 40971
saved_models/tfidf_naive_bayes_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ae10d842a4cd5dd5c80f72282e77656966e9db2c84b225b0837cc5ad74004379
3
+ size 80863
saved_models/tfidf_svm_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:64ccf59afc5d2ae7fdfce7f035a7d80d3b5d60b49763d8776a115a792d691e1f
3
+ size 113247
saved_models/tfidf_vectorizer.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fd90469dbb21db584976f686b771a12957fc718f4755e37aeb31e0bef1f13ef6
3
+ size 37114