Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from fastapi import FastAPI, Query
|
|
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
4 |
import os
|
@@ -12,51 +13,38 @@ model = AutoModelForSequenceClassification.from_pretrained(
|
|
12 |
tokenizer = AutoTokenizer.from_pretrained("kmcs-casulit/department-classification", token=token)
|
13 |
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
14 |
|
15 |
-
# Define a request model
|
16 |
-
class TextRequest(BaseModel):
|
17 |
-
text: str
|
18 |
|
19 |
-
@app.get("/")
|
20 |
-
def
|
21 |
-
return {"message": "Hello, World!"}
|
22 |
-
|
23 |
-
|
24 |
-
@app.get("/classify", response_class=HTMLResponse)
|
25 |
-
def get_form():
|
26 |
return """
|
27 |
<html>
|
28 |
<body>
|
29 |
-
<
|
30 |
-
<form
|
31 |
<label for="text">Enter text:</label>
|
32 |
-
<input type="text" id="text" name="text"
|
33 |
<button type="submit">Classify</button>
|
34 |
</form>
|
35 |
-
<p id="result"></p>
|
36 |
-
<script>
|
37 |
-
document.getElementById('classificationForm').addEventListener('submit', async (event) => {
|
38 |
-
event.preventDefault();
|
39 |
-
const text = document.getElementById('text').value;
|
40 |
-
const response = await fetch('/classify', {
|
41 |
-
method: 'POST',
|
42 |
-
headers: {
|
43 |
-
'Content-Type': 'application/json',
|
44 |
-
},
|
45 |
-
body: JSON.stringify({ text }),
|
46 |
-
});
|
47 |
-
const result = await response.json();
|
48 |
-
document.getElementById('result').innerText = 'Classification: ' + result.output;
|
49 |
-
});
|
50 |
-
</script>
|
51 |
</body>
|
52 |
</html>
|
53 |
"""
|
54 |
|
55 |
-
@app.post("/classify")
|
56 |
-
async def
|
57 |
-
|
58 |
-
text =
|
59 |
if not text:
|
60 |
-
return
|
|
|
61 |
output = pipe(text)
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, Query
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
from pydantic import BaseModel
|
4 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
5 |
import os
|
|
|
13 |
tokenizer = AutoTokenizer.from_pretrained("kmcs-casulit/department-classification", token=token)
|
14 |
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
15 |
|
|
|
|
|
|
|
16 |
|
17 |
+
@app.get("/", response_class=HTMLResponse)
|
18 |
+
def read_root():
|
|
|
|
|
|
|
|
|
|
|
19 |
return """
|
20 |
<html>
|
21 |
<body>
|
22 |
+
<h1>Welcome to the Text Classification App</h1>
|
23 |
+
<form action="/classify" method="post">
|
24 |
<label for="text">Enter text:</label>
|
25 |
+
<input type="text" id="text" name="text">
|
26 |
<button type="submit">Classify</button>
|
27 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
</body>
|
29 |
</html>
|
30 |
"""
|
31 |
|
32 |
+
@app.post("/classify", response_class=HTMLResponse)
|
33 |
+
async def classify(request: Request):
|
34 |
+
form_data = await request.form()
|
35 |
+
text = form_data.get('text')
|
36 |
if not text:
|
37 |
+
return HTMLResponse(content="<html><body><p>No text provided.</p></body></html>", status_code=400)
|
38 |
+
|
39 |
output = pipe(text)
|
40 |
+
classification = output[0]['label']
|
41 |
+
return HTMLResponse(content=f"""
|
42 |
+
<html>
|
43 |
+
<body>
|
44 |
+
<h1>Classification Result</h1>
|
45 |
+
<p>Text: {text}</p>
|
46 |
+
<p>Classification: {classification}</p>
|
47 |
+
<a href="/">Back</a>
|
48 |
+
</body>
|
49 |
+
</html>
|
50 |
+
""")
|