Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
import uvicorn
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="Human Essence Emotional Flow Visualizer")
|
| 10 |
+
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 20 |
+
DATASET_REPO = "wop/Human-Essence-Dataset"
|
| 21 |
+
|
| 22 |
+
def fetch_dataset(force=False):
|
| 23 |
+
download_mode = "force_redownload" if force else None
|
| 24 |
+
ds = load_dataset(DATASET_REPO, token=HF_TOKEN, split="train", download_mode=download_mode)
|
| 25 |
+
return ds.to_list()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
print("Loading dataset from Hugging Face...")
|
| 29 |
+
try:
|
| 30 |
+
dataset = fetch_dataset()
|
| 31 |
+
print(f"Loaded {len(dataset)} entries successfully!")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error loading dataset: {e}")
|
| 34 |
+
dataset = []
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.post("/api/refresh")
|
| 38 |
+
def refresh_data():
|
| 39 |
+
global dataset
|
| 40 |
+
try:
|
| 41 |
+
dataset = fetch_dataset(force=True)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
raise HTTPException(status_code=500, detail=f"Refresh failed: {e}")
|
| 44 |
+
print(f"Refreshed dataset: {len(dataset)} entries")
|
| 45 |
+
return {"ok": True, "total_entries": len(dataset)}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@app.get("/api/dataset")
|
| 49 |
+
def get_dataset(limit: int = 100, offset: int = 0):
|
| 50 |
+
"""Get dataset entries with pagination"""
|
| 51 |
+
total = len(dataset)
|
| 52 |
+
end_idx = min(offset + limit, total)
|
| 53 |
+
entries = dataset[offset:end_idx]
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"entries": entries,
|
| 57 |
+
"total": total,
|
| 58 |
+
"offset": offset,
|
| 59 |
+
"limit": limit
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@app.get("/api/entry/{index}")
|
| 64 |
+
def get_entry(index: int):
|
| 65 |
+
"""Get a specific entry by index"""
|
| 66 |
+
if index < 0 or index >= len(dataset):
|
| 67 |
+
raise HTTPException(status_code=404, detail="Entry not found")
|
| 68 |
+
|
| 69 |
+
return dataset[index]
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
@app.get("/api/stats")
|
| 73 |
+
def get_stats():
|
| 74 |
+
"""Get dataset statistics"""
|
| 75 |
+
total_entries = len(dataset)
|
| 76 |
+
entries_with_text = sum(1 for e in dataset if e.get("text"))
|
| 77 |
+
entries_with_emotions = sum(1 for e in dataset if e.get("emotional_flow"))
|
| 78 |
+
|
| 79 |
+
# Count unique emotions
|
| 80 |
+
emotion_counts = {}
|
| 81 |
+
for entry in dataset:
|
| 82 |
+
for flow in entry.get("emotional_flow", []):
|
| 83 |
+
label = flow.get("label", "unknown")
|
| 84 |
+
emotion_counts[label] = emotion_counts.get(label, 0) + 1
|
| 85 |
+
|
| 86 |
+
return {
|
| 87 |
+
"total_entries": total_entries,
|
| 88 |
+
"entries_with_text": entries_with_text,
|
| 89 |
+
"entries_with_emotions": entries_with_emotions,
|
| 90 |
+
"emotion_distribution": emotion_counts
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
class NoCacheStaticFiles(StaticFiles):
|
| 95 |
+
async def get_response(self, path, scope):
|
| 96 |
+
response = await super().get_response(path, scope)
|
| 97 |
+
response.headers["Cache-Control"] = "no-cache, must-revalidate"
|
| 98 |
+
return response
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
app.mount("/", NoCacheStaticFiles(directory="static", html=True), name="static")
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|