Lukas Korganas commited on
Commit
d98e26f
ยท
1 Parent(s): 55ff977

Stabilize example input handling

Browse files
Files changed (1) hide show
  1. app.py +30 -56
app.py CHANGED
@@ -17,9 +17,9 @@ def _hash_config(cfg: dict) -> str:
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):
@@ -63,39 +63,35 @@ def inference(pipeline_config, inputs, inference_kwargs=None):
63
  except Exception as e:
64
  return json.dumps({"error": f"Inference failed: {e}"})
65
 
66
- APP_CSS = """
67
- /* Force body/Blocks to allow scrolling */
68
- .gradio-container {
69
- max-height: none !important;
70
- overflow-y: auto !important;
71
- }
72
- /* Ensure the app wrapper scrolls */
73
- #component-0 {
74
- max-height: none !important;
75
- }
76
- /* JSON editors need fixed height but scroll internally */
77
- .json-editor {
78
- max-height: 300px;
79
- }
80
- """
 
 
 
 
 
81
 
82
  with gr.Blocks(title="Dynamic Transformers Pipeline API") as demo:
83
  gr.Markdown("# ๐Ÿš€ Dynamic Transformers Pipeline API\nZero-GPU. Pass any `transformers.pipeline` config via JSON.")
84
 
85
  with gr.Row():
86
  with gr.Column(scale=1):
87
- pcfg = gr.JSON(
88
- label="pipeline_config",
89
- value={"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"},
90
- )
91
- inp = gr.JSON(
92
- label="inputs",
93
- value='"The future of AI is"',
94
- )
95
- ikw = gr.JSON(
96
- label="inference_kwargs",
97
- value={"max_new_tokens": 50},
98
- )
99
  btn = gr.Button("โ–ถ๏ธ Run Inference", variant="primary")
100
 
101
  with gr.Column(scale=1):
@@ -103,8 +99,7 @@ with gr.Blocks(title="Dynamic Transformers Pipeline API") as demo:
103
 
104
  btn.click(inference, [pcfg, inp, ikw], out)
105
 
106
- # Computed examples (not cached, run live on click)
107
- gr.Markdown("## ๐Ÿ“ Examples (click to compute live)")
108
  with gr.Row():
109
  with gr.Column():
110
  gr.Markdown("**Text Generation**")
@@ -121,39 +116,18 @@ with gr.Blocks(title="Dynamic Transformers Pipeline API") as demo:
121
  ex3_btn = gr.Button("Run: privacy-filter", size="sm")
122
  ex3_out = gr.JSON(label="result")
123
 
124
- EX1 = {
125
- "pipeline_config": {"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"},
126
- "inputs": "The future of AI is",
127
- "inference_kwargs": {"max_new_tokens": 50},
128
- }
129
- EX2 = {
130
- "pipeline_config": {"task": "zero-shot-classification", "model": "facebook/bart-large-mnli"},
131
- "inputs": "This is a contract about data privacy and user rights.",
132
- "inference_kwargs": {"candidate_labels": ["legal", "finance", "technology", "sports"]},
133
- }
134
- EX3 = {
135
- "pipeline_config": {"task": "token-classification", "model": "openai/privacy-filter"},
136
- "inputs": "My name is Alice Smith",
137
- "inference_kwargs": {},
138
- }
139
-
140
- def load_example(ex):
141
- """Populate the main inputs and run the selected example."""
142
- result = inference(ex["pipeline_config"], ex["inputs"], ex["inference_kwargs"])
143
- return ex["pipeline_config"], ex["inputs"], ex["inference_kwargs"], result
144
-
145
  ex1_btn.click(
146
- fn=lambda: load_example(EX1),
147
  inputs=None,
148
  outputs=[pcfg, inp, ikw, ex1_out],
149
  )
150
  ex2_btn.click(
151
- fn=lambda: load_example(EX2),
152
  inputs=None,
153
  outputs=[pcfg, inp, ikw, ex2_out],
154
  )
155
  ex3_btn.click(
156
- fn=lambda: load_example(EX3),
157
  inputs=None,
158
  outputs=[pcfg, inp, ikw, ex3_out],
159
  )
@@ -169,4 +143,4 @@ print(client.predict(
169
  api_name="/inference"
170
  ))""", language="python")
171
 
172
- demo.launch(css=APP_CSS)
 
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):
 
63
  except Exception as e:
64
  return json.dumps({"error": f"Inference failed: {e}"})
65
 
66
+ # Example configs
67
+ EX1 = {
68
+ "pipeline_config": {"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"},
69
+ "inputs": "The future of AI is",
70
+ "inference_kwargs": {"max_new_tokens": 50},
71
+ }
72
+ EX2 = {
73
+ "pipeline_config": {"task": "zero-shot-classification", "model": "facebook/bart-large-mnli"},
74
+ "inputs": "This is a contract about data privacy and user rights.",
75
+ "inference_kwargs": {"candidate_labels": ["legal", "finance", "technology", "sports"]},
76
+ }
77
+ EX3 = {
78
+ "pipeline_config": {"task": "token-classification", "model": "openai/privacy-filter"},
79
+ "inputs": "My name is Alice Smith",
80
+ "inference_kwargs": {},
81
+ }
82
+
83
+ def run_example(ex):
84
+ result = inference(ex["pipeline_config"], ex["inputs"], ex["inference_kwargs"])
85
+ return ex["pipeline_config"], ex["inputs"], ex["inference_kwargs"], result
86
 
87
  with gr.Blocks(title="Dynamic Transformers Pipeline API") as demo:
88
  gr.Markdown("# ๐Ÿš€ Dynamic Transformers Pipeline API\nZero-GPU. Pass any `transformers.pipeline` config via JSON.")
89
 
90
  with gr.Row():
91
  with gr.Column(scale=1):
92
+ pcfg = gr.JSON(label="pipeline_config", value=EX1["pipeline_config"])
93
+ inp = gr.Textbox(label="inputs", value=EX1["inputs"], lines=3)
94
+ ikw = gr.JSON(label="inference_kwargs", value=EX1["inference_kwargs"])
 
 
 
 
 
 
 
 
 
95
  btn = gr.Button("โ–ถ๏ธ Run Inference", variant="primary")
96
 
97
  with gr.Column(scale=1):
 
99
 
100
  btn.click(inference, [pcfg, inp, ikw], out)
101
 
102
+ gr.Markdown("## ๐Ÿ“ Examples (click to populate & run)")
 
103
  with gr.Row():
104
  with gr.Column():
105
  gr.Markdown("**Text Generation**")
 
116
  ex3_btn = gr.Button("Run: privacy-filter", size="sm")
117
  ex3_out = gr.JSON(label="result")
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  ex1_btn.click(
120
+ fn=lambda: run_example(EX1),
121
  inputs=None,
122
  outputs=[pcfg, inp, ikw, ex1_out],
123
  )
124
  ex2_btn.click(
125
+ fn=lambda: run_example(EX2),
126
  inputs=None,
127
  outputs=[pcfg, inp, ikw, ex2_out],
128
  )
129
  ex3_btn.click(
130
+ fn=lambda: run_example(EX3),
131
  inputs=None,
132
  outputs=[pcfg, inp, ikw, ex3_out],
133
  )
 
143
  api_name="/inference"
144
  ))""", language="python")
145
 
146
+ demo.launch()