Skip to content

Commit

Permalink
Merge pull request #12 from nakajima-john-shotaro/fix/docker
Browse files Browse the repository at this point in the history
Fix/docker
  • Loading branch information
nakajima-john-shotaro authored Sep 3, 2021
2 parents 9b2e256 + aef0008 commit 9e9f8f9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
84 changes: 84 additions & 0 deletions aicon/backend/runme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import time
import json
import urllib
import requests

import chromedriver_binary
import pyperclip
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from flask import Flask, jsonify
from flask_cors import CORS
from bs4 import BeautifulSoup
from googletrans import Translator


class Translation(object):
def __init__(
self,
translator: str = "google"
) -> None:
super().__init__()

if translator in ["google", "deepl"]:
self.translator: str = translator
self.g_translator: Translator = Translator()
self.d_options = Options()
else:
raise ValueError("Expected translator is `google` or `deepl` but got {translator}")

def translate(self, text: str) -> str:
if self.translator == "google":
lang = self.g_translator.detect(text).lang

if not lang == "en":
output_text = self.g_translator.translate(text, src=lang, dest="en").text
else:
output_text = text
print(output_text)

return output_text
else:
self.d_options.add_argument('--headless')
self.d_options.add_argument('--no-sandbox')
driver: webdriver.Chrome = webdriver.Chrome(chrome_options=self.d_options)
driver.get("https://www.deepl.com/ja/translator")
input_selector = driver.find_element_by_css_selector(".lmt__textarea.lmt__source_textarea.lmt__textarea_base_style")
input_selector.send_keys(text)

time.sleep(3)

while True:
output_selector: str = ".lmt__textarea.lmt__target_textarea.lmt__textarea_base_style"
output_text: str = driver.find_element_by_css_selector(output_selector).get_property("value")
if output_text != "" :
break
time.sleep(1)
print(output_text)

driver.close()

return output_text


class AIcon(object):
def __init__(
self
) -> None:
super().__init__()

app: Flask = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
app.config["JSON_SORT_KEYS"] = False
CORS(app)

self.translator: Translation = Translation("deepl")


# def main() -> None:
# app.run(host="0.0.0.0", port=8081, threaded=False)


if __name__ == "__main__":
translator = Translation("deepl")
translator.translate("東京特許許可局")
34 changes: 34 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
apt-get install -y \
sudo \
wget \
curl \
unzip \
apt-utils \
libfontconfig \
libappindicator1 \
fonts-liberation \
libasound2 \
libnspr4 \
libnss3 \
libxss1 \
lsb-release \
xdg-utils \
libfreetype6-dev \
libxft-dev \
libjpeg62 \
Expand All @@ -23,6 +36,27 @@ RUN apt-get update && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

RUN mkdir -p /home/root/src && cd $_ && \
touch /etc/default/google-chrome && \
wget -O /tmp/chromedriver.zip \
http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip && \
unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ && \
apt-get update && \
apt-get install -y \
libgconf-2-4 \
libnss3-dev \
libxss1 && \
apt-get --fix-broken install && \
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
apt-get install -y ./google-chrome-stable_current_amd64.deb

RUN pip3 install \
flask-cors \
bs4 \
chromedriver-binary \
selenium \
googletrans==4.0.0-rc1

RUN locale-gen ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
Expand Down
2 changes: 1 addition & 1 deletion docker/build-docker.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION="0.0.1"
VERSION="0.0.2"

IMAGE_NAME="aicon:${VERSION}"
BUILD_DIR=$(dirname $(readlink -f $0))
Expand Down
2 changes: 1 addition & 1 deletion docker/run-docker.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
HOST_SD=$(dirname $(dirname $(readlink -f $0)))/aicon

VERSION=0.0.1
VERSION=0.0.2

IMAGE_NAME="aicon:${VERSION}"

Expand Down

0 comments on commit 9e9f8f9

Please sign in to comment.