Skip to content

Commit

Permalink
dockerize
Browse files Browse the repository at this point in the history
  • Loading branch information
knowsuchagency committed Jun 9, 2024
1 parent 672cb3f commit 2e2148b
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 22 deletions.
162 changes: 162 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12-slim

RUN pip install uv
RUN uv venv

COPY requirements.txt .
RUN uv pip install -r requirements.txt

COPY . .

ENV GRADIO_SERVER_NAME="0.0.0.0"

CMD .venv/bin/python main.py
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
web:
build: .
ports:
- "8080:7860"
environment:
GEMINI_API_KEY: ${GEMINI_API_KEY}

40 changes: 18 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import os
from typing import List, Literal

import gradio as gr
Expand All @@ -9,8 +10,6 @@
from pypdf import PdfReader




class DialogueItem(BaseModel):
text: str
voice: Literal["alloy", "onyx", "fable"]
Expand Down Expand Up @@ -47,8 +46,11 @@ def generate_dialogue(text: str) -> Dialogue:
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.
"""

def get_mp3(text: str, voice: str) -> bytes:
client = OpenAI()

def get_mp3(text: str, voice: str, api_key: str = None) -> bytes:
client = OpenAI(
api_key=api_key or os.getenv("OPENAI_API_KEY"),
)

with client.audio.speech.with_streaming_response.create(
model="tts-1",
Expand All @@ -61,9 +63,8 @@ def get_mp3(text: str, voice: str) -> bytes:
return file.getvalue()


def generate_audio(file: bytes) -> bytes:
def generate_audio(file: bytes, openai_api_key: str) -> bytes:

# Read the PDF file
reader = PdfReader(io.BytesIO(file))
text = "\n\n".join([page.extract_text() for page in reader.pages])

Expand All @@ -73,12 +74,11 @@ def generate_audio(file: bytes) -> bytes:
result = b""
characters = 0

# Generate and play the dialogue
for line in llm_output.dialogue:
logger.info(line.text)
logger.info(line.voice)

audio = get_mp3(line.text, line.voice)
audio = get_mp3(line.text, line.voice, openai_api_key)
result += audio
characters += len(line.text)

Expand All @@ -88,28 +88,24 @@ def generate_audio(file: bytes) -> bytes:


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="Input Text",
# placeholder="Enter text here",
# ),
# gr.Textbox(
# label="Male Voice",
# value="1m3E2x7boso3AU9J3woJ",
# ),
# gr.Textbox(
# label="Female Voice",
# value="uCGnCVg8g9Lwl9wocoHE",
# ),
),
gr.Textbox(
label="OpenAI API Key",
placeholder="Enter your OpenAI API key here",
),
],
outputs=[
gr.Audio(format="mp3"),
],
)

demo.launch()
demo.launch(
show_api=False,
)

0 comments on commit 2e2148b

Please sign in to comment.