diff --git a/final_project/DockerFile/Dockerfile b/final_project/DockerFile/Dockerfile new file mode 100644 index 000000000..9d0d53b8d --- /dev/null +++ b/final_project/DockerFile/Dockerfile @@ -0,0 +1,7 @@ +FROM python:alpine3.7 +COPY . /app +WORKDIR /app +RUN pip install -r requirements.txt +EXPOSE 8080 +ENTRYPOINT [ "python" ] +CMD [ "server.py" ] \ No newline at end of file diff --git a/final_project/DockerFile/requirements.txt b/final_project/DockerFile/requirements.txt new file mode 100755 index 000000000..caed67c0a --- /dev/null +++ b/final_project/DockerFile/requirements.txt @@ -0,0 +1,6 @@ +Flask +Flask-WTF +ibm-watson +python-dotenv +pyJWT +deep_translator diff --git a/final_project/machinetranslation/__init__.py b/final_project/machinetranslation/__init__.py new file mode 100644 index 000000000..67ce7b73e --- /dev/null +++ b/final_project/machinetranslation/__init__.py @@ -0,0 +1 @@ +from . import translator \ No newline at end of file diff --git a/final_project/machinetranslation/test_translator.py b/final_project/machinetranslation/test_translator.py new file mode 100644 index 000000000..049598f49 --- /dev/null +++ b/final_project/machinetranslation/test_translator.py @@ -0,0 +1,26 @@ +import unittest +from translator import english_to_french, french_to_english + +class TestEF(unittest.TestCase): + def test1(self): + # Test Hello returns Bonjour + self.assertEqual(english_to_french('Hello'), 'Bonjour') + # Test Hello does not return Hello + self.assertNotEqual(english_to_french('Hello'), 'Hello') + # Test None returns empty string + self.assertNotEqual(english_to_french("None"), '') + # Test empty string returns empty string + self.assertNotEqual(english_to_french(0), 0) + +class TestFE(unittest.TestCase): + def test1(self): + # Test Hello returns Bonjour + self.assertEqual(french_to_english('Bonjour'), 'Hello') + # Test Hello does not return Hello + self.assertNotEqual(french_to_english('Bonjour'), 'Bonjour') + # Test None returns empty string + self.assertNotEqual(french_to_english("None"), '') + # Test empty string returns empty string + self.assertNotEqual(french_to_english(0), 0) + +unittest.main() diff --git a/final_project/machinetranslation/translator.py b/final_project/machinetranslation/translator.py new file mode 100644 index 000000000..5537bbc8b --- /dev/null +++ b/final_project/machinetranslation/translator.py @@ -0,0 +1,23 @@ +import json +import os +from ibm_watson import LanguageTranslatorV3 +from ibm_cloud_sdk_core.authenticators import IAMAuthenticator + +authenticator = IAMAuthenticator('nA_3AG9jCGWTOl-o5nXCoyGkZYNGMZeLhRP_ULwl4RW8') +language_translator = LanguageTranslatorV3( + version='2018-05-01', + authenticator=authenticator + ) +language_translator.set_service_url('https://api.us-south.language-translator.watson.cloud.ibm.com/instances/9ac5757f-278e-448e-99d6-cb25d97fdb66') + +def english_to_french(english_text): + '''English to French''' + translation = language_translator.translate(text=english_text, model_id='en-fr').get_result() + french_text = translation['translations'][0]['translation'] + return french_text + +def french_to_english(french_text): + '''French to English''' + translation = language_translator.translate(text=french_text, model_id='fr-en').get_result() + english_text = translation['translations'][0]['translation'] + return english_text diff --git a/final_project/requirements.txt b/final_project/requirements.txt deleted file mode 100755 index f341673c2..000000000 --- a/final_project/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -Flask==1.1.2 -Flask-WTF==0.14.3 -ibm-watson==4.4.1 -python-dotenv==0.13.0 -pyJWT==1.7.1 -deep_translator==1.11.1 diff --git a/final_project/server.py b/final_project/server.py index a776f7ae3..2d8abd3fe 100755 --- a/final_project/server.py +++ b/final_project/server.py @@ -1,23 +1,27 @@ from machinetranslation import translator from flask import Flask, render_template, request import json +import machinetranslation app = Flask("Web Translator") @app.route("/englishToFrench") def englishToFrench(): textToTranslate = request.args.get('textToTranslate') - # Write your code here + french_text= translator.english_to_french(textToTranslate) + return french_text return "Translated text to French" @app.route("/frenchToEnglish") def frenchToEnglish(): textToTranslate = request.args.get('textToTranslate') - # Write your code here + english_text= translator.french_to_english(textToTranslate) + return english_text return "Translated text to English" @app.route("/") def renderIndexPage(): + return render_template('index.html') # Write the code to render template if __name__ == "__main__":