Spaces:
Sleeping
Sleeping
Create space_app.py
Browse files- space_app.py +49 -0
space_app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Reuse the existing FastAPI app and inference from api_server
|
| 5 |
+
from api_server import app as fastapi_app, run_models
|
| 6 |
+
|
| 7 |
+
app: FastAPI = fastapi_app
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def ui_fn(text: str):
|
| 11 |
+
r = run_models(text)
|
| 12 |
+
fabsa_output = r.get("FABSA", {"label": "N/A", "scores": {}})
|
| 13 |
+
moodmeter_output = r.get("MoodMeter", {"label": "N/A", "scores": {}})
|
| 14 |
+
twitter_output = r.get("Twitter", {"label": "N/A", "scores": {}})
|
| 15 |
+
ensemble_output = r.get("Ensemble", {"label": "N/A"})
|
| 16 |
+
|
| 17 |
+
# Ensure outputs are dictionaries for gr.JSON
|
| 18 |
+
if not isinstance(fabsa_output, dict):
|
| 19 |
+
fabsa_output = {"label": str(fabsa_output), "scores": {}}
|
| 20 |
+
if not isinstance(moodmeter_output, dict):
|
| 21 |
+
moodmeter_output = {"label": str(moodmeter_output), "scores": {}}
|
| 22 |
+
if not isinstance(twitter_output, dict):
|
| 23 |
+
twitter_output = {"label": str(twitter_output), "scores": {}}
|
| 24 |
+
if not isinstance(ensemble_output, dict):
|
| 25 |
+
ensemble_output = {"label": str(ensemble_output)}
|
| 26 |
+
|
| 27 |
+
return fabsa_output, moodmeter_output, twitter_output, ensemble_output
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=ui_fn,
|
| 32 |
+
inputs=gr.Textbox(label="Enter text", placeholder="Type something emotional…"),
|
| 33 |
+
outputs=[
|
| 34 |
+
gr.JSON(label="FABSA"),
|
| 35 |
+
gr.JSON(label="MoodMeter"),
|
| 36 |
+
gr.JSON(label="Twitter"),
|
| 37 |
+
gr.JSON(label="Ensemble"),
|
| 38 |
+
],
|
| 39 |
+
title="Mental Health Sentiment Analyzer",
|
| 40 |
+
description="Gradio UI + FastAPI backend deployed on Hugging Face Spaces.",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Mount Gradio UI on the root path
|
| 44 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
import uvicorn
|
| 48 |
+
|
| 49 |
+
uvicorn.run("space_app:app", host="127.0.0.1", port=8000, reload=False)
|