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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# main.py - FastAPI Backend

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
import nltk
from nltk.tokenize import sent_tokenize
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

# Download required NLTK data
nltk.download('punkt', quiet=True)
nltk.download('punkt_tab')

# Initialize FastAPI app
app = FastAPI(title="Simple Search Engine")

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Define the document database
documents = {
    "doc1": """

    A new AI analytics tool has been released by TechCorp. 

    This tool uses advanced machine learning algorithms to process large datasets. 

    It can provide real-time insights and predictive analytics for businesses. 

    The tool integrates seamlessly with existing data infrastructure. 

    Companies can now make data-driven decisions faster than ever before. 

    The AI engine continuously learns from new data to improve accuracy.

    """,
    
    "doc2": """

    The quarterly finance report shows strong revenue growth. 

    Operating expenses have decreased by 15% compared to last quarter. 

    Net profit margins have improved significantly across all divisions. 

    The company's cash flow remains healthy with substantial reserves. 

    Investment in new projects is expected to yield returns next year. 

    Shareholders can expect increased dividends this quarter.

    """,
    
    "doc3": """

    Cloud infrastructure services from AWS and Azure are becoming essential. 

    Companies are migrating their legacy systems to the cloud for better scalability. 

    AWS offers a wide range of compute and storage options. 

    Azure provides excellent integration with Microsoft enterprise products. 

    Both platforms support hybrid cloud deployments for flexibility. 

    Security and compliance features are continuously being enhanced.

    """,
    
    "doc4": """

    Our new marketing campaign focuses on SEO optimization strategies. 

    We are targeting high-value keywords to increase organic traffic. 

    Social media engagement has improved by 40% this month. 

    Content marketing efforts are driving more qualified leads. 

    The campaign includes email marketing and paid search ads. 

    We expect to see ROI improvements within the next quarter.

    """,
    
    "doc5": """

    The AI tool leverages machine learning for predictive maintenance. 

    Machine learning models can detect patterns in equipment behavior. 

    This AI-powered solution reduces downtime and operational costs. 

    Deep learning techniques are applied to analyze sensor data. 

    The system continuously learns and adapts to new scenarios. 

    AI and machine learning are transforming industrial operations.

    """
}

# Function to chunk documents
def chunk_documents(documents, sentences_per_chunk=3):
    chunks = []
    chunk_metadata = []
    
    for doc_id, text in documents.items():
        sentences = sent_tokenize(text.strip())
        for i in range(0, len(sentences), sentences_per_chunk):
            chunk = ' '.join(sentences[i:i+sentences_per_chunk])
            chunks.append(chunk)
            chunk_metadata.append({
                'doc_id': doc_id,
                'chunk_index': i // sentences_per_chunk,
                'text': chunk
            })
    
    return chunks, chunk_metadata

# Initialize model and process documents at startup
print("Initializing search engine...")
model = SentenceTransformer('all-MiniLM-L6-v2')
chunks, chunk_metadata = chunk_documents(documents)
chunk_embeddings = model.encode(chunks)
print(f"Search engine ready! {len(chunks)} chunks indexed.")

# Pydantic models
class SearchQuery(BaseModel):
    query: str

class SearchResult(BaseModel):
    rank: int
    doc_id: str
    similarity_score: float
    text: str

