File size: 3,232 Bytes
9c20b4e
 
 
 
 
8bf1635
2e2148b
8bf1635
74e9bb4
8bf1635
9c20b4e
5c2ba64
9c20b4e
a9922ff
5e89640
 
5c2ba64
9c20b4e
672cb3f
9c20b4e
5e89640
9c20b4e
 
 
311d9aa
5e89640
 
 
 
a9922ff
5c2ba64
9c20b4e
07ea011
9c20b4e
 
5c2ba64
 
 
9c20b4e
 
5c2ba64
9c20b4e
5c2ba64
 
 
9c20b4e
 
 
331caac
 
 
5c2ba64
9c20b4e
 
 
8729c75
9c20b4e
 
fb76d6c
9c20b4e
fb76d6c
9c20b4e
 
 
 
 
ffbd52c
9c20b4e
 
 
 
 
5c2ba64
9c20b4e
 
5c2ba64
9c20b4e
1a8723f
 
 
 
 
 
 
 
9c20b4e
1a8723f
 
 
 
 
 
9c20b4e
 
1a8723f
5c2ba64
74e9bb4
5c2ba64
9c20b4e
 
 
5c2ba64
672cb3f
db8ccb7
2e2148b
5c2ba64
 
fb76d6c
 
5c2ba64
3cf1f43
5e89640
5c2ba64
 
5e89640
 
 
 
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
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
"""
main.py
"""

# Standard library imports
import glob
import os
import time
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import List, Literal, Tuple

# Third-party imports
import gradio as gr
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from loguru import logger
from pydantic import BaseModel
from pypdf import PdfReader
from pydub import AudioSegment

# Local imports
from prompts import SYSTEM_PROMPT
from utils import generate_script, generate_audio

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")


class DialogueItem(BaseModel):
    """A single dialogue item."""

    speaker: Literal["Host (Jane)", "Guest"]
    text: str


class Dialogue(BaseModel):
    """The dialogue between the host and guest."""

    scratchpad: str
    participants: List[str]
    dialogue: List[DialogueItem]


def generate_podcast(file: str) -> Tuple[str, str]:
    """Generate the audio and transcript from the PDF."""
    # Read the PDF file and extract text
    with Path(file).open("rb") as f:
        reader = PdfReader(f)
        text = "\n\n".join([page.extract_text() for page in reader.pages])

    # Call the LLM
    llm_output = generate_script(SYSTEM_PROMPT, text, Dialogue)
    logger.info(f"Generated dialogue: {llm_output}")

    # Process the dialogue
    audio_segments = []
    transcript = ""
    total_characters = 0

    for line in llm_output.dialogue:
        logger.info(f"Generating audio for {line.speaker}: {line.text}")
        transcript_line = f"{line.speaker}: {line.text}"
        transcript += transcript_line + "\n\n"
        total_characters += len(line.text)

        # Get audio file path
        audio_file_path = generate_audio(line.text, line.speaker)
        # Read the audio file into an AudioSegment
        audio_segment = AudioSegment.from_file(audio_file_path)
        audio_segments.append(audio_segment)

    # Concatenate all audio segments
    combined_audio = sum(audio_segments)

    # Export the combined audio to a temporary file
    temporary_directory = "./gradio_cached_examples/tmp/"
    os.makedirs(temporary_directory, exist_ok=True)

    temporary_file = NamedTemporaryFile(
        dir=temporary_directory,
        delete=False,
        suffix=".mp3",
    )
    combined_audio.export(temporary_file.name, format="mp3")

    # Delete any files in the temp directory that end with .mp3 and are over a day old
    for file in glob.glob(f"{temporary_directory}*.mp3"):
        if os.path.isfile(file) and time.time() - os.path.getmtime(file) > 24 * 60 * 60:
            os.remove(file)

    logger.info(f"Generated {total_characters} characters of audio")

    return temporary_file.name, transcript


demo = gr.Interface(
    title="OpenPodcast",
    description="Convert your PDFs into podcasts with open-source AI models.",
    fn=generate_podcast,
    inputs=[
        gr.File(
            label="PDF",
        ),
    ],
    outputs=[
        gr.Audio(label="Audio", format="mp3"),
        gr.Textbox(label="Transcript"),
    ],
    allow_flagging="never",
    api_name=False,
)

app = gr.mount_gradio_app(app, demo, path="/")

if __name__ == "__main__":
    demo.launch(show_api=False)