Erenuo commited on
Commit
60b4e60
·
verified ·
1 Parent(s): 2a2f4cf

Upload 10 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ 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
+ db/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -text
client.html ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 API Client</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
9
+ <style>
10
+ body { font-family: 'Inter', sans-serif; }
11
+ .spinner {
12
+ border: 4px solid rgba(0, 0, 0, 0.1);
13
+ width: 36px; height: 36px; border-radius: 50%;
14
+ border-left-color: #09f;
15
+ animation: spin 1s ease infinite;
16
+ }
17
+ @keyframes spin {
18
+ 0% { transform: rotate(0deg); }
19
+ 100% { transform: rotate(360deg); }
20
+ }
21
+ </style>
22
+ </head>
23
+ <body class="bg-gray-100 flex items-center justify-center min-h-screen">
24
+ <div class="w-full max-w-2xl mx-auto bg-white rounded-xl shadow-lg p-8">
25
+ <div class="text-center mb-8">
26
+ <h1 class="text-3xl font-bold text-gray-800">Intent Recognizer</h1>
27
+ <p class="text-gray-500 mt-2">Enter a sentence to find its top intents using the API.</p>
28
+ </div>
29
+
30
+ <div class="space-y-4">
31
+ <textarea id="query-input" class="w-full h-24 p-4 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" placeholder="e.g., can you reduce the price on the house in islington"></textarea>
32
+ <button id="submit-btn" class="w-full bg-blue-600 text-white font-semibold py-3 px-4 rounded-lg hover:bg-blue-700">
33
+ Recognize Intent
34
+ </button>
35
+ </div>
36
+
37
+ <div id="results-container" class="mt-8 hidden">
38
+ <h2 class="text-2xl font-semibold text-gray-700 mb-4 border-b pb-2">API Response</h2>
39
+ <div id="results-content" class="space-y-6 bg-gray-50 p-6 rounded-lg">
40
+ <!-- Results will be injected here -->
41
+ </div>
42
+ </div>
43
+
44
+ <div id="loading-spinner" class="hidden justify-center mt-8">
45
+ <div class="spinner"></div>
46
+ </div>
47
+
48
+ <div id="error-message" class="hidden mt-6 p-4 bg-red-100 text-red-700 border border-red-300 rounded-lg"></div>
49
+ </div>
50
+
51
+ <script>
52
+ const submitBtn = document.getElementById('submit-btn');
53
+ const queryInput = document.getElementById('query-input');
54
+ const resultsContainer = document.getElementById('results-container');
55
+ const resultsContent = document.getElementById('results-content');
56
+ const loadingSpinner = document.getElementById('loading-spinner');
57
+ const errorMessage = document.getElementById('error-message');
58
+ const API_URL = "http://127.0.0.1:8000/recognize-intent/";
59
+
60
+ submitBtn.addEventListener('click', async () => {
61
+ const query = queryInput.value.trim();
62
+ if (!query) { return; }
63
+
64
+ loadingSpinner.style.display = 'flex';
65
+ resultsContainer.style.display = 'none';
66
+ errorMessage.style.display = 'none';
67
+ resultsContent.innerHTML = '';
68
+
69
+ try {
70
+ const response = await fetch(API_URL, {
71
+ method: 'POST',
72
+ headers: { 'Content-Type': 'application/json' },
73
+ body: JSON.stringify({ text: query }),
74
+ });
75
+ if (!response.ok) {
76
+ const errorData = await response.json();
77
+ throw new Error(errorData.detail || `API request failed: ${response.status}`);
78
+ }
79
+ const data = await response.json();
80
+ displayResults(data);
81
+ } catch (error) {
82
+ displayError(error);
83
+ } finally {
84
+ loadingSpinner.style.display = 'none';
85
+ }
86
+ });
87
+
88
+ function displayResults(data) {
89
+ resultsContainer.style.display = 'block';
90
+
91
+ // FIX: The API now returns a `predictions` array. We must loop through it.
92
+ // The error occurs when trying to access `data.confidence_score` which no longer exists.
93
+ // The correct value is inside each `prediction` object as `prediction.score`.
94
+ data.predictions.forEach((prediction, index) => {
95
+ const intent = prediction.intent;
96
+ const score = prediction.score.toFixed(4); // Accessing score from the prediction object
97
+
98
+ const resultBlock = `
99
+ <div>
100
+ <div class="flex justify-between items-center">
101
+ <span class="text-sm font-bold text-gray-500">Prediction #${index + 1}</span>
102
+ </div>
103
+ <div class="mt-2 grid grid-cols-2 gap-4">
104
+ <div class="flex flex-col">
105
+ <span class="text-sm font-medium text-gray-500">Recognized Intent</span>
106
+ <span class="text-lg font-semibold text-gray-800">${intent}</span>
107
+ </div>
108
+ <div class="flex flex-col">
109
+ <span class="text-sm font-medium text-gray-500">Confidence Score</span>
110
+ <span class="text-lg font-semibold text-gray-800">${score}</span>
111
+ </div>
112
+ </div>
113
+ </div>
114
+ `;
115
+ // Add a divider if it's not the last item
116
+ resultsContent.innerHTML += resultBlock + (index < data.predictions.length - 1 ? '<hr class="my-3">' : '');
117
+ });
118
+ }
119
+
120
+ function displayError(error) {
121
+ errorMessage.style.display = 'block';
122
+ errorMessage.textContent = `An error occurred: ${error.message}. Please ensure the API server is running.`;
123
+ }
124
+ </script>
125
+ </body>
126
+ </html>
db/05613b54-03e2-4057-be23-cd17cf407dc5/data_level0.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:80ed106534f1f811cccbbf846bccd920365edd407de747b53df490022898cd70
3
+ size 32120000
db/05613b54-03e2-4057-be23-cd17cf407dc5/header.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:111d78352bbd55e12a9b9860d3418768bd839f84859d58c79f5d06b7835e40ee
3
+ size 100
db/05613b54-03e2-4057-be23-cd17cf407dc5/index_metadata.pickle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:961e739fa96504733b904ca895f1c28dfdf6eaafc543d941b9c8fcf3148fe178
3
+ size 92132
db/05613b54-03e2-4057-be23-cd17cf407dc5/length.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:30f9db8b6c56df60c85199505650eb7b5c444768e155824577c86859e4b17da2
3
+ size 40000
db/05613b54-03e2-4057-be23-cd17cf407dc5/link_lists.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b7d4d872d9a9e9b81f8d529085901695f169eaafa40984561846d7b5c294154d
3
+ size 8624
db/chroma.sqlite3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc7c14ffbdc4271a175ba00c8407b6ad28f5b6a166456eb4f092e3bd57ea8b07
3
+ size 5255168
main_api.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ import uvicorn
5
+ from typing import List
6
+
7
+ try:
8
+ from recognize import IntentRecognizer
9
+ except ImportError:
10
+ # Dummy class for startup if recognize.py is missing
11
+ class IntentRecognizer:
12
+ def __init__(self): self.collection = None
13
+ def recognize_intent(self, query_text): return [{"intent": "error", "score": 0.0}]
14
+
15
+ # --- CHANGE: Update API Data Models ---
16
+ class Prediction(BaseModel):
17
+ intent: str
18
+ score: float
19
+
20
+ class QueryRequest(BaseModel):
21
+ text: str
22
+
23
+ class IntentResponse(BaseModel):
24
+ query_text: str
25
+ predictions: List[Prediction] # The response is now a list of predictions
26
+
27
+ # --- FastAPI Application ---
28
+ app = FastAPI(
29
+ title="Intent Recognition API",
30
+ description="An API that uses a vector database to recognize user intent.",
31
+ version="1.1.0", # Version bump
32
+ )
33
+
34
+ origins = ["*"]
35
+ app.add_middleware(
36
+ CORSMiddleware,
37
+ allow_origins=origins,
38
+ allow_credentials=True,
39
+ allow_methods=["*"],
40
+ allow_headers=["*"],
41
+ )
42
+
43
+ recognizer = None
44
+ @app.on_event("startup")
45
+ def load_recognizer():
46
+ global recognizer
47
+ print("--- Application starting up... ---")
48
+ recognizer = IntentRecognizer()
49
+
50
+ # --- CHANGE: Update API Endpoint ---
51
+ @app.post("/recognize-intent/", response_model=IntentResponse)
52
+ def recognize_intent_endpoint(request: QueryRequest):
53
+ if not recognizer or not recognizer.collection:
54
+ raise HTTPException(status_code=503, detail="Intent Recognizer is not available.")
55
+
56
+ query = request.text
57
+ # The recognizer now returns a list of prediction dicts
58
+ predictions_list = recognizer.recognize_intent(query)
59
+
60
+ return IntentResponse(
61
+ query_text=query,
62
+ predictions=predictions_list
63
+ )
64
+
65
+ @app.get("/", summary="Root endpoint for health check")
66
+ def read_root():
67
+ return {"status": "Intent Recognition API is running."}
68
+
69
+ if __name__ == "__main__":
70
+ uvicorn.run("main_api:app", host="127.0.0.1", port=8000, reload=True)
recognize.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chromadb
2
+ from sentence_transformers import SentenceTransformer
3
+ from collections import defaultdict
4
+
5
+ # --- Configuration ---
6
+ COLLECTION_NAME = "intent_recognizer"
7
+ DB_PATH = "db"
8
+ EMBEDDING_MODEL = 'all-mpnet-base-v2'
9
+
10
+ # --- Tunable Parameters ---
11
+ N_RESULTS = 100
12
+ CONFIDENCE_THRESHOLD = 18.0
13
+
14
+
15
+ class IntentRecognizer:
16
+ def __init__(self):
17
+ print("--- Initializing Intent Recognizer ---")
18
+ try:
19
+ print(f"Loading embedding model: '{EMBEDDING_MODEL}'...")
20
+ self.model = SentenceTransformer(EMBEDDING_MODEL)
21
+ print(f"Connecting to persistent ChromaDB client at: '{DB_PATH}'...")
22
+ client = chromadb.PersistentClient(path=DB_PATH)
23
+ print(f"Getting collection: '{COLLECTION_NAME}'...")
24
+ self.collection = client.get_collection(name=COLLECTION_NAME)
25
+ print("--- Initialization Complete ---")
26
+ except Exception as e:
27
+ print(f"Error during initialization: {e}")
28
+ self.model = None
29
+ self.collection = None
30
+
31
+ def recognize_intent(self, query_text):
32
+ if not self.collection or not self.model:
33
+ return [] # Return an empty list on error
34
+
35
+ print(f"\n--- Recognizing Intent for Query: '{query_text}' ---")
36
+ query_embedding = self.model.encode([query_text], show_progress_bar=False)
37
+ results = self.collection.query(
38
+ query_embeddings=query_embedding.tolist(),
39
+ n_results=N_RESULTS,
40
+ )
41
+
42
+ intent_scores = defaultdict(float)
43
+ retrieved_metadatas = results.get('metadatas', [[]])[0]
44
+ retrieved_distances = results.get('distances', [[]])[0]
45
+
46
+ if not retrieved_metadatas:
47
+ return []
48
+
49
+ print(f"Retrieved {len(retrieved_metadatas)} results. Calculating scores...")
50
+ for metadata, distance in zip(retrieved_metadatas, retrieved_distances):
51
+ intent = metadata.get('intent')
52
+ if intent:
53
+ similarity_score = 1 - distance
54
+ intent_scores[intent] += similarity_score
55
+
56
+ if not intent_scores:
57
+ return []
58
+
59
+ # --- CHANGE: Sort all intents by score and get the top 2 ---
60
+ sorted_intents = sorted(intent_scores.items(), key=lambda item: item[1], reverse=True)
61
+
62
+ # Format as a list of dictionaries
63
+ top_predictions = [{"intent": intent, "score": score} for intent, score in sorted_intents[:2]]
64
+
65
+ print("\n--- Scoring Summary (Top 2) ---")
66
+ for pred in top_predictions:
67
+ print(f" - Intent: {pred['intent']:<25} | Total Score: {pred['score']:.4f}")
68
+
69
+ # --- CHANGE: Apply threshold to the top prediction ---
70
+ # If the best score is too low, return an 'unknown' intent.
71
+ if not top_predictions or top_predictions[0]['score'] < CONFIDENCE_THRESHOLD:
72
+ print(f"\nTop score is below threshold of {CONFIDENCE_THRESHOLD}.")
73
+ return [{"intent": "unknown", "score": top_predictions[0]['score'] if top_predictions else 0.0}]
74
+
75
+ print(f"\n=> Top Prediction: '{top_predictions[0]['intent']}' (Confidence: {top_predictions[0]['score']:.4f})")
76
+ return top_predictions
77
+
78
+ # This part is no longer needed for running the API, but can be kept for testing
79
+ if __name__ == '__main__':
80
+ recognizer = IntentRecognizer()
81
+ if recognizer.collection:
82
+ recognizer.recognize_intent("can you reduce the price on the house in islington")
requirements.txt ADDED
Binary file (4.05 kB). View file