Fix: POST /compress endpoint, simplify dependencies
Browse files- app.py +23 -113
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -1,148 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
-
import tiktoken
|
| 5 |
|
| 6 |
-
API_URL = "https://api.ilang.ai"
|
| 7 |
-
|
| 8 |
-
def count_tokens(text):
|
| 9 |
-
"""Estimate token count using cl100k_base."""
|
| 10 |
-
try:
|
| 11 |
-
enc = tiktoken.get_encoding("cl100k_base")
|
| 12 |
-
return len(enc.encode(text))
|
| 13 |
-
except Exception:
|
| 14 |
-
return len(text.split()) * 1.3 # fallback rough estimate
|
| 15 |
|
| 16 |
def compress(text, ratio):
|
| 17 |
-
"""Call api.ilang.ai to compress natural language to I-Lang."""
|
| 18 |
if not text.strip():
|
| 19 |
-
return "", ""
|
| 20 |
-
|
| 21 |
try:
|
| 22 |
-
resp = requests.post(
|
| 23 |
-
API_URL,
|
| 24 |
-
json={"input": text, "ratio": ratio / 100},
|
| 25 |
-
headers={"Content-Type": "application/json"},
|
| 26 |
-
timeout=30,
|
| 27 |
-
)
|
| 28 |
if resp.status_code == 200:
|
| 29 |
data = resp.json()
|
| 30 |
-
|
|
|
|
| 31 |
elif resp.status_code == 429:
|
| 32 |
-
return
|
| 33 |
else:
|
| 34 |
-
return f"API error {resp.status_code}", ""
|
| 35 |
except Exception as e:
|
| 36 |
-
return f"Connection error: {e}", ""
|
| 37 |
-
|
| 38 |
-
original_tokens = count_tokens(text)
|
| 39 |
-
compressed_tokens = count_tokens(compressed)
|
| 40 |
-
if original_tokens > 0:
|
| 41 |
-
actual_ratio = (1 - compressed_tokens / original_tokens) * 100
|
| 42 |
-
savings = f"{original_tokens} tokens -> {compressed_tokens} tokens ({actual_ratio:.0f}% saved)"
|
| 43 |
-
else:
|
| 44 |
-
savings = ""
|
| 45 |
-
|
| 46 |
-
return compressed, savings, f"Original: {len(text)} chars | Compressed: {len(compressed)} chars"
|
| 47 |
|
| 48 |
def decompress(ilang_text):
|
| 49 |
-
"""Call api.ilang.ai to expand I-Lang back to natural language."""
|
| 50 |
if not ilang_text.strip():
|
| 51 |
return ""
|
| 52 |
-
|
| 53 |
-
prompt = f"Expand this I-Lang notation back to clear, natural English:\n\n{ilang_text}"
|
| 54 |
try:
|
| 55 |
-
resp = requests.post(
|
| 56 |
-
API_URL,
|
| 57 |
-
json={"input": prompt, "ratio": 0},
|
| 58 |
-
headers={"Content-Type": "application/json"},
|
| 59 |
-
timeout=30,
|
| 60 |
-
)
|
| 61 |
if resp.status_code == 200:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
else:
|
| 65 |
-
return f"API error {resp.status_code}"
|
| 66 |
except Exception as e:
|
| 67 |
return f"Connection error: {e}"
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
["Scrape Hacker News, filter AI-related posts, summarize top 5, format as markdown table, send to Telegram.", 70],
|
| 72 |
-
["Check if the user's input contains sensitive content. If yes, reject with explanation. If no, process normally and log the result.", 50],
|
| 73 |
-
]
|
| 74 |
-
|
| 75 |
-
with gr.Blocks(
|
| 76 |
-
title="I-Lang Protocol",
|
| 77 |
-
theme=gr.themes.Base(),
|
| 78 |
-
css="""
|
| 79 |
-
.header { text-align: center; margin-bottom: 1rem; }
|
| 80 |
-
.stats { font-family: monospace; color: #666; }
|
| 81 |
-
"""
|
| 82 |
-
) as demo:
|
| 83 |
-
|
| 84 |
-
gr.Markdown("""
|
| 85 |
-
# I-Lang: AI Communication Protocol
|
| 86 |
-
**Compress natural language into structured, machine-readable instructions.**
|
| 87 |
-
|
| 88 |
-
52 verbs | 28 modifiers | 14 entities | Pipe syntax | 40-65% compression
|
| 89 |
-
|
| 90 |
-
[GitHub](https://github.com/ilang-ai) | [Spec](https://github.com/ilang-ai/ilang-spec) | [Dictionary](https://github.com/ilang-ai/ilang-dict) | [Paper](https://doi.org/10.13140/RG.2.2.22821.97762)
|
| 91 |
-
""")
|
| 92 |
-
|
| 93 |
with gr.Tab("Compress"):
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column():
|
| 96 |
-
input_text = gr.Textbox(
|
| 97 |
-
label="Natural Language",
|
| 98 |
-
placeholder="Describe a task, workflow, or instruction...",
|
| 99 |
-
lines=6,
|
| 100 |
-
)
|
| 101 |
-
ratio_slider = gr.Slider(
|
| 102 |
-
minimum=20, maximum=90, value=60, step=5,
|
| 103 |
-
label="Target Compression (%)",
|
| 104 |
-
info="Higher = denser output, less human-readable"
|
| 105 |
-
)
|
| 106 |
compress_btn = gr.Button("Compress to I-Lang", variant="primary")
|
| 107 |
-
|
| 108 |
with gr.Column():
|
| 109 |
-
output_text = gr.Textbox(label="I-Lang Output", lines=
|
| 110 |
-
savings_text = gr.Textbox(label="Token Savings", interactive=False
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
compress_btn.click(
|
| 114 |
-
compress,
|
| 115 |
-
inputs=[input_text, ratio_slider],
|
| 116 |
-
outputs=[output_text, savings_text, chars_text],
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
gr.Examples(
|
| 120 |
-
examples=EXAMPLES,
|
| 121 |
-
inputs=[input_text, ratio_slider],
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
with gr.Tab("Expand"):
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column():
|
| 127 |
-
ilang_input = gr.Textbox(
|
| 128 |
-
label="I-Lang Input",
|
| 129 |
-
placeholder="Paste I-Lang notation here...",
|
| 130 |
-
lines=6,
|
| 131 |
-
)
|
| 132 |
expand_btn = gr.Button("Expand to Natural Language", variant="primary")
|
| 133 |
with gr.Column():
|
| 134 |
-
expanded_output = gr.Textbox(label="Natural Language", lines=
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
inputs=[ilang_input],
|
| 139 |
-
outputs=[expanded_output],
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
gr.Markdown("""
|
| 143 |
-
---
|
| 144 |
-
I-Lang v2.0 | Created by AI x Human collaboration | Palm Media Technology
|
| 145 |
-
""")
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
+
API_URL = "https://api.ilang.ai/compress"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def compress(text, ratio):
|
|
|
|
| 8 |
if not text.strip():
|
| 9 |
+
return "", ""
|
|
|
|
| 10 |
try:
|
| 11 |
+
resp = requests.post(API_URL, json={"input": text}, headers={"Content-Type": "application/json"}, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if resp.status_code == 200:
|
| 13 |
data = resp.json()
|
| 14 |
+
tokens = data.get("tokens", {})
|
| 15 |
+
return data.get("output", ""), f"{tokens.get('input','?')} -> {tokens.get('output','?')} tokens ({tokens.get('saved','?')} saved)"
|
| 16 |
elif resp.status_code == 429:
|
| 17 |
+
return resp.json().get("error", "Rate limited"), ""
|
| 18 |
else:
|
| 19 |
+
return resp.json().get("error", f"API error {resp.status_code}"), ""
|
| 20 |
except Exception as e:
|
| 21 |
+
return f"Connection error: {e}", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def decompress(ilang_text):
|
|
|
|
| 24 |
if not ilang_text.strip():
|
| 25 |
return ""
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
+
resp = requests.post(API_URL, json={"input": f"Expand this I-Lang back to clear natural English. Output ONLY natural language:\n\n{ilang_text}"}, headers={"Content-Type": "application/json"}, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if resp.status_code == 200:
|
| 29 |
+
return resp.json().get("output", "")
|
| 30 |
+
return f"API error {resp.status_code}"
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"Connection error: {e}"
|
| 33 |
|
| 34 |
+
with gr.Blocks(title="I-Lang Protocol", theme=gr.themes.Base()) as demo:
|
| 35 |
+
gr.Markdown("# I-Lang: AI Communication Protocol\n**Compress natural language into structured, machine-readable instructions.**\n\n52 verbs | Pipe syntax | 40-65% token savings\n\n[GitHub](https://github.com/ilang-ai) | [Spec](https://github.com/ilang-ai/ilang-spec) | [Dictionary](https://github.com/ilang-ai/ilang-dict) | [Training Data](https://huggingface.co/datasets/i-Lang/ilang-instruction-corpus) | [Paper](https://doi.org/10.13140/RG.2.2.22821.97762)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
with gr.Tab("Compress"):
|
| 37 |
with gr.Row():
|
| 38 |
with gr.Column():
|
| 39 |
+
input_text = gr.Textbox(label="Natural Language", placeholder="Describe a task, workflow, or image...", lines=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
compress_btn = gr.Button("Compress to I-Lang", variant="primary")
|
|
|
|
| 41 |
with gr.Column():
|
| 42 |
+
output_text = gr.Textbox(label="I-Lang Output", lines=5, show_copy_button=True)
|
| 43 |
+
savings_text = gr.Textbox(label="Token Savings", interactive=False)
|
| 44 |
+
compress_btn.click(compress, inputs=[input_text], outputs=[output_text, savings_text])
|
| 45 |
+
gr.Examples(examples=[["Read all customer feedback, filter negative sentiment, group by product, count issues, rank by frequency, export CSV."],["Read README from GitHub, translate to Chinese, save to cloud storage."],["Scrape Hacker News, filter AI posts, summarize top 5, format as markdown table."],["Create a cyberpunk cityscape at night with neon lights and rain reflections."]], inputs=[input_text])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
with gr.Tab("Expand"):
|
| 47 |
with gr.Row():
|
| 48 |
with gr.Column():
|
| 49 |
+
ilang_input = gr.Textbox(label="I-Lang Input", placeholder="Paste I-Lang notation...", lines=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
expand_btn = gr.Button("Expand to Natural Language", variant="primary")
|
| 51 |
with gr.Column():
|
| 52 |
+
expanded_output = gr.Textbox(label="Natural Language", lines=5, show_copy_button=True)
|
| 53 |
+
expand_btn.click(decompress, inputs=[ilang_input], outputs=[expanded_output])
|
| 54 |
+
gr.Examples(examples=[["[READ:@GH|path=readme.md]=>[TRANSLATE|lang=zh]=>[FMT|fmt=md]=>[WRITE:@R2]"],["[SCAN:@FILE|type=deps]=>[CHECK|type=security]=>[FILT|key=critical]=>[ALERT]=>[LOG]"]], inputs=[ilang_input])
|
| 55 |
+
gr.Markdown("---\nI-Lang v2.0 | [Palm Media Technology](https://ilang.ai)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
gradio>=5.0
|
| 2 |
requests
|
| 3 |
-
tiktoken
|
|
|
|
| 1 |
gradio>=5.0
|
| 2 |
requests
|
|
|