Spaces:
Sleeping
Sleeping
Commit
·
faff143
1
Parent(s):
cfbb534
html fix
Browse files- app.py +10 -25
- static/index.html +36 -24
- static/script.js +12 -8
app.py
CHANGED
@@ -1,35 +1,20 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
-
from transformers import pipeline
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
from fastapi.responses import FileResponse
|
5 |
|
6 |
-
|
|
|
7 |
app = FastAPI()
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
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
|
29 |
-
can be found [here](<https://huggingface.co/google/flan-t5-small>).
|
30 |
-
"""
|
31 |
-
# Use the pipeline to generate text from the given input text
|
32 |
-
output = pipe(text)
|
33 |
-
|
34 |
-
# Return the generated text in a JSON response
|
35 |
-
return {"output": output[0]["generated_text"]}
|
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from fastapi.responses import FileResponse
|
4 |
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
app = FastAPI()
|
8 |
+
|
9 |
+
pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
|
10 |
+
|
11 |
+
@app.get("/infer_t5")
|
12 |
+
def t5(input):
|
13 |
+
output = pipe_flan(input)
|
14 |
+
return {"output": output[0]["generated_text"]}
|
15 |
|
16 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
17 |
|
18 |
@app.get("/")
|
19 |
def index() -> FileResponse:
|
20 |
+
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static/index.html
CHANGED
@@ -1,24 +1,36 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
<
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8" />
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
6 |
+
<title>Fast API 🤗 Space served with Uvicorn</title>
|
7 |
+
<link rel="stylesheet" href="style.css" />
|
8 |
+
<script type="module" src="script.js"></script>
|
9 |
+
</head>
|
10 |
+
<body>
|
11 |
+
<main>
|
12 |
+
<section id="text-gen">
|
13 |
+
<h1>Text generation using Flan T5</h1>
|
14 |
+
<p>
|
15 |
+
Model:
|
16 |
+
<a
|
17 |
+
href="https://huggingface.co/google/flan-t5-small"
|
18 |
+
rel="noreferrer"
|
19 |
+
target="_blank"
|
20 |
+
>google/flan-t5-small</a
|
21 |
+
>
|
22 |
+
</p>
|
23 |
+
<form class="text-gen-form">
|
24 |
+
<label for="text-gen-input">Text prompt</label>
|
25 |
+
<input
|
26 |
+
id="text-gen-input"
|
27 |
+
type="text"
|
28 |
+
value="English: Translate How are you ?. Russian:"
|
29 |
+
/>
|
30 |
+
<button id="text-gen-submit">Submit</button>
|
31 |
+
<p class="text-gen-output"></p>
|
32 |
+
</form>
|
33 |
+
</section>
|
34 |
+
</main>
|
35 |
+
</body>
|
36 |
+
</html>
|
static/script.js
CHANGED
@@ -1,17 +1,21 @@
|
|
1 |
-
const textGenForm = document.querySelector(
|
2 |
|
3 |
const translateText = async (text) => {
|
4 |
-
|
5 |
-
|
6 |
|
7 |
-
|
8 |
};
|
9 |
|
10 |
-
textGenForm.addEventListener(
|
11 |
event.preventDefault();
|
12 |
|
13 |
-
const textGenInput = document.getElementById(
|
14 |
-
const textGenParagraph = document.querySelector(
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
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 |
+
try {
|
17 |
+
textGenParagraph.textContent = await translateText(textGenInput.value);
|
18 |
+
} catch (err) {
|
19 |
+
console.error(err);
|
20 |
+
}
|
21 |
});
|