Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,214 +1,159 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import uuid
|
| 3 |
-
import json
|
| 4 |
import boto3
|
| 5 |
-
import
|
| 6 |
|
| 7 |
# -----------------------------
|
| 8 |
-
#
|
| 9 |
# -----------------------------
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"title": block_type.upper(),
|
| 16 |
-
"var": f"{block_type}_{str(uuid.uuid4())[:4]}",
|
| 17 |
-
"value": "",
|
| 18 |
-
"prompt": "",
|
| 19 |
-
"system": "",
|
| 20 |
-
"output": ""
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
def resolve_vars(text, variables):
|
| 24 |
-
return re.sub(
|
| 25 |
-
r"\{\{(\w+)\}\}",
|
| 26 |
-
lambda m: variables.get(m.group(1), m.group(0)),
|
| 27 |
-
text or ""
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
# AI CALL
|
| 32 |
-
# -----------------------------
|
| 33 |
|
| 34 |
-
def call_claude(prompt, system):
|
| 35 |
-
client = boto3.client("bedrock-runtime", region_name="us-east-1")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
result = json.loads(response["body"].read())
|
| 49 |
-
return result["content"][0]["text"]
|
| 50 |
|
| 51 |
# -----------------------------
|
| 52 |
-
#
|
| 53 |
# -----------------------------
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
for
|
| 60 |
-
|
| 61 |
-
if b["type"] == "text":
|
| 62 |
-
variables[b["var"]] = b["value"]
|
| 63 |
|
| 64 |
-
elif b["type"] == "ai":
|
| 65 |
-
prompt = resolve_vars(b["prompt"], variables)
|
| 66 |
-
system = b["system"] or "You are helpful"
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
except Exception as e:
|
| 75 |
-
logs.append(f"❌ {b['title']} failed: {str(e)}")
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# -----------------------------
|
| 82 |
|
| 83 |
-
|
| 84 |
-
return [f"{i}: {b['title']}" for i, b in enumerate(blocks)]
|
| 85 |
|
| 86 |
-
def add_block(blocks, block_type):
|
| 87 |
-
blocks.append(create_block(block_type))
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
if not selected:
|
| 96 |
-
return "", "", "", "", ""
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
-
return
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
idx = int(selected.split(":")[0])
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
"var": var,
|
| 111 |
-
"value": value,
|
| 112 |
-
"prompt": prompt,
|
| 113 |
-
"system": system
|
| 114 |
-
})
|
| 115 |
|
| 116 |
-
return blocks
|
| 117 |
|
| 118 |
# -----------------------------
|
| 119 |
# UI
|
| 120 |
# -----------------------------
|
| 121 |
|
| 122 |
-
with gr.Blocks() as
|
| 123 |
|
| 124 |
-
|
| 125 |
|
| 126 |
-
gr.
|
| 127 |
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
with gr.Column(scale=1):
|
| 132 |
-
gr.Markdown("## 🧩 Blocks")
|
| 133 |
-
add_text = gr.Button("➕ Text Input")
|
| 134 |
-
add_ai = gr.Button("➕ AI Block")
|
| 135 |
-
|
| 136 |
-
# CANVAS (Dropdown instead of Radio)
|
| 137 |
-
with gr.Column(scale=2):
|
| 138 |
-
gr.Markdown("## 🧠 Canvas")
|
| 139 |
-
|
| 140 |
-
block_selector = gr.Dropdown(
|
| 141 |
-
choices=[],
|
| 142 |
-
value=None,
|
| 143 |
-
label="Select Block"
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
# INSPECTOR
|
| 147 |
-
with gr.Column(scale=2):
|
| 148 |
-
gr.Markdown("## ⚙️ Inspector")
|
| 149 |
-
|
| 150 |
-
var = gr.Textbox(label="Variable")
|
| 151 |
-
value = gr.Textbox(label="Value")
|
| 152 |
-
prompt = gr.Textbox(label="Prompt")
|
| 153 |
-
system = gr.Textbox(label="System Prompt")
|
| 154 |
-
output = gr.Textbox(label="Output", interactive=False)
|
| 155 |
-
|
| 156 |
-
# RUN
|
| 157 |
-
run_btn = gr.Button("▶ Run Flow")
|
| 158 |
-
variables_out = gr.JSON(label="Variables")
|
| 159 |
-
logs = gr.Textbox(label="Logs")
|
| 160 |
-
|
| 161 |
-
# ---------------- EVENTS ----------------
|
| 162 |
-
|
| 163 |
-
add_text.click(
|
| 164 |
-
fn=lambda b: add_block(b, "text"),
|
| 165 |
-
inputs=blocks_state,
|
| 166 |
-
outputs=[blocks_state, block_selector, block_selector]
|
| 167 |
-
)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
fn=lambda b: add_block(b, "ai"),
|
| 171 |
-
inputs=blocks_state,
|
| 172 |
-
outputs=[blocks_state, block_selector, block_selector]
|
| 173 |
-
)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
)
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
fn=
|
| 189 |
-
inputs=[
|
| 190 |
-
outputs=
|
| 191 |
)
|
| 192 |
|
| 193 |
-
|
| 194 |
-
fn=
|
| 195 |
-
inputs=[
|
| 196 |
-
outputs=
|
| 197 |
)
|
| 198 |
|
| 199 |
-
|
| 200 |
-
fn=
|
| 201 |
-
inputs=[
|
| 202 |
-
outputs=
|
| 203 |
)
|
| 204 |
|
| 205 |
-
|
| 206 |
-
fn=
|
| 207 |
-
inputs=
|
| 208 |
-
outputs=
|
| 209 |
)
|
| 210 |
|
| 211 |
-
|
| 212 |
-
demo.launch(css="""
|
| 213 |
-
body { background-color: #0f172a; color: white; }
|
| 214 |
-
""")
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import boto3
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
+
# AWS BEDROCK SETUP
|
| 7 |
# -----------------------------
|
| 8 |
|
| 9 |
+
bedrock = boto3.client(
|
| 10 |
+
service_name="bedrock-runtime",
|
| 11 |
+
region_name="us-east-1" # change if needed
|
| 12 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
MODEL_ID = "anthropic.claude-3-5-sonnet-20240620-v1:0"
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def call_llm(prompt):
|
| 18 |
+
try:
|
| 19 |
+
body = json.dumps({
|
| 20 |
+
"anthropic_version": "bedrock-2023-05-31",
|
| 21 |
+
"messages": [
|
| 22 |
+
{"role": "user", "content": prompt}
|
| 23 |
+
],
|
| 24 |
+
"max_tokens": 500,
|
| 25 |
+
"temperature": 0.7
|
| 26 |
+
})
|
| 27 |
|
| 28 |
+
response = bedrock.invoke_model(
|
| 29 |
+
body=body,
|
| 30 |
+
modelId=MODEL_ID,
|
| 31 |
+
contentType="application/json"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
response_body = json.loads(response["body"].read())
|
| 35 |
+
|
| 36 |
+
return response_body["content"][0]["text"]
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"ERROR: {str(e)}"
|
| 40 |
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# -----------------------------
|
| 43 |
+
# BLOCK LOGIC
|
| 44 |
# -----------------------------
|
| 45 |
|
| 46 |
+
def create_blocks(text):
|
| 47 |
+
lines = [line.strip() for line in text.split("\n") if line.strip()]
|
| 48 |
+
if not lines:
|
| 49 |
+
lines = ["EMPTY"]
|
| 50 |
|
| 51 |
+
blocks = [f"{i}: {line}" for i, line in enumerate(lines)]
|
| 52 |
+
return blocks, gr.update(choices=blocks, value=blocks[0])
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
def select_block(blocks, selected):
|
| 56 |
+
if not blocks or not selected:
|
| 57 |
+
return ""
|
| 58 |
|
| 59 |
+
idx = int(selected.split(":")[0])
|
| 60 |
+
return blocks[idx].split(": ", 1)[1]
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
def update_block(blocks, selected, new_text):
|
| 64 |
+
if not blocks or not selected:
|
| 65 |
+
return blocks, gr.update()
|
| 66 |
|
| 67 |
+
idx = int(selected.split(":")[0])
|
| 68 |
+
blocks[idx] = f"{idx}: {new_text}"
|
|
|
|
| 69 |
|
| 70 |
+
return blocks, gr.update(choices=blocks, value=blocks[idx])
|
|
|
|
| 71 |
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
def ai_rewrite(blocks, selected):
|
| 74 |
+
if not blocks or not selected:
|
| 75 |
+
return blocks, gr.update()
|
| 76 |
|
| 77 |
+
idx = int(selected.split(":")[0])
|
| 78 |
+
original = blocks[idx].split(": ", 1)[1]
|
| 79 |
|
| 80 |
+
prompt = f"Rewrite this more clearly and professionally:\n\n{original}"
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
new_text = call_llm(prompt)
|
| 83 |
+
|
| 84 |
+
blocks[idx] = f"{idx}: {new_text}"
|
| 85 |
|
| 86 |
+
return blocks, gr.update(choices=blocks, value=blocks[idx])
|
| 87 |
|
| 88 |
+
|
| 89 |
+
def ai_summarize(blocks, selected):
|
| 90 |
+
if not blocks or not selected:
|
| 91 |
+
return ""
|
| 92 |
|
| 93 |
idx = int(selected.split(":")[0])
|
| 94 |
+
text = blocks[idx].split(": ", 1)[1]
|
| 95 |
+
|
| 96 |
+
prompt = f"Summarize this in 1 sentence:\n\n{text}"
|
| 97 |
|
| 98 |
+
return call_llm(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
| 100 |
|
| 101 |
# -----------------------------
|
| 102 |
# UI
|
| 103 |
# -----------------------------
|
| 104 |
|
| 105 |
+
with gr.Blocks(title="Blocksmith + Bedrock") as app:
|
| 106 |
|
| 107 |
+
gr.Markdown("# 🧱 Blocksmith (AI Powered via Bedrock)")
|
| 108 |
|
| 109 |
+
state_blocks = gr.State([])
|
| 110 |
|
| 111 |
+
input_text = gr.Textbox(label="Paste Text", lines=8)
|
| 112 |
+
create_btn = gr.Button("Create Blocks")
|
| 113 |
|
| 114 |
+
block_selector = gr.Dropdown(choices=[], label="Blocks", interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
block_editor = gr.Textbox(label="Edit Block")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
with gr.Row():
|
| 119 |
+
update_btn = gr.Button("Update")
|
| 120 |
+
rewrite_btn = gr.Button("✨ Rewrite (AI)")
|
| 121 |
+
summarize_btn = gr.Button("🧠 Summarize")
|
|
|
|
| 122 |
|
| 123 |
+
summary_output = gr.Textbox(label="Summary")
|
| 124 |
+
|
| 125 |
+
# -----------------------------
|
| 126 |
+
# EVENTS
|
| 127 |
+
# -----------------------------
|
| 128 |
+
|
| 129 |
+
create_btn.click(
|
| 130 |
+
fn=create_blocks,
|
| 131 |
+
inputs=input_text,
|
| 132 |
+
outputs=[state_blocks, block_selector]
|
| 133 |
)
|
| 134 |
|
| 135 |
+
block_selector.change(
|
| 136 |
+
fn=select_block,
|
| 137 |
+
inputs=[state_blocks, block_selector],
|
| 138 |
+
outputs=block_editor
|
| 139 |
)
|
| 140 |
|
| 141 |
+
update_btn.click(
|
| 142 |
+
fn=update_block,
|
| 143 |
+
inputs=[state_blocks, block_selector, block_editor],
|
| 144 |
+
outputs=[state_blocks, block_selector]
|
| 145 |
)
|
| 146 |
|
| 147 |
+
rewrite_btn.click(
|
| 148 |
+
fn=ai_rewrite,
|
| 149 |
+
inputs=[state_blocks, block_selector],
|
| 150 |
+
outputs=[state_blocks, block_selector]
|
| 151 |
)
|
| 152 |
|
| 153 |
+
summarize_btn.click(
|
| 154 |
+
fn=ai_summarize,
|
| 155 |
+
inputs=[state_blocks, block_selector],
|
| 156 |
+
outputs=summary_output
|
| 157 |
)
|
| 158 |
|
| 159 |
+
app.launch()
|
|
|
|
|
|
|
|
|