Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,42 @@ def greet_json():
|
|
21 |
return {"message": "Hello, World!"}
|
22 |
|
23 |
|
24 |
-
@app.
|
25 |
-
def
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
return {"output": output[0]['label']}
|
|
|
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 |
+
<h2>Text Classification</h2>
|
30 |
+
<form id="classificationForm">
|
31 |
+
<label for="text">Enter text:</label>
|
32 |
+
<input type="text" id="text" name="text" required>
|
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 classify_post(request: Request):
|
57 |
+
data = await request.json()
|
58 |
+
text = data.get('text')
|
59 |
+
if not text:
|
60 |
+
return {"error": "Text field is required."}
|
61 |
+
output = pipe(text)
|
62 |
return {"output": output[0]['label']}
|