vsmolyakov commited on
Commit
cfbb534
·
1 Parent(s): b49483d

java script

Browse files
Files changed (4) hide show
  1. app.py +12 -4
  2. static/index.html +24 -0
  3. static/script.js +17 -0
  4. static/style.css +45 -0
app.py CHANGED
@@ -1,6 +1,8 @@
1
  from fastapi import FastAPI
2
  from transformers import pipeline
3
-
 
 
4
  # Create a new FastAPI app instance
5
  app = FastAPI()
6
 
@@ -8,13 +10,19 @@ app = FastAPI()
8
  # This function will be able to generate text
9
  # given an input.
10
  pipe = pipeline("text2text-generation", model="google/flan-t5-small")
11
-
 
 
 
 
 
 
12
  # Define a function to handle the GET request at `/generate`
13
  # The generate() function is defined as a FastAPI route that takes a
14
  # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
15
  # containing the generated text under the key "output"
16
- @app.get("/")
17
- def generate(text: str):
18
  """
19
  Using the text2text-generation pipeline from `transformers`, generate text
20
  from the given input text. The model used is `google/flan-t5-small`, which
 
1
  from fastapi import FastAPI
2
  from transformers import pipeline
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.responses import FileResponse
5
+
6
  # Create a new FastAPI app instance
7
  app = FastAPI()
8
 
 
10
  # This function will be able to generate text
11
  # given an input.
12
  pipe = pipeline("text2text-generation", model="google/flan-t5-small")
13
+
14
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
15
+
16
+ @app.get("/")
17
+ def index() -> FileResponse:
18
+ return FileResponse(path="/app/static/index.html", media_type="text/html")
19
+
20
  # Define a function to handle the GET request at `/generate`
21
  # The generate() function is defined as a FastAPI route that takes a
22
  # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
23
  # containing the generated text under the key "output"
24
+ @app.get("/infer_t5")
25
+ def t5(text: str):
26
  """
27
  Using the text2text-generation pipeline from `transformers`, generate text
28
  from the given input text. The model used is `google/flan-t5-small`, which
static/index.html ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <main>
2
+ <section id="text-gen">
3
+ <h2>Text generation using Flan T5</h2>
4
+ <p>
5
+ Model:
6
+ <a
7
+ href="https://huggingface.co/google/flan-t5-small"
8
+ rel="noreferrer"
9
+ target="_blank"
10
+ >google/flan-t5-small
11
+ </a>
12
+ </p>
13
+ <form class="text-gen-form">
14
+ <label for="text-gen-input">Text prompt</label>
15
+ <input
16
+ id="text-gen-input"
17
+ type="text"
18
+ value="German: There are many ducks"
19
+ />
20
+ <button id="text-gen-submit">Submit</button>
21
+ <p class="text-gen-output"></p>
22
+ </form>
23
+ </section>
24
+ </main>
static/script.js ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const textGenForm = document.querySelector(".text-gen-form");
2
+
3
+ const translateText = async (text) => {
4
+ const inferResponse = await fetch(`infer_t5?input=${text}`);
5
+ const inferJson = await inferResponse.json();
6
+
7
+ return inferJson.output;
8
+ };
9
+
10
+ textGenForm.addEventListener("submit", async (event) => {
11
+ event.preventDefault();
12
+
13
+ const textGenInput = document.getElementById("text-gen-input");
14
+ const textGenParagraph = document.querySelector(".text-gen-output");
15
+
16
+ textGenParagraph.textContent = await translateText(textGenInput.value);
17
+ });
static/style.css ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ --text: hsl(0 0% 15%);
3
+ padding: 2.5rem;
4
+ font-family: sans-serif;
5
+ color: var(--text);
6
+ }
7
+
8
+ body.dark-theme {
9
+ --text: hsl(0 0% 90%);
10
+ background-color: hsl(223 39% 7%);
11
+ }
12
+
13
+ main {
14
+ max-width: 80rem;
15
+ text-align: center;
16
+ }
17
+
18
+ section {
19
+ display: flex;
20
+ flex-direction: column;
21
+ align-items: center;
22
+ }
23
+
24
+ a {
25
+ color: var(--text);
26
+ }
27
+
28
+ form {
29
+ width: 30rem;
30
+ margin: 0 auto;
31
+ }
32
+
33
+ input {
34
+ width: 100%;
35
+ }
36
+
37
+ button {
38
+ cursor: pointer;
39
+ }
40
+
41
+ .text-gen-output {
42
+ min-height: 1.2rem;
43
+ margin: 1rem;
44
+ border: 0.5px solid grey;
45
+ }