rajeshradhakrishnan commited on
Commit
400caeb
1 Parent(s): 2bb0092

English-Malayalam Translate v30

Browse files
Files changed (7) hide show
  1. Dockerfile +4 -0
  2. main.py +1 -9
  3. requirements.txt +0 -1
  4. static/chat.js +0 -84
  5. static/index.html +27 -4
  6. static/script.js +81 -12
  7. templates/chat.html +0 -34
Dockerfile CHANGED
@@ -27,4 +27,8 @@ WORKDIR $HOME/app
27
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
28
  COPY --chown=user . $HOME/app
29
 
 
 
 
 
30
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
27
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
28
  COPY --chown=user . $HOME/app
29
 
30
+ RUN --mount=type=secret,id=API_KEY,mode=0444,required=true \
31
+ API_KEY=$(cat /run/secrets/API_KEY)&&
32
+ echo "const API_KEY = '${API_KEY}';" > $HOME/app/static/config.js
33
+
34
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  from fastapi import FastAPI, Request
3
  from fastapi.staticfiles import StaticFiles
4
  from fastapi.responses import FileResponse
5
- from fastapi.templating import Jinja2Templates
6
  from transformers import pipeline
7
 
8
 
@@ -20,11 +19,4 @@ app.mount("/", StaticFiles(directory="static", html=True), name="static")
20
 
21
  @app.get("/")
22
  def index() -> FileResponse:
23
- return FileResponse(path="/app/static/index.html", media_type="text/html")
24
-
25
- templates = Jinja2Templates(directory="/app/templates")
26
-
27
- @app.get("/chat")
28
- async def chat():
29
- apikey = {"APIKEY": os.environ.get("API_KEY")}
30
- return templates.TemplateResponse("chat.html", {"apikey": apikey})
 
2
  from fastapi import FastAPI, Request
3
  from fastapi.staticfiles import StaticFiles
4
  from fastapi.responses import FileResponse
 
5
  from transformers import pipeline
6
 
7
 
 
19
 
20
  @app.get("/")
21
  def index() -> FileResponse:
22
+ return FileResponse(path="/app/static/index.html", media_type="text/html")
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  fastapi==0.74.*
2
- jinja2
3
  requests==2.27.*
4
  sentencepiece==0.1.*
5
  torch==1.11.*
 
1
  fastapi==0.74.*
 
2
  requests==2.27.*
3
  sentencepiece==0.1.*
4
  torch==1.11.*
static/chat.js DELETED
@@ -1,84 +0,0 @@
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 CHANGED
@@ -1,9 +1,32 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
 
 
 
 
4
  </head>
5
  <body>
6
- <!-- Include your script file -->
7
- <script src="script.js"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  </body>
9
- </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <title>JavaScipt Open Assistant Clone</title>
6
+ <link rel="stylesheet" href="style.css">
7
+ <script src="config.js"></script>
8
  </head>
9
  <body>
10
+ <section class="side-bar">
11
+ <button>New Chat</button>
12
+ <div class="history"></div>
13
+ <nav>
14
+ <p>Made by a മലയാളി</p>
15
+ </nav>
16
+ </section>
17
+ <section class="main">
18
+ <h1>മലയാളം - Open Assistant</h1>
19
+ <p id="output"></p>
20
+ <div class="bottom-section">
21
+ <div class="input-container">
22
+ <input>
23
+ <div id="submit">➢</div>
24
+ </div>
25
+ </div>
26
+ <p class="info">Open Assistant - This is the 4th iteration English
27
+ supervised-fine-tuning (SFT) model of the Open-Assistant project.
28
+ </p>
29
+ </section>
30
+ <script src="script.js"></script>
31
  </body>
32
+ </html>
static/script.js CHANGED
@@ -1,15 +1,84 @@
1
- const xhr = new XMLHttpRequest();
2
- xhr.open('GET', '/chat');
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
 
 
 
 
 
 
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)
templates/chat.html DELETED
@@ -1,34 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <title>JavaScipt Open Assistant Clone</title>
6
- <link rel="stylesheet" href="style.css">
7
- </head>
8
- <body>
9
- <section class="side-bar">
10
- <button>New Chat</button>
11
- <div class="history"></div>
12
- <nav>
13
- <p>Made by a മലയാളി</p>
14
- </nav>
15
- </section>
16
- <section class="main">
17
- <h1>മലയാളം - Open Assistant</h1>
18
- <p id="output"></p>
19
- <div class="bottom-section">
20
- <div class="input-container">
21
- <input>
22
- <div id="submit">➢</div>
23
- </div>
24
- </div>
25
- <p class="info">Open Assistant - This is the 4th iteration English
26
- supervised-fine-tuning (SFT) model of the Open-Assistant project.
27
- </p>
28
- </section>
29
- <script>
30
- var apiKey = {{ apikey.APIKEY }};
31
- </script>
32
- <script src="chat.js"></script>
33
- </body>
34
- </html>