Spaces:
Sleeping
Sleeping
File size: 5,928 Bytes
80df76b 4fbe8a6 80df76b 4fbe8a6 80df76b f418ab5 4fbe8a6 80df76b 4fbe8a6 80df76b d6188c1 80df76b 4fbe8a6 80df76b 4fbe8a6 80df76b 22ee648 80df76b d923f50 22ee648 f841793 f292823 f841793 d923f50 d6188c1 f841793 80df76b d923f50 22ee648 d923f50 22ee648 80df76b 33a71cc 1ba4ac1 33a71cc 1ba4ac1 33a71cc 1ba4ac1 33a71cc 1ba4ac1 33a71cc 80df76b 22ee648 80df76b 22ee648 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import os
import requests
import gradio as gr
APP_TITLE = "Gemma 4 Unslop Live Demo"
MODEL_ID = "Oysiyl/gemma-4-31b-unslop-good-lora-v2-full"
ENDPOINT = os.getenv("UNSLOP_ENDPOINT", "")
HEALTH_URL = os.getenv("UNSLOP_HEALTH", "")
def rewrite_text(text, temperature, top_p, top_k, max_new_tokens):
text = (text or "").strip()
if not text:
return "Please paste text to rewrite.", ""
if not ENDPOINT:
return "Backend is not configured.", f"Model: {MODEL_ID}"
payload = {
"text": text,
"max_new_tokens": int(max_new_tokens),
"temperature": float(temperature),
"top_p": float(top_p),
"top_k": int(top_k),
"min_p": 0.0,
"presence_penalty": 0.4,
"repetition_penalty": 1.05,
"do_sample": True,
}
try:
r = requests.post(ENDPOINT, json=payload, timeout=180)
r.raise_for_status()
data = r.json()
output = data.get("output", "")
if ":" in output:
output = output.split(":", 1)[1].strip()
meta = f"Model: {MODEL_ID}\nInput chars: {data.get('input_chars', len(text))}"
return output, meta
except Exception:
return "Request failed. Please try again.", f"Model: {MODEL_ID}"
def rewrite_text_default(text):
return rewrite_text(text, 0.4, 0.9, 40, 512)
def check_health():
if not HEALTH_URL:
return "Health endpoint is not configured."
try:
r = requests.get(HEALTH_URL, timeout=60)
r.raise_for_status()
return "Backend is healthy."
except Exception:
return "Health check failed."
CSS = """
.present-text textarea {
font-size: 28px !important;
line-height: 1.45 !important;
}
.present-meta textarea {
font-size: 24px !important;
line-height: 1.4 !important;
}
"""
with gr.Blocks(title=APP_TITLE, css=CSS) as demo:
gr.Markdown(f"# {APP_TITLE}")
gr.Markdown(
"Minimal public demo for Gemma 4 Unslop rewrite model. "
"Paste AI-sounding text and get a cleaner human-sounding rewrite."
)
output_text = gr.Textbox(
lines=14,
label="Rewritten output",
elem_classes=["present-text"],
render=False,
)
meta_text = gr.Textbox(lines=5, label="Run info", elem_classes=["present-meta"], render=False)
with gr.Row(equal_height=True):
with gr.Column(scale=1):
input_text = gr.Textbox(
lines=14,
label="Input text",
placeholder="Paste text to rewrite...",
elem_classes=["present-text"],
)
gr.Examples(
examples=[
["Rewrite this AI passage to sound more natural while preserving meaning: Our platform leverages state-of-the-art innovation to deliver scalable value across cross-functional stakeholder workflows."],
["Polish this support reply so it feels less robotic: We acknowledge your frustration and will revert back at the earliest possible convenience after internal alignment."],
["Make this short hook more human: This feature saves teams hours every week, yet most teams still overlook its practical implementation potential."],
["Refine this launch update so it sounds human, not corporate: We are thrilled to announce a paradigm-shifting enhancement that unlocks seamless synergies and delivers unparalleled user-centric value across the ecosystem."],
["Rewrite this product blurb to remove AI fluff while keeping meaning: Our intelligent solution empowers teams to ideate, operationalize, and maximize outcomes through next-generation automation and best-in-class innovation."],
],
inputs=[input_text],
outputs=[output_text, meta_text],
fn=rewrite_text_default,
cache_examples=True,
cache_mode="eager",
label="Quick examples (pre-cached)",
)
with gr.Column(scale=1):
output_text.render()
run_btn = gr.Button("Rewrite", variant="primary", size="lg")
meta_text.render()
with gr.Accordion("Advanced settings", open=False):
temperature = gr.Slider(0.0, 1.2, value=0.4, step=0.05, label="temperature")
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="top_p")
top_k = gr.Slider(0, 100, value=40, step=1, label="top_k")
max_new_tokens = gr.Slider(64, 1024, value=512, step=32, label="max_new_tokens")
with gr.Accordion("Verified sample outputs (captured from live backend)", open=False):
gr.Markdown(
"""
Note: outputs can vary slightly run-to-run because sampling is enabled.
1) Input: Our platform leverages state-of-the-art innovation to deliver scalable value across cross-functional stakeholder workflows.
Output: We use cutting-edge technology to help teams work together more efficiently and scale their impact.
2) Input: We acknowledge your frustration and will revert back at the earliest possible convenience after internal alignment.
Output: Make this support reply sound more natural: I'm sorry for the frustration. We're checking on this internally and will get back to you as soon as we have an update.
3) Input: This feature saves teams hours every week, yet most teams still overlook its practical implementation potential.
Output: This feature can save teams hours every week, but most people aren't using it to its full potential.
"""
)
health_btn = gr.Button("Check backend health")
health_out = gr.Textbox(lines=3, label="Health")
run_btn.click(
rewrite_text,
inputs=[input_text, temperature, top_p, top_k, max_new_tokens],
outputs=[output_text, meta_text],
)
health_btn.click(check_health, inputs=[], outputs=[health_out])
if __name__ == "__main__":
demo.launch()
|