knowsuchagency commited on
Commit
2e2148b
1 Parent(s): 672cb3f
Files changed (4) hide show
  1. .dockerignore +162 -0
  2. Dockerfile +13 -0
  3. docker-compose.yml +8 -0
  4. main.py +18 -22
.dockerignore ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
110
+ .pdm.toml
111
+ .pdm-python
112
+ .pdm-build/
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
156
+
157
+ # PyCharm
158
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
161
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
+ #.idea/
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ RUN pip install uv
4
+ RUN uv venv
5
+
6
+ COPY requirements.txt .
7
+ RUN uv pip install -r requirements.txt
8
+
9
+ COPY . .
10
+
11
+ ENV GRADIO_SERVER_NAME="0.0.0.0"
12
+
13
+ CMD .venv/bin/python main.py
docker-compose.yml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ web:
3
+ build: .
4
+ ports:
5
+ - "8080:7860"
6
+ environment:
7
+ GEMINI_API_KEY: ${GEMINI_API_KEY}
8
+
main.py CHANGED
@@ -1,4 +1,5 @@
1
  import io
 
2
  from typing import List, Literal
3
 
4
  import gradio as gr
@@ -9,8 +10,6 @@ from pydantic import BaseModel
9
  from pypdf import PdfReader
10
 
11
 
12
-
13
-
14
  class DialogueItem(BaseModel):
15
  text: str
16
  voice: Literal["alloy", "onyx", "fable"]
@@ -47,8 +46,11 @@ def generate_dialogue(text: str) -> Dialogue:
47
  Write your engaging, informative podcast dialogue based on the key points and creative ideas you came up with during the brainstorming session. Use a conversational tone and include any necessary context or explanations to make the content accessible to a general audience.
48
  """
49
 
50
- def get_mp3(text: str, voice: str) -> bytes:
51
- client = OpenAI()
 
 
 
52
 
53
  with client.audio.speech.with_streaming_response.create(
54
  model="tts-1",
@@ -61,9 +63,8 @@ def get_mp3(text: str, voice: str) -> bytes:
61
  return file.getvalue()
62
 
63
 
64
- def generate_audio(file: bytes) -> bytes:
65
 
66
- # Read the PDF file
67
  reader = PdfReader(io.BytesIO(file))
68
  text = "\n\n".join([page.extract_text() for page in reader.pages])
69
 
@@ -73,12 +74,11 @@ def generate_audio(file: bytes) -> bytes:
73
  result = b""
74
  characters = 0
75
 
76
- # Generate and play the dialogue
77
  for line in llm_output.dialogue:
78
  logger.info(line.text)
79
  logger.info(line.voice)
80
 
81
- audio = get_mp3(line.text, line.voice)
82
  result += audio
83
  characters += len(line.text)
84
 
@@ -88,28 +88,24 @@ def generate_audio(file: bytes) -> bytes:
88
 
89
 
90
  demo = gr.Interface(
 
 
91
  fn=generate_audio,
92
  inputs=[
93
  gr.File(
94
  label="Input PDF",
95
  type="binary",
96
- )
97
- # gr.Textbox(
98
- # label="Input Text",
99
- # placeholder="Enter text here",
100
- # ),
101
- # gr.Textbox(
102
- # label="Male Voice",
103
- # value="1m3E2x7boso3AU9J3woJ",
104
- # ),
105
- # gr.Textbox(
106
- # label="Female Voice",
107
- # value="uCGnCVg8g9Lwl9wocoHE",
108
- # ),
109
  ],
110
  outputs=[
111
  gr.Audio(format="mp3"),
112
  ],
113
  )
114
 
115
- demo.launch()
 
 
 
1
  import io
2
+ import os
3
  from typing import List, Literal
4
 
5
  import gradio as gr
 
10
  from pypdf import PdfReader
11
 
12
 
 
 
13
  class DialogueItem(BaseModel):
14
  text: str
15
  voice: Literal["alloy", "onyx", "fable"]
 
46
  Write your engaging, informative podcast dialogue based on the key points and creative ideas you came up with during the brainstorming session. Use a conversational tone and include any necessary context or explanations to make the content accessible to a general audience.
47
  """
48
 
49
+
50
+ def get_mp3(text: str, voice: str, api_key: str = None) -> bytes:
51
+ client = OpenAI(
52
+ api_key=api_key or os.getenv("OPENAI_API_KEY"),
53
+ )
54
 
55
  with client.audio.speech.with_streaming_response.create(
56
  model="tts-1",
 
63
  return file.getvalue()
64
 
65
 
66
+ def generate_audio(file: bytes, openai_api_key: str) -> bytes:
67
 
 
68
  reader = PdfReader(io.BytesIO(file))
69
  text = "\n\n".join([page.extract_text() for page in reader.pages])
70
 
 
74
  result = b""
75
  characters = 0
76
 
 
77
  for line in llm_output.dialogue:
78
  logger.info(line.text)
79
  logger.info(line.voice)
80
 
81
+ audio = get_mp3(line.text, line.voice, openai_api_key)
82
  result += audio
83
  characters += len(line.text)
84
 
 
88
 
89
 
90
  demo = gr.Interface(
91
+ title="PDF to Podcast",
92
+ description="Convert any PDF document into an engaging podcast episode!",
93
  fn=generate_audio,
94
  inputs=[
95
  gr.File(
96
  label="Input PDF",
97
  type="binary",
98
+ ),
99
+ gr.Textbox(
100
+ label="OpenAI API Key",
101
+ placeholder="Enter your OpenAI API key here",
102
+ ),
 
 
 
 
 
 
 
 
103
  ],
104
  outputs=[
105
  gr.Audio(format="mp3"),
106
  ],
107
  )
108
 
109
+ demo.launch(
110
+ show_api=False,
111
+ )