# API Endpoints
@app.get("/")
async def read_root():
    html_content = """

    <!DOCTYPE html>

    <html lang="en">

    <head>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>Simple Search Engine</title>

        <style>

            * {

                margin: 0;

                padding: 0;

                box-sizing: border-box;

            }

            

            body {

                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

                min-height: 100vh;

                padding: 20px;

            }

            

            .container {

                max-width: 900px;

                margin: 0 auto;

            }

            

            .header {

                text-align: center;

                color: white;

                margin-bottom: 40px;

                padding-top: 60px;

            }

            

            .header h1 {

                font-size: 3em;

                margin-bottom: 10px;

                text-shadow: 2px 2px 4px rgba(0,0,0,0.3);

            }

            

            .header p {

                font-size: 1.2em;

                opacity: 0.9;

            }

            

            .search-box {

                background: white;

                border-radius: 50px;

                padding: 10px 20px;

                box-shadow: 0 8px 30px rgba(0,0,0,0.3);

                display: flex;

                align-items: center;

                margin-bottom: 40px;

            }

            

            .search-box input {

                flex: 1;

                border: none;

                outline: none;

                font-size: 1.1em;

                padding: 10px;

            }

            

            .search-box button {

                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

                border: none;

                color: white;

                padding: 12px 30px;

                border-radius: 25px;

                font-size: 1em;

                cursor: pointer;

                transition: transform 0.2s;

                font-weight: bold;

            }

            

            .search-box button:hover {

                transform: scale(1.05);

            }

            

            .search-box button:active {

                transform: scale(0.95);

            }

            

            .loading {

                text-align: center;

                color: white;

                font-size: 1.2em;

                margin: 20px 0;

                display: none;

            }

            

            .loading.show {

                display: block;

            }

            

            .results {

                display: none;

            }

            

            .results.show {

                display: block;

            }

            

            .result-card {

                background: white;

                border-radius: 15px;

                padding: 25px;

                margin-bottom: 20px;

                box-shadow: 0 4px 15px rgba(0,0,0,0.2);

                transition: transform 0.2s, box-shadow 0.2s;

                animation: slideIn 0.5s ease-out;

            }

            

            @keyframes slideIn {

                from {

                    opacity: 0;

                    transform: translateY(20px);

                }

                to {

                    opacity: 1;

                    transform: translateY(0);

                }

            }

            

            .result-card:hover {

                transform: translateY(-5px);

                box-shadow: 0 6px 25px rgba(0,0,0,0.3);

            }

            

            .result-header {

                display: flex;

                justify-content: space-between;

                align-items: center;

                margin-bottom: 15px;

            }

            

            .result-rank {

                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

                color: white;

                padding: 5px 15px;

                border-radius: 20px;

                font-weight: bold;

                font-size: 0.9em;

            }

            

            .result-doc {

                color: #666;

                font-size: 0.9em;

                font-weight: 600;

            }

            

            .result-score {

                background: #e8f5e9;

                color: #2e7d32;

                padding: 5px 12px;

                border-radius: 15px;

                font-size: 0.85em;

                font-weight: bold;

            }

            

            .result-text {

                color: #333;

                line-height: 1.6;

                font-size: 1em;

            }

            

            .no-results {

                text-align: center;

                color: white;

                font-size: 1.2em;

                margin-top: 40px;

                display: none;

            }

            

            .no-results.show {

                display: block;

            }

            

            .stats {

                text-align: center;

                color: white;

                margin-bottom: 30px;

                font-size: 1.1em;

                opacity: 0.9;

            }

        </style>

    </head>

    <body>

        <div class="container">

            <div class="header">

                <h1>๐Ÿ” SimpleSearch</h1>

                <p>Your intelligent document search engine</p>

            </div>

            

            <div class="search-box">

                <input type="text" id="searchInput" placeholder="Search for documents..." />

                <button onclick="performSearch()">Search</button>

            </div>

            

            <div class="loading" id="loading">

                <p>๐Ÿ”„ Searching...</p>

            </div>

            

            <div class="stats" id="stats"></div>

            

            <div class="results" id="results"></div>

            

            <div class="no-results" id="noResults">

                <p>No results found. Try a different query!</p>

            </div>

        </div>

        

        <script>

            // Allow Enter key to trigger search

            document.getElementById('searchInput').addEventListener('keypress', function(e) {

                if (e.key === 'Enter') {

                    performSearch();

                }

            });

            

            async function performSearch() {

                const query = document.getElementById('searchInput').value.trim();

                

                if (!query) {

                    alert('Please enter a search query!');

                    return;

                }

                

                // Show loading, hide results

                document.getElementById('loading').classList.add('show');

                document.getElementById('results').classList.remove('show');

                document.getElementById('noResults').classList.remove('show');

                document.getElementById('stats').innerHTML = '';

                

                try {

                    const response = await fetch('/search', {

                        method: 'POST',

                        headers: {

                            'Content-Type': 'application/json',

                        },

                        body: JSON.stringify({ query: query })

                    });

                    

                    if (!response.ok) {

                        throw new Error('Search failed');

                    }

                    

                    const data = await response.json();

                    displayResults(data, query);

                    

                } catch (error) {

                    console.error('Error:', error);

                    alert('Search failed. Please try again.');

                } finally {

                    document.getElementById('loading').classList.remove('show');

                }

            }

            

            function displayResults(results, query) {

                const resultsDiv = document.getElementById('results');

                const noResultsDiv = document.getElementById('noResults');

                const statsDiv = document.getElementById('stats');

                

                if (results.length === 0) {

                    noResultsDiv.classList.add('show');

                    return;

                }

                

                statsDiv.innerHTML = `Found <strong>${results.length}</strong> results for "<strong>${query}</strong>"`;

                

                resultsDiv.innerHTML = '';

                

                results.forEach(result => {

                    const card = document.createElement('div');

                    card.className = 'result-card';

                    card.style.animationDelay = `${(result.rank - 1) * 0.1}s`;

                    

                    card.innerHTML = `

                        <div class="result-header">

                            <div style="display: flex; gap: 10px; align-items: center;">

                                <span class="result-rank">Rank ${result.rank}</span>

                                <span class="result-doc">${result.doc_id.toUpperCase()}</span>

                            </div>

                            <span class="result-score">Score: ${result.similarity_score.toFixed(4)}</span>

                        </div>

                        <div class="result-text">${result.text}</div>

                    `;

                    

                    resultsDiv.appendChild(card);

                });

                

                resultsDiv.classList.add('show');

            }

        </script>

    </body>

    </html>

    """
    return HTMLResponse(content=html_content)

@app.post("/search", response_model=list[SearchResult])
async def search(search_query: SearchQuery):
    """

    Search endpoint that takes a query and returns top 5 relevant chunks

    """
    if not search_query.query.strip():
        raise HTTPException(status_code=400, detail="Query cannot be empty")
    
    try:
        # Encode the query
        query_embedding = model.encode([search_query.query])
        
        # Calculate cosine similarity
        similarities = cosine_similarity(query_embedding, chunk_embeddings)[0]
        
        # Create results
        results = []
        for idx, score in enumerate(similarities):
            results.append({
                'chunk_index': idx,
                'doc_id': chunk_metadata[idx]['doc_id'],
                'similarity_score': float(score),
                'text': chunk_metadata[idx]['text']
            })
        
        # Sort by similarity score
        results_sorted = sorted(results, key=lambda x: x['similarity_score'], reverse=True)
        
        # Return top 5 results
        top_results = []
        for rank, result in enumerate(results_sorted[:5], 1):
            top_results.append(SearchResult(
                rank=rank,
                doc_id=result['doc_id'],
                similarity_score=result['similarity_score'],
                text=result['text']
            ))
        
        return top_results
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Search error: {str(e)}")

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {"status": "healthy", "total_chunks": len(chunks)}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)