Lukas Korganas commited on
Commit Β·
64c6b78
1
Parent(s): 01c9799
Initial deploy
Browse files- README.md +60 -7
- app.py +88 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,13 +1,66 @@
|
|
| 1 |
---
|
| 2 |
-
title: Dynamic Transformers
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Dynamic Transformers Pipeline API
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.x
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Dynamic Transformers Pipeline API
|
| 14 |
+
|
| 15 |
+
Pass any `transformers.pipeline()` config as JSON, run inference on ZeroGPU RTX Pro 6000 Blackwell.
|
| 16 |
+
|
| 17 |
+
## API Usage
|
| 18 |
+
|
| 19 |
+
```python
|
| 20 |
+
from gradio_client import Client
|
| 21 |
+
|
| 22 |
+
client = Client("your-username/your-space")
|
| 23 |
+
result = client.predict(
|
| 24 |
+
pipeline_config={"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"},
|
| 25 |
+
inputs="Hello world",
|
| 26 |
+
inference_kwargs={"max_new_tokens": 50},
|
| 27 |
+
api_name="/inference"
|
| 28 |
+
)
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## 3. Hit deploy
|
| 35 |
+
|
| 36 |
+
Upload β Space builds (~30-60s) β done.
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
## 4. Test it
|
| 41 |
+
|
| 42 |
+
Replace `your-username/your-space` with your actual Space name:
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
from gradio_client import Client
|
| 46 |
+
|
| 47 |
+
client = Client("your-username/dynamic-transformers-api")
|
| 48 |
+
|
| 49 |
+
# Text generation
|
| 50 |
+
print(client.predict(
|
| 51 |
+
pipeline_config={"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"},
|
| 52 |
+
inputs="The future of AI is",
|
| 53 |
+
inference_kwargs={"max_new_tokens": 50},
|
| 54 |
+
api_name="/inference"
|
| 55 |
+
))
|
| 56 |
+
|
| 57 |
+
# NER (swap pipeline on the fly, same Space)
|
| 58 |
+
print(client.predict(
|
| 59 |
+
pipeline_config={"task": "ner", "model": "Jean-Baptiste/camembert-ner", "aggregation_strategy": "simple"},
|
| 60 |
+
inputs="Apple is looking at buying U.K. startup for $1 billion",
|
| 61 |
+
inference_kwargs={},
|
| 62 |
+
api_name="/inference"
|
| 63 |
+
))
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
|
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import json
|
| 4 |
+
import hashlib
|
| 5 |
+
import logging
|
| 6 |
+
from functools import lru_cache
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
_pipeline_cache = {"hash": None, "pipe": None, "config": None}
|
| 13 |
+
|
| 14 |
+
def _hash_config(cfg: dict) -> str:
|
| 15 |
+
return hashlib.sha256(json.dumps(cfg, sort_keys=True).encode()).hexdigest()[:16]
|
| 16 |
+
|
| 17 |
+
@lru_cache(maxsize=3)
|
| 18 |
+
def _load_pipeline(hash_key: str, cfg_json: str):
|
| 19 |
+
cfg = json.loads(cfg_json)
|
| 20 |
+
logger.info(f"π Loading pipeline: {cfg}")
|
| 21 |
+
pipe = pipeline(**cfg)
|
| 22 |
+
logger.info("β
Loaded.")
|
| 23 |
+
return pipe
|
| 24 |
+
|
| 25 |
+
def get_pipe(cfg: dict):
|
| 26 |
+
h = _hash_config(cfg)
|
| 27 |
+
if _pipeline_cache["hash"] == h and _pipeline_cache["pipe"] is not None:
|
| 28 |
+
return _pipeline_cache["pipe"], False
|
| 29 |
+
pipe = _load_pipeline(h, json.dumps(cfg, sort_keys=True))
|
| 30 |
+
_pipeline_cache.update({"hash": h, "pipe": pipe, "config": cfg})
|
| 31 |
+
return pipe, True
|
| 32 |
+
|
| 33 |
+
@spaces.GPU(duration=40)
|
| 34 |
+
def inference(pipeline_config, inputs, inference_kwargs=None):
|
| 35 |
+
if inference_kwargs is None:
|
| 36 |
+
inference_kwargs = {}
|
| 37 |
+
try:
|
| 38 |
+
pconf = json.loads(pipeline_config) if isinstance(pipeline_config, str) else pipeline_config
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return json.dumps({"error": f"Bad pipeline_config: {e}"})
|
| 41 |
+
try:
|
| 42 |
+
ikw = json.loads(inference_kwargs) if isinstance(inference_kwargs, str) else inference_kwargs
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return json.dumps({"error": f"Bad inference_kwargs: {e}"})
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
pipe, reloaded = get_pipe(pconf)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return json.dumps({"error": f"Pipeline load failed: {e}"})
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
raw = json.loads(inputs) if isinstance(inputs, str) and inputs.strip().startswith(("[", "{")) else inputs
|
| 53 |
+
except:
|
| 54 |
+
raw = inputs
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
result = pipe(raw, **ikw)
|
| 58 |
+
return json.dumps({
|
| 59 |
+
"success": True,
|
| 60 |
+
"reloaded": reloaded,
|
| 61 |
+
"result": result,
|
| 62 |
+
}, default=str, indent=2)
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return json.dumps({"error": f"Inference failed: {e}"})
|
| 65 |
+
|
| 66 |
+
with gr.Blocks(title="Dynamic Transformers Pipeline API") as demo:
|
| 67 |
+
gr.Markdown("# π Dynamic Transformers Pipeline API\nZero-GPU. Pass any `transformers.pipeline` config via JSON.")
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column():
|
| 70 |
+
pcfg = gr.JSON(label="pipeline_config", value={"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"})
|
| 71 |
+
inp = gr.Textbox(label="inputs", value="The future of AI is", lines=3)
|
| 72 |
+
ikw = gr.JSON(label="inference_kwargs", value={"max_new_tokens": 50})
|
| 73 |
+
btn = gr.Button("Run", variant="primary")
|
| 74 |
+
with gr.Column():
|
| 75 |
+
out = gr.JSON(label="output")
|
| 76 |
+
btn.click(inference, [pcfg, inp, ikw], out)
|
| 77 |
+
|
| 78 |
+
gr.Markdown("## API Example")
|
| 79 |
+
gr.Code("""from gradio_client import Client
|
| 80 |
+
c = Client("your-username/your-space")
|
| 81 |
+
print(c.predict(
|
| 82 |
+
pipeline_config={"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"},
|
| 83 |
+
inputs="Hello",
|
| 84 |
+
inference_kwargs={"max_new_tokens": 20},
|
| 85 |
+
api_name="/inference"
|
| 86 |
+
))""", language="python")
|
| 87 |
+
|
| 88 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
accelerate
|
| 5 |
+
spaces
|