Save session when file is uploaded

This commit is contained in:
Guillem Borrell 2024-06-12 12:45:09 +02:00
parent a994eacd2d
commit 833a446899
4 changed files with 41 additions and 16 deletions

View file

@ -3,10 +3,12 @@ import aiofiles
# import s3fs # import s3fs
from fastapi import APIRouter, File, UploadFile from fastapi import APIRouter, File, UploadFile
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from starlette.requests import Request
from ..config import settings from ..config import settings
from ..db import StorageEngines from ..db import StorageEngines
from ..sessions import SessionDB from ..sessions import SessionDB
from ..users import OwnershipDB
router = APIRouter() router = APIRouter()
@ -21,7 +23,7 @@ router = APIRouter()
@router.post("/upload", tags=["files"]) @router.post("/upload", tags=["files"])
async def upload_file(file: UploadFile = File(...), sid: str = ""): async def upload_file(request: Request, file: UploadFile = File(...), sid: str = ""):
async with aiofiles.tempfile.NamedTemporaryFile("wb") as f: async with aiofiles.tempfile.NamedTemporaryFile("wb") as f:
content = await file.read() content = await file.read()
await f.write(content) await f.write(content)
@ -39,6 +41,13 @@ async def upload_file(file: UploadFile = File(...), sid: str = ""):
.dump() .dump()
) )
OwnershipDB(
StorageEngines.gcs,
gcs_access=settings.gcs_access,
gcs_secret=settings.gcs_secret,
bucket=settings.gcs_bucketname,
).set_ownersip(request.session.get("user").get("email"), sid)
return JSONResponse( return JSONResponse(
content={"message": "File uploaded successfully"}, status_code=200 content={"message": "File uploaded successfully"}, status_code=200
) )

View file

@ -16,17 +16,8 @@ router = APIRouter()
@router.get("/new_session") @router.get("/new_session")
async def get_new_session(request: Request) -> str: async def get_new_session() -> str:
user_email = request.session.get("user").get("email") return str(uuid4())
ownership = OwnershipDB(
StorageEngines.gcs,
gcs_access=settings.gcs_access,
gcs_secret=settings.gcs_secret,
bucket=settings.gcs_bucketname,
)
sid = str(uuid4())
return ownership.set_ownersip(user_email, sid)
@router.get("/greetings", response_class=PlainTextResponse) @router.get("/greetings", response_class=PlainTextResponse)

View file

@ -86,8 +86,9 @@
</div> </div>
<!-- Button trigger modal --> <!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop"> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop"
Or load an existing session id="loadSessionsButton">
Browse sessions
</button> </button>
<!-- Modal --> <!-- Modal -->
@ -100,8 +101,8 @@
<button type="button" class="btn-close" data-bs-dismiss="modal" <button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button> aria-label="Close"></button>
</div> </div>
<div class="modal-body"> <div class="modal-body" id="userSessions">
<p>Current sessions</p> <p>Current sessions...</p>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>

View file

@ -13,6 +13,7 @@ $('#menu-toggle').click(function (e) {
toggleMenuArrow(document.getElementById('menu-toggle')); toggleMenuArrow(document.getElementById('menu-toggle'));
}); });
// Hide sidebar on mobile devices
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
console.log('Width: ' + window.innerWidth + ' Height: ' + window.innerHeight); console.log('Width: ' + window.innerWidth + ' Height: ' + window.innerHeight);
if ((window.innerWidth <= 800) && (window.innerHeight <= 600)) { if ((window.innerWidth <= 800) && (window.innerHeight <= 600)) {
@ -25,8 +26,10 @@ document.addEventListener("DOMContentLoaded", function () {
const textarea = document.getElementById('chatTextarea'); const textarea = document.getElementById('chatTextarea');
const sendButton = document.getElementById('sendButton'); const sendButton = document.getElementById('sendButton');
const sessions = document.getElementById('userSessions');
const chatMessages = document.querySelector('.chat-messages'); const chatMessages = document.querySelector('.chat-messages');
// Auto resize textarea
textarea.addEventListener('input', function () { textarea.addEventListener('input', function () {
this.style.height = 'auto'; this.style.height = 'auto';
this.style.height = (this.scrollHeight <= 150 ? this.scrollHeight : 150) + 'px'; this.style.height = (this.scrollHeight <= 150 ? this.scrollHeight : 150) + 'px';
@ -53,6 +56,7 @@ async function fetchResponse(message, newMessage) {
} }
} }
// Function to add AI message
function addAIMessage(messageContent) { function addAIMessage(messageContent) {
const newMessage = document.createElement('div'); const newMessage = document.createElement('div');
newMessage.classList.add('message', 'bg-white', 'p-2', 'mb-2', 'rounded'); newMessage.classList.add('message', 'bg-white', 'p-2', 'mb-2', 'rounded');
@ -123,6 +127,7 @@ document.addEventListener("DOMContentLoaded", function () {
fetchGreeting(); fetchGreeting();
}); });
// Function upload the data file
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
const fileInput = document.getElementById('inputGroupFile01'); const fileInput = document.getElementById('inputGroupFile01');
const uploadButton = document.getElementById('uploadButton'); const uploadButton = document.getElementById('uploadButton');
@ -158,3 +163,22 @@ document.addEventListener("DOMContentLoaded", function () {
} }
}); });
}); });
// Function to get the user sessions
document.addEventListener("DOMContentLoaded", function () {
const sessionsButton = document.getElementById('loadSessionsButton');
sessionsButton.addEventListener('click', async function fetchSessions() {
try {
const response = await fetch('/sessions');
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const data = await response.text();
sessions.innerHTML = data;
} catch (error) {
sessions.innerHTML = 'Error: ' + error.message;
}
}
);
}
);