Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,57 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
-
#
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# --- LOAD MODEL (
|
| 10 |
-
print("
|
| 11 |
try:
|
| 12 |
classifier = pipeline(
|
| 13 |
"text-classification",
|
| 14 |
model="anggars/emotive-sentiment",
|
| 15 |
-
top_k=None #
|
| 16 |
)
|
| 17 |
-
print("Model
|
| 18 |
except Exception as e:
|
| 19 |
-
print(f"
|
| 20 |
classifier = None
|
| 21 |
|
| 22 |
class TextRequest(BaseModel):
|
| 23 |
text: str
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
def home():
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
@app.post("/predict")
|
| 30 |
def predict(req: TextRequest):
|
| 31 |
if classifier is None:
|
| 32 |
-
return {"error": "Model
|
| 33 |
|
| 34 |
try:
|
| 35 |
-
#
|
| 36 |
results = classifier(req.text)
|
| 37 |
return {"emotions": results[0]}
|
| 38 |
except Exception as e:
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
from fastapi.responses import HTMLResponse
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# Initialize FastAPI
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# --- LOAD MODEL (Run only once on server startup) ---
|
| 11 |
+
print("Loading model anggars/emotive-sentiment...")
|
| 12 |
try:
|
| 13 |
classifier = pipeline(
|
| 14 |
"text-classification",
|
| 15 |
model="anggars/emotive-sentiment",
|
| 16 |
+
top_k=None # Retrieve all scores
|
| 17 |
)
|
| 18 |
+
print("Model successfully loaded!")
|
| 19 |
except Exception as e:
|
| 20 |
+
print(f"Failed to load model: {e}")
|
| 21 |
classifier = None
|
| 22 |
|
| 23 |
class TextRequest(BaseModel):
|
| 24 |
text: str
|
| 25 |
|
| 26 |
+
# --- ROOT ENDPOINT (Embed Web via Iframe) ---
|
| 27 |
+
@app.get("/", response_class=HTMLResponse)
|
| 28 |
def home():
|
| 29 |
+
# Return simple HTML containing full-screen iframe to Vercel
|
| 30 |
+
# This keeps HF Space settings menu accessible while showing your app
|
| 31 |
+
return """
|
| 32 |
+
<!DOCTYPE html>
|
| 33 |
+
<html>
|
| 34 |
+
<head>
|
| 35 |
+
<title>Personalify API</title>
|
| 36 |
+
<style>
|
| 37 |
+
body, html { margin: 0; padding: 0; height: 100%; overflow: hidden; }
|
| 38 |
+
iframe { width: 100%; height: 100%; border: none; }
|
| 39 |
+
</style>
|
| 40 |
+
</head>
|
| 41 |
+
<body>
|
| 42 |
+
<iframe src="https://personalify.vercel.app/lyrics"></iframe>
|
| 43 |
+
</body>
|
| 44 |
+
</html>
|
| 45 |
+
"""
|
| 46 |
|
| 47 |
+
# --- API ENDPOINT (For Backend requests) ---
|
| 48 |
@app.post("/predict")
|
| 49 |
def predict(req: TextRequest):
|
| 50 |
if classifier is None:
|
| 51 |
+
return {"error": "Model is not ready or failed to load."}
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
# Pipeline result format: [[{'label': 'joy', 'score': 0.9}, ...]]
|
| 55 |
results = classifier(req.text)
|
| 56 |
return {"emotions": results[0]}
|
| 57 |
except Exception as e:
|