Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
|
| 6 |
+
# Initialize your model (this happens once when container starts)
|
| 7 |
+
classifier = pipeline("sentiment-analysis")
|
| 8 |
+
|
| 9 |
+
@app.get("/")
|
| 10 |
+
def read_root():
|
| 11 |
+
return {"message": "Hello from Hugging Face Spaces!"}
|
| 12 |
+
|
| 13 |
+
@app.post("/analyze")
|
| 14 |
+
def analyze_sentiment(text: str):
|
| 15 |
+
result = classifier(text)
|
| 16 |
+
return {"sentiment": result[0]["label"], "score": result[0]["score"]}
|