Skip to content

Commit

Permalink
Merge pull request #17 from nwaughachukwuma/replace-local-session-wit…
Browse files Browse the repository at this point in the history
…h-db-session-model

Replace local session with db session model
  • Loading branch information
nwaughachukwuma authored Nov 26, 2024
2 parents 293743a + 65cd92d commit edd6700
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 230 deletions.
34 changes: 15 additions & 19 deletions api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,29 @@
from fastapi.responses import JSONResponse, StreamingResponse
from fastapi_utilities import add_timer_middleware

from src.services.storage import StorageManager
from src.utils.chat_request import chat_request
from src.utils.chat_utils import (
from .services.storage import StorageManager
from .utils.chat_request import chat_request
from .utils.chat_utils import (
SessionChatItem,
SessionChatRequest,
)
from src.utils.custom_sources.base_utils import SourceContent
from src.utils.custom_sources.extract_url_content import ExtractURLContent, ExtractURLContentRequest
from src.utils.custom_sources.generate_url_source import (
from .utils.custom_sources.base_utils import SourceContent
from .utils.custom_sources.extract_url_content import ExtractURLContent, ExtractURLContentRequest
from .utils.custom_sources.generate_url_source import (
CustomSourceManager,
CustomSourceModel,
DeleteCustomSourcesRequest,
GenerateCustomSourceRequest,
GetCustomSourcesRequest,
generate_custom_source,
)
from src.utils.custom_sources.save_copied_source import CopiedPasteSourceRequest, save_copied_source
from src.utils.custom_sources.save_uploaded_sources import UploadedFiles
from src.utils.generate_audiocast import (
GenerateAudioCastRequest,
GenerateAudioCastResponse,
generate_audiocast,
)
from src.utils.generate_audiocast_source import GenerateAudiocastSource, generate_audiocast_source
from src.utils.get_audiocast import get_audiocast
from src.utils.get_session_title import GetSessionTitleModel, get_session_title
from src.utils.session_manager import SessionManager
from .utils.custom_sources.save_copied_source import CopiedPasteSourceRequest, save_copied_source
from .utils.custom_sources.save_uploaded_sources import UploadedFiles
from .utils.generate_audiocast import GenerateAudioCastRequest, generate_audiocast
from .utils.generate_audiocast_source import GenerateAudiocastSource, generate_audiocast_source
from .utils.get_audiocast import get_audiocast
from .utils.get_session_title import GetSessionTitleModel, get_session_title
from .utils.session_manager import SessionManager, SessionModel

app = FastAPI(title="Audiora", version="1.0.0")

Expand Down Expand Up @@ -96,15 +92,15 @@ def on_finish(text: str):
return StreamingResponse(response, media_type="text/event-stream")


@app.post("/audiocast/generate", response_model=GenerateAudioCastResponse)
@app.post("/audiocast/generate", response_model=str)
async def generate_audiocast_endpoint(
request: GenerateAudioCastRequest,
background_tasks: BackgroundTasks,
):
return await generate_audiocast(request, background_tasks)


@app.get("/audiocast/{session_id}", response_model=GenerateAudioCastResponse)
@app.get("/audiocast/{session_id}", response_model=SessionModel)
def get_audiocast_endpoint(session_id: str):
result = get_audiocast(session_id)
return result
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UploadItemParams:


class StorageManager:
def check_blob_exists(self, root_path: str, filename: str):
def check_blob_exists(self, filename: str, root_path=BLOB_BASE_URI):
"""check if a file exists in the bucket"""
blobname = f"{root_path}/{filename}"
blobs = listBlobs(prefix=root_path)
Expand Down
6 changes: 0 additions & 6 deletions api/src/utils/audiocast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ class GenerateAudioCastRequest(BaseModel):
category: ContentCategory


class GenerateAudioCastResponse(BaseModel):
script: str
source_content: str
created_at: str | None


class GenerateAudiocastDict(TypedDict):
url: str
script: str
Expand Down
26 changes: 2 additions & 24 deletions api/src/utils/generate_audiocast.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from datetime import datetime

from fastapi import BackgroundTasks, HTTPException

from src.services.storage import StorageManager
from src.utils.audio_manager import AudioManager, AudioManagerConfig
from src.utils.audiocast_script_maker import AudioScriptMaker
from src.utils.audiocast_utils import (
GenerateAudioCastRequest,
GenerateAudioCastResponse,
)
from src.utils.audiocast_utils import GenerateAudioCastRequest
from src.utils.chat_utils import ContentCategory
from src.utils.custom_sources.base_utils import CustomSourceManager
from src.utils.generate_audiocast_source import GenerateAudiocastSource, generate_audiocast_source
Expand Down Expand Up @@ -65,19 +60,6 @@ def update_session_info(info: str):
session_data = SessionManager.data(session_id)
source_content = session_data.metadata.source if session_data and session_data.metadata else None

if not source_content:
update_session_info("Generating source content...")
source_content = await generate_audiocast_source(
GenerateAudiocastSource(
sessionId=session_id,
category=category,
preferenceSummary=summary,
),
)

session_data = SessionManager.data(session_id)
source_content = session_data.metadata.source if session_data and session_data.metadata else None

if not source_content:
update_session_info("Generating source content...")
source_content = await generate_audiocast_source(
Expand Down Expand Up @@ -116,8 +98,4 @@ def update_session_info(info: str):
audio_script,
)

return GenerateAudioCastResponse(
script=audio_script,
source_content=source_content,
created_at=datetime.now().strftime("%Y-%m-%d %H:%M"),
)
return "Audiocast generated successfully!"
25 changes: 9 additions & 16 deletions api/src/utils/get_audiocast.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from datetime import datetime

from fastapi import HTTPException

from src.services.storage import StorageManager
from src.utils.generate_audiocast import GenerateAudioCastResponse
from src.utils.session_manager import SessionManager

from .decorators import process_time
from .session_manager import SessionManager


@process_time()
def get_audiocast(session_id: str):
"""
Get audiocast based on session id
Get audiocast based on session_id
"""
try:
StorageManager().download_from_gcs(session_id)
exists = StorageManager().check_blob_exists(session_id)
if not exists:
raise
except Exception:
raise HTTPException(
status_code=404,
Expand All @@ -26,13 +28,4 @@ def get_audiocast(session_id: str):
detail=f"Audiocast not found for session_id: {session_id}",
)

metadata = session_data.metadata
source = metadata.source if metadata else ""
transcript = metadata.transcript if metadata else ""
title = metadata.title if metadata and metadata.title else "Untitled"

created_at = None
if session_data.created_at:
created_at = datetime.fromisoformat(session_data.created_at).strftime("%Y-%m-%d %H:%M")

return GenerateAudioCastResponse(script=transcript, source_content=source, created_at=created_at)
return session_data.__dict__
1 change: 1 addition & 0 deletions app/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<link rel="icon" href="%sveltekit.assets%/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes">
<meta name="theme-color" content="#000000" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />

Expand Down
8 changes: 2 additions & 6 deletions app/src/lib/components/AudiocastPageAction.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { Button } from '@/components/ui/button';
import ShareModal from './share/ShareModal.svelte';
import { getShareableLink, getShareTitle } from '@/utils/shareMeta';
import { Share2Icon } from 'lucide-svelte';
import NewAudiocastButton from './NewAudiocastButton.svelte';
export let sessionId: string;
export let sessionTitle: string;
Expand All @@ -26,9 +26,5 @@
</Button>
</ShareModal>

<Button
on:click={() => goto('/', { invalidateAll: true, replaceState: true })}
class="py-6 px-10 rounded-md text-base no-underline text-center hover:no-underline bg-emerald-600/70 text-emerald-100 hover:bg-emerald-600"
>Create Audiocast
</Button>
<NewAudiocastButton />
</div>
14 changes: 7 additions & 7 deletions app/src/lib/components/AudiocastPageHeader.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<script lang="ts">
import type { SessionModel } from '@/utils/types';
export let session: SessionModel;
import type { ContentCategory } from '@/utils/types';
export let title: string = 'Untitled';
export let category: ContentCategory;
</script>

<div class="flex w-full px-4 flex-col gap-y-3 sm:max-w-xl lg:max-w-3xl max-w-full">
<div class="flex w-full flex-col gap-y-3 sm:max-w-xl lg:max-w-3xl max-w-full">
<div class="mb-4 flex flex-col gap-y-2">
<span class="capitalize bg-gray-800 text-gray-300 w-fit py-1 px-3 rounded-md">
{session.category}
{category}
</span>

{#if session.metadata?.title}
<h1 class="text-2xl font-semibold text-sky-200">{session.metadata.title}</h1>
{/if}
<h1 class="text-2xl font-semibold text-sky-200">{title}</h1>
</div>
</div>
3 changes: 1 addition & 2 deletions app/src/lib/components/AudiocastPageSkeletonLoader.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<div class="w-full max-w-3xl mx-auto p-6 px-4">

<div class="w-full max-w-3xl mx-auto">
<!-- {/* Player container */} -->
<div class="w-full rounded-lg bg-gray-800 p-4 mb-4">
<!-- {/* Player controls and progress row */} -->
Expand Down
30 changes: 11 additions & 19 deletions app/src/lib/components/ChatListActionItems.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<script context="module">
export const FINAL_RESPONSE_PREFIX = 'Ok, thanks for clarifying!';
export const FINAL_RESPONSE_SUFFIX =
'Please click the button below to start generating the audiocast.';
</script>

<script lang="ts">
import { goto } from '$app/navigation';
import type { ContentCategory } from '@/utils/types';
Expand All @@ -19,28 +13,26 @@
export let sessionId: string;
export let category: ContentCategory;
export let content: string;
export let summary: string;
export let title: string;
const { session$, audioSource$, fetchingSource$, sessionId$, sessionCompleted$ } =
getSessionContext();
function getSummary() {
const replacePrefixRegex = new RegExp(FINAL_RESPONSE_PREFIX, 'gi');
const replaceSuffixRegex = new RegExp(FINAL_RESPONSE_SUFFIX, 'gi');
return content.replace(replacePrefixRegex, '').replace(replaceSuffixRegex, '').trim();
}
async function ongenerate(summary: string) {
session$.update((session) => {
if (!session) throw new Error('Session not found');
session.completed = true;
session.summary = summary;
return session;
});
return goto(`/audiocast/${sessionId}`, { replaceState: true });
return goto(`/audiocast/${sessionId}`, { replaceState: true }).then(() => {
session$.update((session) => {
if (!session) throw new Error('Session not found');
session.completed = true;
return session;
});
});
}
async function onreviewSource(category: ContentCategory, summary: string) {
Expand Down Expand Up @@ -77,7 +69,7 @@
body: JSON.stringify({
sessionId,
category,
summary: getSummary()
summary
}),
headers: { 'Content-Type': 'application/json' }
})
Expand Down Expand Up @@ -129,7 +121,7 @@
<div class="animate-fade-in grid sm:grid-cols-2 gap-3">
<Button
class="bg-emerald-600 text-emerald-100 text-base py-6 hover:bg-emerald-700"
on:click={() => ongenerate(getSummary())}>Generate Audiocast</Button
on:click={() => ongenerate(summary)}>Generate Audiocast</Button
>

{#if $audioSource$}
Expand All @@ -138,7 +130,7 @@
<Button
variant="ghost"
class="bg-gray-800 py-6 text-base hover:bg-gray-700 text-emerald-600 hover:text-emerald-600"
on:click={() => onreviewSource(category, getSummary())}
on:click={() => onreviewSource(category, summary)}
>
Review Source
</Button>
Expand Down
13 changes: 13 additions & 0 deletions app/src/lib/components/NewAudiocastButton.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { goto } from '$app/navigation';
import { PlusIcon } from 'lucide-svelte';
import { Button } from './ui/button';
</script>

<Button
on:click={() => goto('/', { invalidateAll: true, replaceState: true })}
class="py-6 px-10 animate-fade-in rounded-md text-base no-underline text-center hover:no-underline bg-gray-800 text-gray-200 hover:bg-gray-700"
>
<PlusIcon class="w-4 h-4 inline mr-2" />
New Audiocast
</Button>
27 changes: 1 addition & 26 deletions app/src/lib/components/RootNav.svelte
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
<script lang="ts">
import { page } from '$app/stores';
import Logo from './Logo.svelte';
import { PlusIcon } from 'lucide-svelte';
import * as Tooltip from './ui/tooltip';
import SearchSlideSheet from './SlideSheet.svelte';
import SearchSidebar from './Sidebar.svelte';
import { Button } from './ui/button';
import { getAppContext } from '$lib/stores/appContext.svelte';
import { goto, invalidateAll } from '$app/navigation';
import { invalidateAll } from '$app/navigation';
import { mediaBreakPoints$ } from '@/utils/mediaBreakpoints';
export let sessionId: string;
const { openSettingsDrawer$ } = getAppContext();
const mdAndUp = mediaBreakPoints$('md');
$: pathname = $page.url.pathname;
function generateNew() {
goto('/', { invalidateAll: true, replaceState: true });
}
</script>

<nav class="relative flex h-16 w-full items-center">
Expand Down Expand Up @@ -57,21 +49,4 @@
<Logo />
</a>
</div>

<div class="ml-12 w-full flex h-full max-h-full items-center justify-between px-3">
{#if pathname.match(/chat\/\S+/g)}
<Tooltip.Root>
<Tooltip.Trigger>
<Button
on:click={generateNew}
variant="ghost"
class="flex h-7 items-center justify-center rounded-md border border-gray-600 p-1 px-1.5 text-gray-500 transition-colors duration-200 hover:border-gray-500"
>
<PlusIcon class="h-4 w-4" />
</Button>
</Tooltip.Trigger>
<Tooltip.Content class="bg-gray-600 border-none">New Audiocast</Tooltip.Content>
</Tooltip.Root>
{/if}
</div>
</nav>
Loading

0 comments on commit edd6700

Please sign in to comment.