Spaces:
Sleeping
Sleeping
hasanriaz121
commited on
Commit
•
9ca45a3
1
Parent(s):
8a9c8ec
Added Application files
Browse files- Dockerfile +11 -0
- main.py +58 -0
- requirements.txt +7 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
58 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.74.*
|
2 |
+
requests==2.27.*
|
3 |
+
sentencepiece==0.1.*
|
4 |
+
torch==1.11.*
|
5 |
+
transformers==4.*
|
6 |
+
uvicorn[standard]==0.17.*
|
7 |
+
nest_asyncio==1.5.*
|