Spaces:
Sleeping
Sleeping
fin09 commited on
Commit ·
fc47766
1
Parent(s): 16f4ce1
Add @spaces.GPU decorator to satisfy ZeroGPU supervisor on HuggingFace Spaces
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
Main entry point for Hugging Face Spaces (Gradio SDK).
|
| 3 |
Provides an interactive Gradio Web UI and exposes the FastAPI REST API simultaneously.
|
| 4 |
"""
|
| 5 |
|
|
@@ -11,6 +11,18 @@ PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
|
|
| 11 |
if PROJECT_ROOT not in sys.path:
|
| 12 |
sys.path.insert(0, PROJECT_ROOT)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
import gradio as gr
|
| 15 |
from api.server import app as fastapi_app
|
| 16 |
from nlp_core.language_detector import LanguageDetector
|
|
@@ -30,6 +42,7 @@ seq_ar = Seq2SeqSummarizer.load_checkpoint(CKPT_AR, device="cpu") if os.path.exi
|
|
| 30 |
seq_en = Seq2SeqSummarizer.load_checkpoint(CKPT_EN, device="cpu") if os.path.exists(CKPT_EN) else None
|
| 31 |
|
| 32 |
|
|
|
|
| 33 |
def summarize_text(text, algorithm, sentences, beam_width):
|
| 34 |
if not text or not text.strip():
|
| 35 |
return "Please enter text to summarize.", "N/A", "N/A", "N/A"
|
|
@@ -71,7 +84,7 @@ with gr.Blocks(title="Bilingual Arabic-English Text Summarization AI", theme=gr.
|
|
| 71 |
gr.Markdown("""
|
| 72 |
**Production NLP Platform** supporting Extractive (TextRank, LSA, Hybrid) and Abstractive (Seq2Seq + Bahdanau Attention) summarization.
|
| 73 |
|
| 74 |
-
🔗 **
|
| 75 |
""")
|
| 76 |
|
| 77 |
with gr.Row():
|
|
@@ -115,6 +128,6 @@ for route in fastapi_app.routes:
|
|
| 115 |
if hasattr(route, "path") and route.path.startswith("/api/"):
|
| 116 |
demo.app.routes.append(route)
|
| 117 |
|
| 118 |
-
# Launch Gradio app
|
| 119 |
demo.queue()
|
| 120 |
-
demo.launch(
|
|
|
|
| 1 |
"""
|
| 2 |
+
Main entry point for Hugging Face Spaces (Gradio SDK + ZeroGPU support).
|
| 3 |
Provides an interactive Gradio Web UI and exposes the FastAPI REST API simultaneously.
|
| 4 |
"""
|
| 5 |
|
|
|
|
| 11 |
if PROJECT_ROOT not in sys.path:
|
| 12 |
sys.path.insert(0, PROJECT_ROOT)
|
| 13 |
|
| 14 |
+
# ZeroGPU support for Hugging Face Spaces
|
| 15 |
+
try:
|
| 16 |
+
import spaces
|
| 17 |
+
has_spaces = True
|
| 18 |
+
except ImportError:
|
| 19 |
+
has_spaces = False
|
| 20 |
+
|
| 21 |
+
def gpu_decorator(func):
|
| 22 |
+
if has_spaces and hasattr(spaces, "GPU"):
|
| 23 |
+
return spaces.GPU(func)
|
| 24 |
+
return func
|
| 25 |
+
|
| 26 |
import gradio as gr
|
| 27 |
from api.server import app as fastapi_app
|
| 28 |
from nlp_core.language_detector import LanguageDetector
|
|
|
|
| 42 |
seq_en = Seq2SeqSummarizer.load_checkpoint(CKPT_EN, device="cpu") if os.path.exists(CKPT_EN) else None
|
| 43 |
|
| 44 |
|
| 45 |
+
@gpu_decorator
|
| 46 |
def summarize_text(text, algorithm, sentences, beam_width):
|
| 47 |
if not text or not text.strip():
|
| 48 |
return "Please enter text to summarize.", "N/A", "N/A", "N/A"
|
|
|
|
| 84 |
gr.Markdown("""
|
| 85 |
**Production NLP Platform** supporting Extractive (TextRank, LSA, Hybrid) and Abstractive (Seq2Seq + Bahdanau Attention) summarization.
|
| 86 |
|
| 87 |
+
🔗 **REST API**: Use `/api/v1/summarize` | `/api/v1/health` | `/api/v1/detect-language` | `/api/v1/evaluate`
|
| 88 |
""")
|
| 89 |
|
| 90 |
with gr.Row():
|
|
|
|
| 128 |
if hasattr(route, "path") and route.path.startswith("/api/"):
|
| 129 |
demo.app.routes.append(route)
|
| 130 |
|
| 131 |
+
# Launch Gradio app
|
| 132 |
demo.queue()
|
| 133 |
+
demo.launch(ssr_mode=False)
|