File size: 3,676 Bytes
50cc9f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
592cbe6
 
50cc9f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# PDF to Podcast Converter

## Overview

This project provides a tool to convert any PDF document into a podcast episode! Using OpenAI's text-to-speech models and Google Gemini, this tool processes the content of a PDF, generates a natural dialogue suitable for an audio podcast, and outputs it as an MP3 file.

## Features

- **Convert PDF to Podcast:** Upload a PDF and convert its content into a podcast dialogue.
- **Engaging Dialogue:** The generated dialogue is designed to be informative and entertaining.
- **Multiple Voice Options:** Choose from different voices to narrate the podcast.
- **User-friendly Interface:** Simple interface using Gradio for easy interaction.

## Installation

To set up the project, follow these steps:

1. **Clone the repository:**
   ```bash
   git clone https://github.com/knowsuchagency/pdf-to-podcast.git
   cd pdf-to-podcast
   ```

2. **Create a virtual environment and activate it:**
   ```bash
   python -m venv .venv
   source .venv/bin/activate
   ```

3. **Install the required packages:**
   ```bash
   pip install -r requirements.txt
   ```

## Usage

1. **Set up API Key(s):**
   Ensure you have an Google Gemini API key. You can get yours at https://aistudio.google.com/app/apikey.
   Use it as the value to `GEMINI_API_KEY`.
   You'll also need an api key for OpenAI which you can either pass through the interface or set as the `OPENAI_API_KEY` environment variable.

   Gemini flash is used as the LLM and OpenAI is used for text-to-speech.

2. **Run the application:**
   ```bash
   python main.py
   ```
   This will launch a Gradio interface in your web browser.

3. **Upload a PDF:**
   Upload the PDF document you want to convert into a podcast.

4. **Enter OpenAI API Key:**
   Provide your OpenAI API key in the designated textbox.

5. **Generate Audio:**
   Click the button to start the conversion process. The output will be an MP3 file containing the podcast dialogue.

## Project Structure

- **main.py:** Main application script.
- **requirements.txt:** List of dependencies.
- **README.md:** Project documentation (this file).

## Code Explanation

### Dialogue Models

Defines the structure of the dialogue using Pydantic models.

```python
class DialogueItem(BaseModel):
    text: str
    voice: Literal["alloy", "onyx", "fable"]

class Dialogue(BaseModel):
    scratchpad: str
    dialogue: List[DialogueItem]
```

### LLM Function

Generates dialogue based on the input text using the `promptic` decorator.

```python
@llm(model="gemini/gemini-1.5-flash")
def generate_dialogue(text: str) -> Dialogue:
    # Function to generate podcast dialogue
```

### TTS Function

Converts text to speech using OpenAI's text-to-speech model.

```python
def get_mp3(text: str, voice: str, api_key: str = None) -> bytes:
    # Function to generate MP3 from text
```

### Main Function

Processes the PDF, generates dialogue, and converts it to audio.

```python
def generate_audio(file: bytes, openai_api_key: str) -> bytes:
    # Main function to process PDF and generate audio
```

### Gradio Interface

Creates a user-friendly interface for uploading PDFs and generating podcasts.

```python
demo = gr.Interface(
    title="PDF to Podcast",
    description="Convert any PDF document into an engaging podcast episode!",
    fn=generate_audio,
    inputs=[
        gr.File(label="Input PDF", type="binary"),
        gr.Textbox(label="OpenAI API Key", placeholder="Enter your OpenAI API key here"),
    ],
    outputs=[
        gr.Audio(format="mp3"),
    ],
)

demo.launch(show_api=False)
```

## License

This project is licensed under the Apache 2.0 License. See the [LICENSE](LICENSE) file for more information.