ChaoticEconomist commited on
Commit
c4fe166
·
verified ·
1 Parent(s): 05d0c7e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -0
app.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import boto3
4
+ import uuid
5
+ import re
6
+
7
+ # -----------------------------
8
+ # GLOBAL STATE HELPERS
9
+ # -----------------------------
10
+
11
+ def new_block(block_type):
12
+ return {
13
+ "id": str(uuid.uuid4())[:8],
14
+ "type": block_type,
15
+ "var": f"{block_type}_{str(uuid.uuid4())[:4]}",
16
+ "title": block_type.upper(),
17
+ "value": "",
18
+ "prompt": "",
19
+ "system": "",
20
+ "output": "",
21
+ "options": [],
22
+ "min": 0,
23
+ "max": 100,
24
+ "step": 1,
25
+ }
26
+
27
+ def resolve_vars(text, variables):
28
+ def repl(match):
29
+ key = match.group(1)
30
+ return variables.get(key, f"{{{{{key}}}}}")
31
+ return re.sub(r"\{\{(\w+)\}\}", repl, text or "")
32
+
33
+ # -----------------------------
34
+ # AI CALL
35
+ # -----------------------------
36
+
37
+ def call_claude(prompt, system):
38
+ client = boto3.client("bedrock-runtime", region_name="us-east-1")
39
+
40
+ body = json.dumps({
41
+ "max_tokens": 1000,
42
+ "system": system,
43
+ "messages": [{"role": "user", "content": prompt}]
44
+ })
45
+
46
+ response = client.invoke_model(
47
+ modelId="anthropic.claude-3-sonnet-20240229-v1:0",
48
+ body=body
49
+ )
50
+
51
+ result = json.loads(response["body"].read())
52
+ return result["content"][0]["text"]
53
+
54
+ # -----------------------------
55
+ # BLOCK EXECUTION ENGINE
56
+ # -----------------------------
57
+
58
+ def run_blocks(blocks):
59
+ variables = {}
60
+ logs = []
61
+
62
+ for b in blocks:
63
+ try:
64
+ if b["type"] == "text-input":
65
+ variables[b["var"]] = b["value"]
66
+
67
+ elif b["type"] == "select-input":
68
+ variables[b["var"]] = b["value"]
69
+
70
+ elif b["type"] == "slider-input":
71
+ variables[b["var"]] = str(b["value"])
72
+
73
+ elif b["type"] in ["ai-text", "ai-transform"]:
74
+ prompt = resolve_vars(b["prompt"], variables)
75
+ system = b["system"] or "You are a helpful assistant."
76
+ output = call_claude(prompt, system)
77
+
78
+ b["output"] = output
79
+ variables[b["var"]] = output
80
+ logs.append(f"✅ {b['title']} ran")
81
+
82
+ elif b["type"] == "display":
83
+ ref = b.get("ref")
84
+ b["output"] = variables.get(ref, "")
85
+
86
+ except Exception as e:
87
+ logs.append(f"❌ {b['title']} failed: {str(e)}")
88
+ b["output"] = f"Error: {str(e)}"
89
+
90
+ return blocks, variables, "\n".join(logs)
91
+
92
+ # -----------------------------
93
+ # UI HELPERS
94
+ # -----------------------------
95
+
96
+ def add_block(blocks, block_type):
97
+ blocks.append(new_block(block_type))
98
+ return blocks, blocks
99
+
100
+ def update_block(blocks, idx, field, value):
101
+ blocks[idx][field] = value
102
+ return blocks
103
+
104
+ # -----------------------------
105
+ # UI RENDERER
106
+ # -----------------------------
107
+
108
+ def render_blocks(blocks):
109
+ ui = []
110
+
111
+ for i, b in enumerate(blocks):
112
+ with gr.Group():
113
+ gr.Markdown(f"### {b['title']} ({b['type']})")
114
+
115
+ var = gr.Textbox(value=b["var"], label="Variable")
116
+ val = gr.Textbox(value=b["value"], label="Value")
117
+
118
+ prompt = gr.Textbox(value=b["prompt"], label="Prompt")
119
+ system = gr.Textbox(value=b["system"], label="System Prompt")
120
+
121
+ out = gr.Textbox(value=b.get("output", ""), label="Output")
122
+
123
+ var.change(lambda v, blocks=blocks, i=i: update_block(blocks, i, "var", v), var, None)
124
+ val.change(lambda v, blocks=blocks, i=i: update_block(blocks, i, "value", v), val, None)
125
+ prompt.change(lambda v, blocks=blocks, i=i: update_block(blocks, i, "prompt", v), prompt, None)
126
+ system.change(lambda v, blocks=blocks, i=i: update_block(blocks, i, "system", v), system, None)
127
+
128
+ ui.append(gr.Markdown("---"))
129
+
130
+ return ui
131
+
132
+ # -----------------------------
133
+ # MAIN UI
134
+ # -----------------------------
135
+
136
+ with gr.Blocks(title="Blocksmith AI") as demo:
137
+
138
+ blocks_state = gr.State([])
139
+
140
+ gr.Markdown("# ⚡ Blocksmith (Gradio Version)")
141
+
142
+ with gr.Row():
143
+ add_text = gr.Button("➕ Text Input")
144
+ add_ai = gr.Button("➕ AI Generate")
145
+ add_display = gr.Button("➕ Display")
146
+
147
+ blocks_ui = gr.Column()
148
+
149
+ run_btn = gr.Button("▶ Run All")
150
+ output_vars = gr.JSON(label="Variables")
151
+ logs = gr.Textbox(label="Run Logs")
152
+
153
+ # ADD BLOCKS
154
+ add_text.click(lambda b: add_block(b, "text-input"), blocks_state, [blocks_state, blocks_ui])
155
+ add_ai.click(lambda b: add_block(b, "ai-text"), blocks_state, [blocks_state, blocks_ui])
156
+ add_display.click(lambda b: add_block(b, "display"), blocks_state, [blocks_state, blocks_ui])
157
+
158
+ # RUN
159
+ run_btn.click(run_blocks, blocks_state, [blocks_state, output_vars, logs])
160
+
161
+ demo.launch()