Spaces:
Running
Running
shourya commited on
Commit ·
96d64c3
1
Parent(s): ef56a1c
Add Spaces GPU decorators for ZeroGPU startup
Browse files
app.py
CHANGED
|
@@ -8,6 +8,18 @@ import config
|
|
| 8 |
import utils
|
| 9 |
import pandas as pd
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# ===================== UTILITIES =====================
|
| 12 |
|
| 13 |
def load_sample_texts():
|
|
@@ -71,6 +83,7 @@ def get_embeddings_examples():
|
|
| 71 |
|
| 72 |
# ===================== SENTIMENT ANALYSIS =====================
|
| 73 |
|
|
|
|
| 74 |
def demo_sentiment(text):
|
| 75 |
"""Demo sentiment analysis."""
|
| 76 |
if not text.strip():
|
|
@@ -82,6 +95,7 @@ def demo_sentiment(text):
|
|
| 82 |
|
| 83 |
# ===================== NER =====================
|
| 84 |
|
|
|
|
| 85 |
def demo_ner(text):
|
| 86 |
"""Demo named entity recognition."""
|
| 87 |
if not text.strip():
|
|
@@ -95,6 +109,7 @@ def demo_ner(text):
|
|
| 95 |
|
| 96 |
# ===================== QUESTION ANSWERING =====================
|
| 97 |
|
|
|
|
| 98 |
def demo_qa(context, question):
|
| 99 |
"""Demo question answering."""
|
| 100 |
if not context.strip() or not question.strip():
|
|
@@ -108,6 +123,7 @@ def demo_qa(context, question):
|
|
| 108 |
|
| 109 |
# ===================== SUMMARIZATION =====================
|
| 110 |
|
|
|
|
| 111 |
def demo_summarization(text):
|
| 112 |
"""Demo text summarization."""
|
| 113 |
if not text.strip():
|
|
@@ -124,6 +140,7 @@ def demo_summarization(text):
|
|
| 124 |
|
| 125 |
# ===================== SEMANTIC SIMILARITY =====================
|
| 126 |
|
|
|
|
| 127 |
def demo_similarity(text1, text2):
|
| 128 |
"""Demo semantic similarity."""
|
| 129 |
if not text1.strip() or not text2.strip():
|
|
@@ -137,6 +154,7 @@ def demo_similarity(text1, text2):
|
|
| 137 |
|
| 138 |
# ===================== TOKENIZATION =====================
|
| 139 |
|
|
|
|
| 140 |
def demo_tokenization(text):
|
| 141 |
"""Demo tokenization."""
|
| 142 |
if not text.strip():
|
|
|
|
| 8 |
import utils
|
| 9 |
import pandas as pd
|
| 10 |
|
| 11 |
+
try:
|
| 12 |
+
import spaces
|
| 13 |
+
except Exception:
|
| 14 |
+
spaces = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _gpu_decorator(func):
|
| 18 |
+
"""Use Spaces GPU decorator when available; no-op locally."""
|
| 19 |
+
if spaces is None:
|
| 20 |
+
return func
|
| 21 |
+
return spaces.GPU(func)
|
| 22 |
+
|
| 23 |
# ===================== UTILITIES =====================
|
| 24 |
|
| 25 |
def load_sample_texts():
|
|
|
|
| 83 |
|
| 84 |
# ===================== SENTIMENT ANALYSIS =====================
|
| 85 |
|
| 86 |
+
@_gpu_decorator
|
| 87 |
def demo_sentiment(text):
|
| 88 |
"""Demo sentiment analysis."""
|
| 89 |
if not text.strip():
|
|
|
|
| 95 |
|
| 96 |
# ===================== NER =====================
|
| 97 |
|
| 98 |
+
@_gpu_decorator
|
| 99 |
def demo_ner(text):
|
| 100 |
"""Demo named entity recognition."""
|
| 101 |
if not text.strip():
|
|
|
|
| 109 |
|
| 110 |
# ===================== QUESTION ANSWERING =====================
|
| 111 |
|
| 112 |
+
@_gpu_decorator
|
| 113 |
def demo_qa(context, question):
|
| 114 |
"""Demo question answering."""
|
| 115 |
if not context.strip() or not question.strip():
|
|
|
|
| 123 |
|
| 124 |
# ===================== SUMMARIZATION =====================
|
| 125 |
|
| 126 |
+
@_gpu_decorator
|
| 127 |
def demo_summarization(text):
|
| 128 |
"""Demo text summarization."""
|
| 129 |
if not text.strip():
|
|
|
|
| 140 |
|
| 141 |
# ===================== SEMANTIC SIMILARITY =====================
|
| 142 |
|
| 143 |
+
@_gpu_decorator
|
| 144 |
def demo_similarity(text1, text2):
|
| 145 |
"""Demo semantic similarity."""
|
| 146 |
if not text1.strip() or not text2.strip():
|
|
|
|
| 154 |
|
| 155 |
# ===================== TOKENIZATION =====================
|
| 156 |
|
| 157 |
+
@_gpu_decorator
|
| 158 |
def demo_tokenization(text):
|
| 159 |
"""Demo tokenization."""
|
| 160 |
if not text.strip():
|