Spaces:
Runtime error
Runtime error
add new files for docker and fast api
Browse filesFiles changed (11)
- .vscode/settings.jsonAdded+16
- DockerfileAdded+38
- LICENSEAdded+21
- app.py+9-21
- docker-compose.ymlAdded+29
- main.py+42-34
- run.pyAdded+9
- static/css/style.cssAdded+64
- templates/error.htmlAdded+11
- templates/home.htmlAdded+11
- tox.iniAdded+5
12345678910111213141516{ "python.linting.pylint" : false, "python.linting.flake8" : true, "python.linting.enabled" : true, "python.formatting.black" : true, "editor.formatOnSave": true, "python.linting.flake8Args":[ "--max-line-length=88" ], "python.testing.unittestEnabled": false, "python.testing.pytestEnabled": true, "python.testing.autoTestDiscoverOnSaveEnabled": true, "python.testing.pytestArgs": [ "tests" ],}1234567891011121314151617181920212223242526272829303132333435363738FROM python:3.10-slim as builder
# set environment variablesENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONFAULTHANDLER=1 \ PYTHONUNBUFFERED=1ENV POETRY_NO_INTERACTION=1 \ POETRY_VIRTUALENVS_IN_PROJECT=1 \ POETRY_VIRTUALENVS_CREATE=1 \ POETRY_CACHE_DIR=/tmp/poetry_cache
RUN mkdir -p /appWORKDIR /appRUN pip install poetryCOPY poetry.lock pyproject.toml ./RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --no-root
FROM python:3.10-slim as base
ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONFAULTHANDLER=1 \ PYTHONUNBUFFERED=1
RUN apt-get update && apt-get install -y curlWORKDIR /appENV VIRTUAL_ENV=/app/.venv \ PATH="/app/.venv/bin:$PATH"
COPY --from=builder /app/.venv /app/.venv
# Necessary FilesCOPY main.py app.py /app/
# Expose portEXPOSE 8000
CMD ["uvicorn","main:app","--proxy-headers","--host","0.0.0.0","--port","8000","--workers","3"]123456789101112131415161718192021MIT License
Copyright (c) 2023 Alexander Frantsev
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.74 unmodified lines7576777879808182838426 unmodified lines1111121131141151161171181191201211221231241251261271281291301311321335 unmodified lines13914014114214314416 unmodified lines16116216316416516616716816917017117274 unmodified linesself.ru_sentiment_pipe = pipeline("sentiment-analysis", model=RU_SENTIMENT_MODEL)self.en_summary_pipe = sum_pipeself.en_sentiment_pipe = pipeline("sentiment-analysis", model=EN_SENTIMENT_MODEL)def mT5_summarize(self, text: str) -> str:WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))input_ids = self.sum_tokenizer(26 unmodified linessentiment = {'en': self.en_sentiment_pipe,'ru': self.ru_sentiment_pipe,}return summary[lang], sentiment[lang]# def summarize(self, text: str, lang: str = 'en') -> str:# result = {}# sum_pipe, sent_pipe = self.get_pipe(lang)# response_summary = sum_pipe(text)# logger.info(response_summary)# result["summary"] = response_summary[0]["summary_text"]# response_sentiment = sent_pipe(text)# logger.info(response_sentiment)# result["sentiment_label"] = response_sentiment[0]["label"]# result["sentiment_score"] = response_sentiment[0]["score"]# return f"Summary: {result['summary']}\n Sentiment: {result['sentiment_label']} ({result['sentiment_score']:.3f})"def summarize(self, text: Request, lang: str = 'en') -> str:sum_pipe, sent_pipe = self.get_pipe(lang)response_summary = sum_pipe(text)logger.info(response_summary)5 unmodified linessentiment_score=response_sentiment[0]["score"],)return resultif __name__ == "__main__":pipe = Summarizer()16 unmodified linesru_inbtn = gr.Button("Запустить")en_inbtn.click(pipe.summarize.to_str(),[en_inputs, en_lang],[en_outputs],)ru_inbtn.click(pipe.summarize.to_str(),[ru_inputs, ru_lang],[ru_outputs],)74 unmodified lines7576777879808182838426 unmodified lines1111121131141151161171185 unmodified lines12412512612712812913013113216 unmodified lines14915015115215315415515615715815916074 unmodified linesself.ru_sentiment_pipe = pipeline("sentiment-analysis", model=RU_SENTIMENT_MODEL)self.en_summary_pipe = sum_pipeself.en_sentiment_pipe = pipeline("sentiment-analysis", model=EN_SENTIMENT_MODEL)def mT5_summarize(self, text: str) -> str:'''Handle text with mT5 model without pipeline'''WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))input_ids = self.sum_tokenizer(26 unmodified linessentiment = {'en': self.en_sentiment_pipe,'ru': self.ru_sentiment_pipe,}return summary[lang], sentiment[lang]def summarize(self, text: Request, lang: str = 'en') -> Result:sum_pipe, sent_pipe = self.get_pipe(lang)response_summary = sum_pipe(text)logger.info(response_summary)5 unmodified linessentiment_score=response_sentiment[0]["score"],)return resultdef summ(self, text: Request, lang: str = 'en') -> str:return self.summarize(text, lang).to_str()if __name__ == "__main__":pipe = Summarizer()16 unmodified linesru_inbtn = gr.Button("Запустить")en_inbtn.click(pipe.summ,[en_inputs, en_lang],[en_outputs],)ru_inbtn.click(pipe.summ,[ru_inputs, ru_lang],[ru_outputs],)
1234567891011121314151617181920212223242526272829version: '3.8'services: app: container_name: summary-app restart: unless-stopped # env_file: # - .env # environment: # PGHOST: 'db' # PGDATABASE: ${DATABASE_NAME} # PGUSER: ${DATABASE_USER} # PGPASSWORD: ${DATABASE_PASS} build: context: . dockerfile: Dockerfile ports: - "8000:8000" expose: - 8000 healthcheck: test: curl --fail -s http://localhost:8000/ || exit 1 interval: 10s timeout: 5s retries: 3 start_period: 10s command: ["uvicorn","main:app","--proxy-headers","--host","0.0.0.0","--port","8000","--workers","3"]
7 unmodified lines8910111213141516171819202122232425262728293031323334353637383940414243444546477 unmodified linesapp = FastAPI()pipe = Summarizer()@app.post("/summ_ru", response_model=Result)async def ru_summ_api(request: Request):results = pipe.summarize(request.text)return resultsif __name__ == "__main__":with gr.Blocks() as demo:with gr.Row():with gr.Column(scale=2, min_width=600):en_sum_description=gr.Markdown(value=f"Model for Summary: {EN_SUMMARY_MODEL}")en_sent_description=gr.Markdown(value=f"Model for Sentiment: {EN_SENTIMENT_MODEL}")en_inputs=gr.Textbox(label="en_input", lines=5, value=DEFAULT_EN_TEXT, placeholder=DEFAULT_EN_TEXT)en_lang=gr.Textbox(value='en',visible=False)en_outputs=gr.Textbox(label="en_output", lines=5, placeholder="Summary and Sentiment would be here...")en_inbtn = gr.Button("Proceed")with gr.Column(scale=2, min_width=600):ru_sum_description=gr.Markdown(value=f"Model for Summary: {RU_SUMMARY_MODEL}")ru_sent_description=gr.Markdown(value=f"Model for Sentiment: {RU_SENTIMENT_MODEL}")ru_inputs=gr.Textbox(label="ru_input", lines=5, value=DEFAULT_RU_TEXT, placeholder=DEFAULT_RU_TEXT)ru_lang=gr.Textbox(value='ru',visible=False)ru_outputs=gr.Textbox(label="ru_output", lines=5, placeholder="Здесь будет обобщение и эмоциональный окрас текста...")ru_inbtn = gr.Button("Запустить")en_inbtn.click(pipe.summarize.to_str(),[en_inputs, en_lang],[en_outputs],)ru_inbtn.click(pipe.summarize.to_str(),[ru_inputs, ru_lang],[ru_outputs],)demo.launch(show_api=False)7 unmodified lines89101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354557 unmodified linesapp = FastAPI()pipe = Summarizer()@app.post("/summ_ru", response_model=Result)async def ru_summ_api(request: Request):results = pipe.summarize(request.text, lang='ru')return results@app.post("/summ_en", response_model=Result)async def ru_summ_api(request: Request):results = pipe.summarize(request.text, lang='en')return resultswith gr.Blocks() as demo:with gr.Row():with gr.Column(scale=2, min_width=600):en_sum_description=gr.Markdown(value=f"Model for Summary: {EN_SUMMARY_MODEL}")en_sent_description=gr.Markdown(value=f"Model for Sentiment: {EN_SENTIMENT_MODEL}")en_inputs=gr.Textbox(label="en_input", lines=5, value=DEFAULT_EN_TEXT, placeholder=DEFAULT_EN_TEXT)en_lang=gr.Textbox(value='en',visible=False)en_outputs=gr.Textbox(label="en_output", lines=5, placeholder="Summary and Sentiment would be here...")en_inbtn = gr.Button("Proceed")with gr.Column(scale=2, min_width=600):ru_sum_description=gr.Markdown(value=f"Model for Summary: {RU_SUMMARY_MODEL}")ru_sent_description=gr.Markdown(value=f"Model for Sentiment: {RU_SENTIMENT_MODEL}")ru_inputs=gr.Textbox(label="ru_input", lines=5, value=DEFAULT_RU_TEXT, placeholder=DEFAULT_RU_TEXT)ru_lang=gr.Textbox(value='ru',visible=False)ru_outputs=gr.Textbox(label="ru_output", lines=5, placeholder="Здесь будет обобщение и эмоциональный окрас текста...")ru_inbtn = gr.Button("Запустить")en_inbtn.click(pipe.summ,[en_inputs, en_lang],[en_outputs],)ru_inbtn.click(pipe.summ,[ru_inputs, ru_lang],[ru_outputs],)# demo.launch(show_api=False)# mounting at the root pathapp = gr.mount_gradio_app(app, demo, path="/")
123456789import uvicorn
if __name__ == "__main__": uvicorn.run( app="main:app", host="localhost", port=8000, reload=True )12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; font-family: Arial, sans-serif;}
.container { height: 50vh; width: 25vw; display: flex; padding-block: 2rem; justify-content: space-between; flex-direction: column; align-items: center; border: 1px solid #47616c; border-radius: 12px;}
.header { display: flex; flex-direction: column; align-items: center;}
.img-box { width: 70px; height: 70px; border-radius: 50%; overflow: hidden;}
.img { width: 100%;}
.google-btn { display: flex; align-items: center; background-color: #47616c; color: white; width: 200px; height: 50px; border-radius: 5px; box-shadow: 0px 3px 10px -2px rgba(0, 0, 0, 0.15); overflow: hidden; padding-inline: 8px; text-decoration: none;}
.google-icon { background: url("https://upload.wikimedia.org/wikipedia/commons/5/53/Google_%22G%22_Logo.svg") transparent 5px 50% no-repeat; display: inline-block; vertical-align: middle; width: 35px; height: 50px;}
.btn-text { margin-left: 10px;}1234567891011<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Error</title> </head> <body> <h2>{{error}}</h2> </body></html>1234567891011<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"/> <link rel="stylesheet" href="{{ url_for('static',path='/css/style.css')}}"/> <title>Home Page</title> </head> <body> <h1>Welcome!</h1> </body></html>12345[flake8]exclude = .git, __pycache__, env, .venvmax-line-length = 88max-complexity = 18extend-ignore = E203, E266, E501, W503.vscode/settings.jsonAdded+16
DockerfileAdded+38
LICENSEAdded+21
app.py+9-21
docker-compose.ymlAdded+29
main.py+42-34
run.pyAdded+9
static/css/style.cssAdded+64
templates/error.htmlAdded+11
templates/home.htmlAdded+11
tox.iniAdded+5