├── final_project ├── runtime.txt ├── Procfile ├── machinetranslation │ ├── __init__.py │ ├── tests │ │ └── tests.py │ └── translator.py ├── manifest.yml ├── requirements.txt ├── server.py ├── templates │ └── index.html └── static │ └── mywebscript.js └── README.md /final_project/runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.9.4 -------------------------------------------------------------------------------- /final_project/Procfile: -------------------------------------------------------------------------------- 1 | web: python3 server.py -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IBM-AI 2 | Language Translator Project 3 | -------------------------------------------------------------------------------- /final_project/machinetranslation/__init__.py: -------------------------------------------------------------------------------- 1 | from machinetranslation import translator -------------------------------------------------------------------------------- /final_project/manifest.yml: -------------------------------------------------------------------------------- 1 | --- 2 | applications: 3 | - name: ibmlearner_translation_app 4 | random-random: true 5 | memory: 64M 6 | -------------------------------------------------------------------------------- /final_project/requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==1.1.2 2 | Flask-WTF==0.14.3 3 | ibm-watson==4.4.1 4 | python-dotenv==0.13.0 5 | pyJWT==1.7.1 6 | -------------------------------------------------------------------------------- /final_project/machinetranslation/tests/tests.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from translator import english_to_french, french_to_english 3 | 4 | class TestEnglishToFrench(unittest.TestCase): 5 | def test_english_to_french(self): 6 | self.assertEqual(english_to_french(''), 'No input') 7 | self.assertEqual(english_to_french('Hello'), 'Bonjour') 8 | self.assertNotEqual(english_to_french('Yo'), 'Bonjour') 9 | 10 | class TestFrenchToEnglish(unittest.TestCase): 11 | def test_french_to_english(self): 12 | self.assertEqual(french_to_english(''), 'No input') 13 | self.assertEqual(french_to_english('Bonjour'), 'Hello') 14 | self.assertNotEqual(french_to_english('Bonjour'), 'Yo') 15 | -------------------------------------------------------------------------------- /final_project/server.py: -------------------------------------------------------------------------------- 1 | from machinetranslation import translator 2 | from flask import Flask, render_template, request 3 | import json 4 | 5 | app = Flask("Web Translator") 6 | 7 | @app.route("/englishToFrench") 8 | def englishToFrench(): 9 | textToTranslate = request.args.get('textToTranslate') 10 | # Write your code here 11 | french_text = translator.english_to_french(textToTranslate) 12 | return french_text 13 | 14 | @app.route("/frenchToEnglish") 15 | def frenchToEnglish(): 16 | textToTranslate = request.args.get('textToTranslate') 17 | # Write your code here 18 | english_text = translator.french_to_english(textToTranslate) 19 | return english_text 20 | 21 | @app.route("/") 22 | def renderIndexPage(): 23 | # Write the code to render template 24 | return render_template('index.html') 25 | 26 | if __name__ == "__main__": 27 | app.run(host="0.0.0.0", port=8080) 28 | 29 | -------------------------------------------------------------------------------- /final_project/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 |
7 | 8 | 9 |
10 | 11 |
12 | 13 | 14 |
15 | 16 |
17 |
18 |
19 | -------------------------------------------------------------------------------- /final_project/static/mywebscript.js: -------------------------------------------------------------------------------- 1 | let translateToFrench = ()=>{ 2 | textToTranslate = document.getElementById("textToTranslate").value; 3 | 4 | let xhttp = new XMLHttpRequest(); 5 | xhttp.onreadystatechange = function() { 6 | if (this.readyState == 4 && this.status == 200) { 7 | document.getElementById("translated_text").innerHTML = xhttp.responseText; 8 | } 9 | }; 10 | xhttp.open("GET", "englishToFrench?textToTranslate"+"="+textToTranslate, true); 11 | xhttp.send(); 12 | } 13 | 14 | let translateToEnglish = ()=>{ 15 | textToTranslate = document.getElementById("textToTranslate").value; 16 | 17 | let xhttp = new XMLHttpRequest(); 18 | xhttp.onreadystatechange = function() { 19 | if (this.readyState == 4 && this.status == 200) { 20 | document.getElementById("translated_text").innerHTML = xhttp.responseText; 21 | } 22 | }; 23 | xhttp.open("GET", "frenchToEnglish?textToTranslate"+"="+textToTranslate, true); 24 | xhttp.send(); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /final_project/machinetranslation/translator.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | from ibm_watson import LanguageTranslatorV3 4 | from ibm_cloud_sdk_core.authenticators import IAMAuthenticator 5 | from dotenv import load_dotenv 6 | 7 | load_dotenv() 8 | 9 | apikey = os.environ['apikey'] 10 | url = os.environ['url'] 11 | TRANSLATE_TEXT = 'The world is yours' 12 | 13 | """Authenticate""" 14 | authenticator = IAMAuthenticator(apikey) 15 | language_translator = LanguageTranslatorV3( 16 | version='2018-05-01', 17 | authenticator=authenticator 18 | ) 19 | 20 | language_translator.set_service_url(url) 21 | 22 | translation = language_translator.translate( 23 | text=TRANSLATE_TEXT, 24 | model_id='en-fr').get_result() 25 | 26 | print(json.dumps(translation, indent=2, ensure_ascii=False)) 27 | 28 | def english_to_french(english_text): 29 | # English to French function 30 | french_text = language_translator.translate( 31 | text=english_text, 32 | model_id='en-fr').get_result() 33 | return french_text 34 | 35 | def french_to_english(french_text): 36 | # French to English function 37 | english_text = language_translator.translate( 38 | text=french_text, 39 | model_id='fr-en').get_result() 40 | return english_text 41 | --------------------------------------------------------------------------------