ChaoticEconomist commited on
Commit
10568f0
Β·
verified Β·
1 Parent(s): ee58796

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -21,27 +21,24 @@ def call_llm(prompt):
21
  }
22
 
23
  payload = {
24
- # βœ… FIXED MODEL
25
  "model": "grok-4-1-fast-non-reasoning",
26
-
27
  "messages": [
28
  {"role": "system", "content": "You are a helpful AI assistant."},
29
  {"role": "user", "content": prompt}
30
  ],
31
  "temperature": 0.7,
32
- "max_tokens": 300
33
  }
34
 
35
  response = requests.post(
36
  API_URL,
37
  headers=headers,
38
  json=payload,
39
- timeout=30 # βœ… prevents hanging
40
  )
41
 
42
  data = response.json()
43
 
44
- # βœ… CRITICAL FIX (no more 'choices' crash)
45
  if "choices" not in data:
46
  return f"❌ API Error:\n{data}"
47
 
@@ -86,6 +83,7 @@ def update_block(blocks, selected, new_text):
86
  # AI FUNCTIONS
87
  # -----------------------------
88
 
 
89
  def ai_rewrite(blocks, selected):
90
  if not blocks or not selected:
91
  return blocks, gr.update()
@@ -102,16 +100,28 @@ def ai_rewrite(blocks, selected):
102
  return blocks, gr.update(choices=blocks, value=blocks[idx])
103
 
104
 
105
- def ai_summarize(blocks, selected):
106
- if not blocks or not selected:
107
- return ""
 
108
 
109
- idx = int(selected.split(":")[0])
110
- text = blocks[idx].split(": ", 1)[1]
 
 
 
 
 
111
 
112
- prompt = f"Summarize this in one concise sentence:\n\n{text}"
113
 
114
- return call_llm(prompt)
 
 
 
 
 
 
115
 
116
 
117
  # -----------------------------
@@ -134,9 +144,7 @@ with gr.Blocks(title="Blocksmith + Grok") as app:
134
  with gr.Row():
135
  update_btn = gr.Button("Update")
136
  rewrite_btn = gr.Button("✨ Rewrite (AI)")
137
- summarize_btn = gr.Button("🧠 Summarize")
138
-
139
- summary_output = gr.Textbox(label="Summary")
140
 
141
  # -----------------------------
142
  # EVENTS
@@ -166,10 +174,10 @@ with gr.Blocks(title="Blocksmith + Grok") as app:
166
  outputs=[state_blocks, block_selector]
167
  )
168
 
169
- summarize_btn.click(
170
- fn=ai_summarize,
171
- inputs=[state_blocks, block_selector],
172
- outputs=summary_output
173
  )
174
 
175
  app.launch()
 
21
  }
22
 
23
  payload = {
 
24
  "model": "grok-4-1-fast-non-reasoning",
 
25
  "messages": [
26
  {"role": "system", "content": "You are a helpful AI assistant."},
27
  {"role": "user", "content": prompt}
28
  ],
29
  "temperature": 0.7,
30
+ "max_tokens": 400
31
  }
32
 
33
  response = requests.post(
34
  API_URL,
35
  headers=headers,
36
  json=payload,
37
+ timeout=30
38
  )
39
 
40
  data = response.json()
41
 
 
42
  if "choices" not in data:
43
  return f"❌ API Error:\n{data}"
44
 
 
83
  # AI FUNCTIONS
84
  # -----------------------------
85
 
86
+ # ✨ Rewrite selected block
87
  def ai_rewrite(blocks, selected):
88
  if not blocks or not selected:
89
  return blocks, gr.update()
 
100
  return blocks, gr.update(choices=blocks, value=blocks[idx])
101
 
102
 
103
+ # πŸš€ Rewrite entire document
104
+ def ai_rewrite_all(blocks):
105
+ if not blocks:
106
+ return blocks, gr.update()
107
 
108
+ full_text = "\n".join([b.split(": ", 1)[1] for b in blocks])
109
+
110
+ prompt = f"""
111
+ Rewrite the following text to be clearer, more professional, and well-structured:
112
+
113
+ {full_text}
114
+ """
115
 
116
+ rewritten = call_llm(prompt)
117
 
118
+ lines = [line.strip() for line in rewritten.split("\n") if line.strip()]
119
+ if not lines:
120
+ lines = ["EMPTY"]
121
+
122
+ new_blocks = [f"{i}: {line}" for i, line in enumerate(lines)]
123
+
124
+ return new_blocks, gr.update(choices=new_blocks, value=new_blocks[0])
125
 
126
 
127
  # -----------------------------
 
144
  with gr.Row():
145
  update_btn = gr.Button("Update")
146
  rewrite_btn = gr.Button("✨ Rewrite (AI)")
147
+ rewrite_all_btn = gr.Button("πŸš€ Rewrite All (AI)")
 
 
148
 
149
  # -----------------------------
150
  # EVENTS
 
174
  outputs=[state_blocks, block_selector]
175
  )
176
 
177
+ rewrite_all_btn.click(
178
+ fn=ai_rewrite_all,
179
+ inputs=state_blocks,
180
+ outputs=[state_blocks, block_selector]
181
  )
182
 
183
  app.launch()