File size: 1,188 Bytes
c9dc9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```python
from fastapi import APIRouter, UploadFile, File
from services.faiss_service import FAISSService
from services.gemini_service import GeminiService
import aiofiles
import os

router = APIRouter()
faiss_service = FAISSService()
gemini_service = GeminiService()

@router.post("/upload")
async def upload_faq(file: UploadFile = File(...)):
    # Save uploaded file temporarily
    temp_path = f"/tmp/{file.filename}"
    async with aiofiles.open(temp_path, 'wb') as out_file:
        content = await file.read()
        await out_file.write(content)
    
    # Process file based on extension
    if file.filename.endswith('.csv'):
        docs = await process_csv(temp_path)
    elif file.filename.endswith('.md'):
        docs = await process_markdown(temp_path)
    else:
        return {"error": "Unsupported file type"}
    
    # Generate embeddings and create index
    await faiss_service.create_index(docs)
    
    # Clean up
    os.remove(temp_path)
    return {"status": "success", "documents_processed": len(docs)}

async def process_csv(path: str):
    # CSV processing logic
    pass

async def process_markdown(path: str):
    # Markdown processing logic
    pass
```