Spaces:
Runtime error
Runtime error
Abineshkumar77 commited on
Commit ·
30c467a
1
Parent(s): 9ce1001
Add application file
Browse files- app.py +20 -2
- requirements.txt +4 -0
app.py
CHANGED
|
@@ -1,7 +1,25 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
@app.get('/')
|
| 6 |
def home():
|
| 7 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Initialize the sentiment analysis pipeline
|
| 5 |
+
pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
@app.get('/')
|
| 10 |
def home():
|
| 11 |
+
return {"message": "Welcome to the Sentiment Analysis API"}
|
| 12 |
+
|
| 13 |
+
@app.get('/analyze')
|
| 14 |
+
def analyze(tweet: str = Query(..., description="The tweet to analyze")):
|
| 15 |
+
# Use the pipeline to analyze the sentiment of the tweet
|
| 16 |
+
results = pipe(tweet)
|
| 17 |
+
|
| 18 |
+
# Format the output to include labels and their corresponding scores
|
| 19 |
+
sentiment_scores = {result['label']: result['score'] for result in results}
|
| 20 |
+
|
| 21 |
+
return sentiment_scores
|
| 22 |
+
|
| 23 |
+
# To run this app on Hugging Face Spaces:
|
| 24 |
+
# 1. Save this code as `app.py`.
|
| 25 |
+
# 2. Deploy it to a new Hugging Face Space, and the Space will automatically start your FastAPI app.
|
requirements.txt
CHANGED
|
@@ -1,2 +1,6 @@
|
|
| 1 |
fastapi[standard]
|
| 2 |
uvicorn[standard]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
fastapi[standard]
|
| 2 |
uvicorn[standard]
|
| 3 |
+
transformers[standard]
|
| 4 |
+
torch[standard]
|
| 5 |
+
torchvision[standard]
|
| 6 |
+
torchaudio[standard]
|