hellosindh commited on
Commit
01c3df5
ยท
1 Parent(s): 6d71e7f

Mount Flask WSGI directly under FastAPI /api and support dual routes

Browse files
Files changed (2) hide show
  1. app.py +17 -5
  2. app_backend.py +4 -0
app.py CHANGED
@@ -5,13 +5,15 @@ Space: https://huggingface.co/spaces/hellosindh/sindhi-ngram
5
 
6
  import os
7
  import sys
8
- import gradio as gr
9
  from starlette.middleware.wsgi import WSGIMiddleware
 
 
10
 
11
  # Import our underlying Flask application
12
  from app_backend import app as flask_app
13
 
14
- # Modern informative status dashboard on Hugging Face Space homepage
15
  with gr.Blocks(title="HelloSindh Sindhi Ngram API") as demo:
16
  gr.Markdown("""
17
  # ๐Ÿš€ HelloSindh Sindhi Ngram Viewer โ€” Production API
@@ -31,13 +33,23 @@ with gr.Blocks(title="HelloSindh Sindhi Ngram API") as demo:
31
  | `/api/ngram?q=ุณู†ฺŒ&start_year=1980&end_year=2026` | `GET` | Historical percentage frequencies across years |
32
  | `/api/trends_entities` | `GET` | Top famous personalities & cities rankings |
33
  | `/api/top50` | `GET` | Top 100 popular words by year & character lengths |
34
- | `/api/quotes?q=ุณู†ฺŒ` | `GET` | Concordance citations & book snippets |
35
  | `/api/author-stats?q=ุณู†ฺŒ` | `GET` | Top authors using the searched term |
36
  | `/api/famous_personalities` | `GET` | Famous personalities by year |
37
  """)
38
 
39
- # Mount all Flask routes directly under the FastAPI/Gradio server
40
- demo.app.mount("/api", WSGIMiddleware(flask_app))
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == "__main__":
43
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
5
 
6
  import os
7
  import sys
8
+ from fastapi import FastAPI
9
  from starlette.middleware.wsgi import WSGIMiddleware
10
+ from starlette.responses import RedirectResponse
11
+ import gradio as gr
12
 
13
  # Import our underlying Flask application
14
  from app_backend import app as flask_app
15
 
16
+ # 1. Informative status dashboard on Gradio
17
  with gr.Blocks(title="HelloSindh Sindhi Ngram API") as demo:
18
  gr.Markdown("""
19
  # ๐Ÿš€ HelloSindh Sindhi Ngram Viewer โ€” Production API
 
33
  | `/api/ngram?q=ุณู†ฺŒ&start_year=1980&end_year=2026` | `GET` | Historical percentage frequencies across years |
34
  | `/api/trends_entities` | `GET` | Top famous personalities & cities rankings |
35
  | `/api/top50` | `GET` | Top 100 popular words by year & character lengths |
36
+ | `/api/snippets?q=ุณู†ฺŒ` | `GET` | Concordance citations & book snippets |
37
  | `/api/author-stats?q=ุณู†ฺŒ` | `GET` | Top authors using the searched term |
38
  | `/api/famous_personalities` | `GET` | Famous personalities by year |
39
  """)
40
 
41
+ # 2. FastAPI app wrapping Flask
42
+ fastapi_app = FastAPI(title="HelloSindh Sindhi Ngram API")
43
+
44
+ # Mount Flask app to handle all /api endpoints
45
+ fastapi_app.mount("/api", WSGIMiddleware(flask_app))
46
+
47
+ @fastapi_app.get("/")
48
+ def home():
49
+ return RedirectResponse(url="/dashboard")
50
+
51
+ # 3. Mount Gradio interface
52
+ app = gr.mount_gradio_app(fastapi_app, demo, path="/dashboard")
53
 
54
  if __name__ == "__main__":
55
  demo.launch(server_name="0.0.0.0", server_port=7860)
app_backend.py CHANGED
@@ -196,6 +196,7 @@ def serve_fonts(filename):
196
  # ==========================================
197
 
198
  @app.route("/api/stats")
 
199
  def api_stats():
200
  # Load from precomputed corpus_stats.json if present
201
  cs_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "corpus_stats.json")
@@ -643,6 +644,7 @@ def get_top_50_insights_for_query(raw_query: str, heh_norm: bool = True) -> list
643
 
644
 
645
  @app.route("/api/ngram")
 
646
  def api_ngram():
647
  raw_query = request.args.get("q", "").strip()
648
  start_year = request.args.get("start_year", type=int)
@@ -865,6 +867,7 @@ def contains_exact_word(sentence: str, term: str, heh_norm: bool = True) -> bool
865
 
866
 
867
  @app.route("/api/snippets")
 
868
  def api_snippets():
869
  raw_query = request.args.get("q", "").strip()
870
  year = request.args.get("year", type=int)
@@ -938,6 +941,7 @@ def api_snippets():
938
 
939
 
940
  @app.route("/api/author-stats")
 
941
  def api_author_stats():
942
  """
943
  Returns the top 10 authors who used the searched word(s) the most.
 
196
  # ==========================================
197
 
198
  @app.route("/api/stats")
199
+ @app.route("/stats")
200
  def api_stats():
201
  # Load from precomputed corpus_stats.json if present
202
  cs_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "corpus_stats.json")
 
644
 
645
 
646
  @app.route("/api/ngram")
647
+ @app.route("/ngram")
648
  def api_ngram():
649
  raw_query = request.args.get("q", "").strip()
650
  start_year = request.args.get("start_year", type=int)
 
867
 
868
 
869
  @app.route("/api/snippets")
870
+ @app.route("/snippets")
871
  def api_snippets():
872
  raw_query = request.args.get("q", "").strip()
873
  year = request.args.get("year", type=int)
 
941
 
942
 
943
  @app.route("/api/author-stats")
944
+ @app.route("/author-stats")
945
  def api_author_stats():
946
  """
947
  Returns the top 10 authors who used the searched word(s) the most.