From 2e2148b98e48aad4777a606cd8501b4a779c5d53 Mon Sep 17 00:00:00 2001 From: Stephan Fitzpatrick Date: Sat, 8 Jun 2024 20:55:51 -0700 Subject: [PATCH] dockerize --- .dockerignore | 162 +++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 13 ++++ docker-compose.yml | 8 +++ main.py | 40 +++++------ 4 files changed, 201 insertions(+), 22 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..82f9275 --- /dev/null +++ b/.dockerignore @@ -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/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..93a7e20 --- /dev/null +++ b/Dockerfile @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..15e78a7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +services: + web: + build: . + ports: + - "8080:7860" + environment: + GEMINI_API_KEY: ${GEMINI_API_KEY} + diff --git a/main.py b/main.py index db7cfcc..b2d875d 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import io +import os from typing import List, Literal import gradio as gr @@ -9,8 +10,6 @@ from pypdf import PdfReader - - class DialogueItem(BaseModel): text: str voice: Literal["alloy", "onyx", "fable"] @@ -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", @@ -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]) @@ -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) @@ -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, +)