Spaces:
Sleeping
Sleeping
add static files and update main.py
Browse files- main.py +9 -1
- static/index.html +27 -0
- static/script.js +21 -0
- static/style.css +45 -0
main.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
2 |
from pydantic import BaseModel
|
3 |
import spacy
|
4 |
from spacy.matcher import Matcher
|
@@ -11,7 +13,7 @@ matcher = Matcher(nlp.vocab)
|
|
11 |
class TextInput(BaseModel):
|
12 |
text: str
|
13 |
|
14 |
-
@app.
|
15 |
def score_text(text_input: TextInput):
|
16 |
"""Endpoint to score text for uncertain statements using spaCy Matcher."""
|
17 |
# Load the text into spaCy's nlp object
|
@@ -36,3 +38,9 @@ def score_text(text_input: TextInput):
|
|
36 |
|
37 |
# Return the score
|
38 |
return {"score": score, "uncertain_statements": uncertain_statements}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from fastapi.staticfiles import StaticFiles
|
3 |
+
from fastapi.responses import FileResponse
|
4 |
from pydantic import BaseModel
|
5 |
import spacy
|
6 |
from spacy.matcher import Matcher
|
|
|
13 |
class TextInput(BaseModel):
|
14 |
text: str
|
15 |
|
16 |
+
@app.get("/score_text")
|
17 |
def score_text(text_input: TextInput):
|
18 |
"""Endpoint to score text for uncertain statements using spaCy Matcher."""
|
19 |
# Load the text into spaCy's nlp object
|
|
|
38 |
|
39 |
# Return the score
|
40 |
return {"score": score, "uncertain_statements": uncertain_statements}
|
41 |
+
|
42 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
43 |
+
|
44 |
+
@app.get("/")
|
45 |
+
def index() -> FileResponse:
|
46 |
+
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
static/index.html
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 with spaCy on 🤗 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-spacy">
|
13 |
+
<h1>Uncertainty statements</h1>
|
14 |
+
<form class="text-spacy-form">
|
15 |
+
<label for="text-spacy-input">Text prompt</label>
|
16 |
+
<input
|
17 |
+
id="text-spacy-input"
|
18 |
+
type="text"
|
19 |
+
value="Enter a sentence to score it:"
|
20 |
+
/>
|
21 |
+
<button id="text-spacy-submit">Submit</button>
|
22 |
+
<p class="text-spacy-output"></p>
|
23 |
+
</form>
|
24 |
+
</section>
|
25 |
+
</main>
|
26 |
+
</body>
|
27 |
+
</html>
|
static/script.js
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const textGenForm = document.querySelector('.text-spacy-form');
|
2 |
+
|
3 |
+
const translateText = async (text) => {
|
4 |
+
const inferResponse = await fetch(`score_text?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 textSpacyInput = document.getElementById('text-spacy-input');
|
14 |
+
const textSpacyParagraph = document.querySelector('.text-spacy-output');
|
15 |
+
|
16 |
+
try {
|
17 |
+
textSpacyParagraph.textContent = await translateText(textSpacyInput.value);
|
18 |
+
} catch (err) {
|
19 |
+
console.error(err);
|
20 |
+
}
|
21 |
+
});
|
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-spacy-output {
|
42 |
+
min-height: 1.2rem;
|
43 |
+
margin: 1rem;
|
44 |
+
border: 0.5px solid grey;
|
45 |
+
}
|