Lukas Korganas commited on
Commit Β·
d0230aa
1
Parent(s): 958a3b5
Fix Gradio 6 JSON and CSS handling
Browse files
app.py
CHANGED
|
@@ -63,9 +63,7 @@ def inference(pipeline_config, inputs, inference_kwargs=None):
|
|
| 63 |
except Exception as e:
|
| 64 |
return json.dumps({"error": f"Inference failed: {e}"})
|
| 65 |
|
| 66 |
-
|
| 67 |
-
title="Dynamic Transformers Pipeline API",
|
| 68 |
-
css="""
|
| 69 |
/* Force body/Blocks to allow scrolling */
|
| 70 |
.gradio-container {
|
| 71 |
max-height: none !important;
|
|
@@ -80,7 +78,8 @@ with gr.Blocks(
|
|
| 80 |
max-height: 300px;
|
| 81 |
}
|
| 82 |
"""
|
| 83 |
-
|
|
|
|
| 84 |
gr.Markdown("# π Dynamic Transformers Pipeline API\nZero-GPU. Pass any `transformers.pipeline` config via JSON.")
|
| 85 |
|
| 86 |
with gr.Row():
|
|
@@ -91,7 +90,7 @@ with gr.Blocks(
|
|
| 91 |
)
|
| 92 |
inp = gr.JSON(
|
| 93 |
label="inputs",
|
| 94 |
-
value="The future of AI is",
|
| 95 |
)
|
| 96 |
ikw = gr.JSON(
|
| 97 |
label="inference_kwargs",
|
|
@@ -104,6 +103,52 @@ with gr.Blocks(
|
|
| 104 |
|
| 105 |
btn.click(inference, [pcfg, inp, ikw], out)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
gr.Markdown("## API Example")
|
| 108 |
gr.Code("""from gradio_client import Client
|
| 109 |
|
|
@@ -115,4 +160,4 @@ print(client.predict(
|
|
| 115 |
api_name="/inference"
|
| 116 |
))""", language="python")
|
| 117 |
|
| 118 |
-
demo.launch()
|
|
|
|
| 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;
|
|
|
|
| 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():
|
|
|
|
| 90 |
)
|
| 91 |
inp = gr.JSON(
|
| 92 |
label="inputs",
|
| 93 |
+
value='"The future of AI is"',
|
| 94 |
)
|
| 95 |
ikw = gr.JSON(
|
| 96 |
label="inference_kwargs",
|
|
|
|
| 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**")
|
| 111 |
+
ex1_btn = gr.Button("Run: SmolLM2 text-gen", size="sm")
|
| 112 |
+
ex1_out = gr.JSON(label="result")
|
| 113 |
+
|
| 114 |
+
with gr.Column():
|
| 115 |
+
gr.Markdown("**Zero-Shot Classification**")
|
| 116 |
+
ex2_btn = gr.Button("Run: zero-shot", size="sm")
|
| 117 |
+
ex2_out = gr.JSON(label="result")
|
| 118 |
+
|
| 119 |
+
with gr.Column():
|
| 120 |
+
gr.Markdown("**Token Classification (Privacy Filter)**")
|
| 121 |
+
ex3_btn = gr.Button("Run: privacy-filter", size="sm")
|
| 122 |
+
ex3_out = gr.JSON(label="result")
|
| 123 |
+
|
| 124 |
+
ex1_btn.click(
|
| 125 |
+
fn=lambda: inference(
|
| 126 |
+
{"task": "text-generation", "model": "HuggingFaceTB/SmolLM2-135M-Instruct"},
|
| 127 |
+
"The future of AI is",
|
| 128 |
+
{"max_new_tokens": 50},
|
| 129 |
+
),
|
| 130 |
+
inputs=None,
|
| 131 |
+
outputs=ex1_out,
|
| 132 |
+
)
|
| 133 |
+
ex2_btn.click(
|
| 134 |
+
fn=lambda: inference(
|
| 135 |
+
{"task": "zero-shot-classification", "model": "facebook/bart-large-mnli"},
|
| 136 |
+
"This is a contract about data privacy and user rights.",
|
| 137 |
+
{"candidate_labels": ["legal", "finance", "technology", "sports"]},
|
| 138 |
+
),
|
| 139 |
+
inputs=None,
|
| 140 |
+
outputs=ex2_out,
|
| 141 |
+
)
|
| 142 |
+
ex3_btn.click(
|
| 143 |
+
fn=lambda: inference(
|
| 144 |
+
{"task": "token-classification", "model": "openai/privacy-filter"},
|
| 145 |
+
"My name is Alice Smith",
|
| 146 |
+
{},
|
| 147 |
+
),
|
| 148 |
+
inputs=None,
|
| 149 |
+
outputs=ex3_out,
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
gr.Markdown("## API Example")
|
| 153 |
gr.Code("""from gradio_client import Client
|
| 154 |
|
|
|
|
| 160 |
api_name="/inference"
|
| 161 |
))""", language="python")
|
| 162 |
|
| 163 |
+
demo.launch(css=APP_CSS)
|