rajeshradhakrishnan commited on
Commit
4a794f1
β€’
1 Parent(s): 0203d1c

English-Malayalam Translate v28

Browse files
main.py CHANGED
@@ -23,7 +23,7 @@ app.mount("/", StaticFiles(directory="static", html=True), name="static")
23
  # return FileResponse(path="/app/static/index.html", media_type="text/html")
24
  templates = Jinja2Templates(directory="templates")
25
 
26
- @app.get("/", response_class=HTMLResponse)
27
  async def index():
28
  apikey = {"APIKEY": os.environ.get("API_KEY")}
29
- return templates.TemplateResponse("index.html", {"apikey": apikey})
 
23
  # return FileResponse(path="/app/static/index.html", media_type="text/html")
24
  templates = Jinja2Templates(directory="templates")
25
 
26
+ @app.get("/")
27
  async def index():
28
  apikey = {"APIKEY": os.environ.get("API_KEY")}
29
+ return templates.TemplateResponse("chat.html", {"apikey": apikey})
static/chat.js ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ console.log("API_KEY " + apiKey)
2
+ const translateText = async (text) => {
3
+ const inferResponse = await fetch(`infer_t5?input=${text}`);
4
+ const inferJson = await inferResponse.json();
5
+
6
+ return inferJson.output;
7
+ };
8
+
9
+
10
+ function generatePrompterAssistantText(inputString) {
11
+ // Split the input string into an array of sentences
12
+ const sentences = inputString.split('<|endoftext|>');
13
+
14
+ // Initialize arrays for prompter and assistant text
15
+ let prompterText = [];
16
+ let assistantText = [];
17
+
18
+ // Loop through each sentence and add it to the prompter or assistant text array
19
+ for (let i = 0; i < sentences.length; i++) {
20
+ // Check if the sentence contains the <prompter> tag
21
+ if (sentences[i].includes('<|prompter|>')) {
22
+ // Extract the text within the <prompter> tags and add it to the prompter text array
23
+ const prompterSentence = sentences[i].replace(/<\|prompter\|>/g, '');
24
+ prompterText.push(prompterSentence);
25
+ } else if (sentences[i].includes('<|assistant|>')) {
26
+ const assistantSentence = sentences[i].replace(/<\|assistant\|>/g, '');
27
+ // Add the sentence to the assistant text array
28
+ assistantText.push(assistantSentence);
29
+ }
30
+ }
31
+
32
+ // Return the prompter and assistant text arrays
33
+ return [prompterText, assistantText];
34
+ }
35
+
36
+ const submitButton = document.querySelector('#submit')
37
+ const outPutElement = document.querySelector('#output')
38
+ const inputElement = document.querySelector('input')
39
+ const historyElement = document.querySelector('.history')
40
+ const buttonElement = document.querySelector('button')
41
+
42
+
43
+ function changeInput(value)
44
+ {
45
+ console.log(value)
46
+ const inputElement = document.querySelector('input')
47
+ inputElement.value = value
48
+ }
49
+ async function getMessage(){
50
+ console.log("input value "+ inputElement.value)
51
+ const options = {
52
+ method: "POST",
53
+ headers: {
54
+ Authorization: `Bearer ${API_KEY}`,
55
+ "Content-Type": "application/json"
56
+ },
57
+ body: JSON.stringify({
58
+ inputs: "<|prompter|>" + inputElement.value + "<|endoftext|><|assistant|>"
59
+ })
60
+ }
61
+ try{
62
+ const response = await fetch("https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5", options);
63
+ const data = await response.json()
64
+ console.log(data[0].generated_text)
65
+
66
+ if(inputElement.value && data && data[0].generated_text){
67
+ const [prompterText, assistantText] = generatePrompterAssistantText(data[0].generated_text);
68
+ outPutElement.textContent = await translateText(assistantText);
69
+ const pElement = document.createElement('p')
70
+ pElement.textContent = inputElement.value
71
+ pElement.addEventListener('click', () => changeInput(pElement.textContent))
72
+ historyElement.append(pElement)
73
+ }
74
+ } catch(error) {
75
+ console.log(error)
76
+ }
77
+ }
78
+
79
+ submitButton.addEventListener('click', getMessage)
80
+
81
+ function clearInput(){
82
+ inputElement.value = ''
83
+ }
84
+ buttonElement.addEventListener('click', clearInput)
static/index.html ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ </head>
5
+ <body>
6
+ <!-- Include your script file -->
7
+ <script src="{{ url_for('static', path='script.js') }}"></script>
8
+ </body>
9
+ </html>
static/script.js CHANGED
@@ -1,84 +1,15 @@
1
- console.log("API_KEY " + apiKey)
2
- const translateText = async (text) => {
3
- const inferResponse = await fetch(`infer_t5?input=${text}`);
4
- const inferJson = await inferResponse.json();
5
-
6
- return inferJson.output;
7
- };
8
-
9
-
10
- function generatePrompterAssistantText(inputString) {
11
- // Split the input string into an array of sentences
12
- const sentences = inputString.split('<|endoftext|>');
13
-
14
- // Initialize arrays for prompter and assistant text
15
- let prompterText = [];
16
- let assistantText = [];
17
-
18
- // Loop through each sentence and add it to the prompter or assistant text array
19
- for (let i = 0; i < sentences.length; i++) {
20
- // Check if the sentence contains the <prompter> tag
21
- if (sentences[i].includes('<|prompter|>')) {
22
- // Extract the text within the <prompter> tags and add it to the prompter text array
23
- const prompterSentence = sentences[i].replace(/<\|prompter\|>/g, '');
24
- prompterText.push(prompterSentence);
25
- } else if (sentences[i].includes('<|assistant|>')) {
26
- const assistantSentence = sentences[i].replace(/<\|assistant\|>/g, '');
27
- // Add the sentence to the assistant text array
28
- assistantText.push(assistantSentence);
29
- }
30
  }
31
-
32
- // Return the prompter and assistant text arrays
33
- return [prompterText, assistantText];
34
- }
35
-
36
- const submitButton = document.querySelector('#submit')
37
- const outPutElement = document.querySelector('#output')
38
- const inputElement = document.querySelector('input')
39
- const historyElement = document.querySelector('.history')
40
- const buttonElement = document.querySelector('button')
41
-
42
-
43
- function changeInput(value)
44
- {
45
- console.log(value)
46
- const inputElement = document.querySelector('input')
47
- inputElement.value = value
48
- }
49
- async function getMessage(){
50
- console.log("input value "+ inputElement.value)
51
- const options = {
52
- method: "POST",
53
- headers: {
54
- Authorization: `Bearer ${API_KEY}`,
55
- "Content-Type": "application/json"
56
- },
57
- body: JSON.stringify({
58
- inputs: "<|prompter|>" + inputElement.value + "<|endoftext|><|assistant|>"
59
- })
60
- }
61
- try{
62
- const response = await fetch("https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5", options);
63
- const data = await response.json()
64
- console.log(data[0].generated_text)
65
-
66
- if(inputElement.value && data && data[0].generated_text){
67
- const [prompterText, assistantText] = generatePrompterAssistantText(data[0].generated_text);
68
- outPutElement.textContent = await translateText(assistantText);
69
- const pElement = document.createElement('p')
70
- pElement.textContent = inputElement.value
71
- pElement.addEventListener('click', () => changeInput(pElement.textContent))
72
- historyElement.append(pElement)
73
- }
74
- } catch(error) {
75
- console.log(error)
76
  }
77
- }
78
-
79
- submitButton.addEventListener('click', getMessage)
80
 
81
- function clearInput(){
82
- inputElement.value = ''
83
- }
84
- buttonElement.addEventListener('click', clearInput)
 
1
+ const xhr = new XMLHttpRequest();
2
+ xhr.open('GET', '/');
3
+ xhr.onload = function() {
4
+ if (xhr.status === 200) {
5
+ // Update the DOM with the rendered HTML
6
+ const content = document.createElement('div');
7
+ content.innerHTML = xhr.response;
8
+ document.body.appendChild(content);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  }
10
+ else {
11
+ console.log('Request failed. Returned status of ' + xhr.status);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
+ };
14
+ xhr.send();
 
15
 
 
 
 
 
templates/{index.html β†’ chat.html} RENAMED
@@ -29,6 +29,6 @@
29
  <script>
30
  var apiKey = {{ apikey.APIKEY }};
31
  </script>
32
- <script src="script.js"></script>
33
  </body>
34
  </html>
 
29
  <script>
30
  var apiKey = {{ apikey.APIKEY }};
31
  </script>
32
+ <script src="chat.js"></script>
33
  </body>
34
  </html>