Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,57 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
|
|
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
@app.post("/analyze")
|
| 11 |
-
async def analyze_text(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
import nest_asyncio
|
| 4 |
+
import uvicorn
|
| 5 |
+
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
+
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
@app.on_event("startup")
|
| 12 |
+
async def startup_event():
|
| 13 |
+
model_path="cardiffnlp/twitter-roberta-base-sentiment-latest"
|
| 14 |
+
global sentiment_task
|
| 15 |
+
sentiment_task = pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@app.get("/", response_class=HTMLResponse)
|
| 19 |
+
async def home():
|
| 20 |
+
html_content = """
|
| 21 |
+
<html>
|
| 22 |
+
<head>
|
| 23 |
+
<title>Text Classification</title>
|
| 24 |
+
</head>
|
| 25 |
+
<body>
|
| 26 |
+
<h1>Text Classification</h1>
|
| 27 |
+
<form method="post" action="/analyze/">
|
| 28 |
+
<input type="text" name="text" placeholder="Enter text to analyze" autocomplete="off" required>
|
| 29 |
+
<input type="submit" value="Analyze">
|
| 30 |
+
</form>
|
| 31 |
+
</body>
|
| 32 |
+
</html>
|
| 33 |
+
"""
|
| 34 |
+
return HTMLResponse(content=html_content, status_code=200)
|
| 35 |
+
|
| 36 |
+
@app.get('/{name}')
|
| 37 |
+
async def get_name(name: str):
|
| 38 |
+
return {'Welcome To Here': f'{name}'}
|
| 39 |
|
| 40 |
+
@app.post("/analyze/", response_class=HTMLResponse)
|
| 41 |
+
async def analyze_text(text: str = Form(...)):
|
| 42 |
+
# Assuming your model is a function that takes input and returns predictions
|
| 43 |
+
prediction = sentiment_task(text)
|
| 44 |
+
html_content = """
|
| 45 |
+
<html>
|
| 46 |
+
<head>
|
| 47 |
+
<title>Analysis Result</title>
|
| 48 |
+
</head>
|
| 49 |
+
<body>
|
| 50 |
+
<h1>Analysis Result:</h1>
|
| 51 |
+
<p>Input Text: {input_text}</p>
|
| 52 |
+
<p>Prediction: {prediction}</p>
|
| 53 |
+
<button><a href="/" >Back</a><button>
|
| 54 |
+
</body>
|
| 55 |
+
</html>
|
| 56 |
+
""".format(input_text=text, prediction=prediction[0]['label'])
|
| 57 |
+
return HTMLResponse(content=html_content, status_code=200)
|