Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,92 @@
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
-
|
| 4 |
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
|
| 5 |
from typing import List # Import List from typing
|
| 6 |
from schemas import LoadModelRequest, ChatRequest
|
| 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 |
# import os
|
| 40 |
# import io
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
|
|
|
| 3 |
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
|
| 4 |
from typing import List # Import List from typing
|
| 5 |
from schemas import LoadModelRequest, ChatRequest
|
| 6 |
+
from drive_service import DriveService
|
| 7 |
+
from chatbot_service import ChatbotService
|
| 8 |
+
from model_service import ModelService
|
| 9 |
+
from config import BASE_MODEL_PATH, CUSTOM_CHATBOTS_DIR
|
| 10 |
+
from config import GOOGLE_DRIVE_FOLDER_ID # Import GOOGLE_DRIVE_FOLDER_ID
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import logging
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
app = FastAPI()
|
| 18 |
+
|
| 19 |
+
# Initialize services
|
| 20 |
+
drive_service = DriveService()
|
| 21 |
+
chatbot_service = ChatbotService()
|
| 22 |
+
model_service = ModelService()
|
| 23 |
+
|
| 24 |
+
@app.on_event("startup")
|
| 25 |
+
async def startup_event():
|
| 26 |
+
"""Initialize necessary components on startup."""
|
| 27 |
+
try:
|
| 28 |
+
logger.info("Application started successfully")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error(f"Startup failed: {str(e)}")
|
| 31 |
+
raise
|
| 32 |
+
|
| 33 |
+
@app.post("/load-model/")
|
| 34 |
+
async def load_model(request: LoadModelRequest):
|
| 35 |
+
return model_service.load_model(request.model_name, request.temperature)
|
| 36 |
+
|
| 37 |
+
@app.post("/chat/{model_name}")
|
| 38 |
+
async def chat_with_model(model_name: str, request: ChatRequest):
|
| 39 |
+
return model_service.chat_with_model(model_name, request.question)
|
| 40 |
+
|
| 41 |
+
@app.post("/create-chatbot/")
|
| 42 |
+
async def create_chatbot(
|
| 43 |
+
folder_name: str = Form(...),
|
| 44 |
+
pdf_files: List[UploadFile] = File(...) # Now List is imported
|
| 45 |
+
):
|
| 46 |
+
return chatbot_service.create_chatbot(folder_name, pdf_files)
|
| 47 |
+
|
| 48 |
+
@app.get("/health")
|
| 49 |
+
async def health_check():
|
| 50 |
+
return {"status": "healthy"}
|
| 51 |
+
|
| 52 |
+
@app.get("/")
|
| 53 |
+
async def root():
|
| 54 |
+
return {"message": "API is running"}
|
| 55 |
+
|
| 56 |
+
@app.get("/available-models")
|
| 57 |
+
async def list_available_models():
|
| 58 |
+
try:
|
| 59 |
+
models = [name for name in os.listdir(BASE_MODEL_PATH) if os.path.isdir(os.path.join(BASE_MODEL_PATH, name))]
|
| 60 |
+
return {
|
| 61 |
+
"status": "success",
|
| 62 |
+
"models": models
|
| 63 |
+
}
|
| 64 |
+
except Exception as e:
|
| 65 |
+
logger.error(f"Error listing models: {str(e)}")
|
| 66 |
+
raise HTTPException(status_code=500, detail="Failed to list available models")
|
| 67 |
+
|
| 68 |
+
@app.get("/list-models/")
|
| 69 |
+
async def list_models():
|
| 70 |
+
"""List all models (chatbots) available in Google Drive."""
|
| 71 |
+
try:
|
| 72 |
+
# List all folders in the Google Drive folder
|
| 73 |
+
folders = drive_service.list_files_in_folder(GOOGLE_DRIVE_FOLDER_ID)
|
| 74 |
+
models = [folder['name'] for folder in folders if folder['mimeType'] == 'application/vnd.google-apps.folder']
|
| 75 |
+
return {
|
| 76 |
+
"status": "success",
|
| 77 |
+
"models": models
|
| 78 |
+
}
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"Error listing models: {str(e)}")
|
| 81 |
+
raise HTTPException(status_code=500, detail=f"Failed to list models: {str(e)}")
|
| 82 |
+
|
| 83 |
+
@app.post("/update-chatbot/")
|
| 84 |
+
async def update_chatbot(
|
| 85 |
+
folder_name: str = Form(...),
|
| 86 |
+
pdf_files: List[UploadFile] = File(...)
|
| 87 |
+
):
|
| 88 |
+
"""Update an existing chatbot by appending new data (PDF files)."""
|
| 89 |
+
return chatbot_service.update_chatbot(folder_name, pdf_files)
|
| 90 |
|
| 91 |
# import os
|
| 92 |
# import io
|