anggars commited on
Commit
3697b25
·
verified ·
1 Parent(s): 91150ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -10
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
- # Inisialisasi FastAPI
7
  app = FastAPI()
8
 
9
- # --- LOAD MODEL (Hanya sekali saat server nyala) ---
10
- print("Sedang memuat model anggars/emotive-sentiment...")
11
  try:
12
  classifier = pipeline(
13
  "text-classification",
14
  model="anggars/emotive-sentiment",
15
- top_k=None # Ambil semua skor
16
  )
17
- print("Model BERHASIL dimuat!")
18
  except Exception as e:
19
- print(f"Gagal memuat model: {e}")
20
  classifier = None
21
 
22
  class TextRequest(BaseModel):
23
  text: str
24
 
25
- @app.get("/")
 
26
  def home():
27
- return {"status": "Online", "message": "Personalify Emotion API is Running!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
 
29
  @app.post("/predict")
30
  def predict(req: TextRequest):
31
  if classifier is None:
32
- return {"error": "Model belum siap/gagal dimuat."}
33
 
34
  try:
35
- # Hasil pipeline: [[{'label': 'joy', 'score': 0.9}, ...]]
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: