InnerI commited on
Commit
b50b730
·
verified ·
1 Parent(s): 78c8f0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -29
app.py CHANGED
@@ -154,51 +154,55 @@ def survival_checklist(days:int=7, people:int=2):
154
  return "\n".join(items)
155
 
156
  # ============ Gradio UI ============
157
- with gr.Blocks(title="Vertical AI: Agriculture Homesteading Off-Grid Preparedness") as demo:
158
- gr.Markdown("## 🌱 Vertical AI Copilot — Agriculture Homesteading Off-Grid Preparedness")
159
 
 
160
  with gr.Tab("Agri Copilot (Chat)"):
161
  chat_in = gr.Textbox(label="Question", lines=3)
162
  temp = gr.Slider(0.0, 1.0, value=0.2, step=0.05, label="Creativity")
163
  out = gr.Textbox(label="Response", lines=12)
164
  ask_btn = gr.Button("Answer")
165
- def answer_plain(q, t):
166
- return llm(f"You are a practical agriculture/off-grid assistant.\n\nQ: {q}\nA:", temperature=t)
 
 
 
167
  ask_btn.click(answer_plain, [chat_in, temp], [out])
168
 
 
169
  with gr.Tab("Knowledge (RAG)"):
170
  ns2 = gr.Textbox(value="farm-commons", label="Namespace")
171
  up = gr.Files(label="Upload .txt/.md", file_types=["text"])
172
  add_btn = gr.Button("Add to Knowledge")
173
  add_out = gr.Textbox(label="Indexer Output")
 
174
  def add_action(namespace, files):
175
  docs = []
176
  if files:
177
  for f in files:
178
  try:
179
- # Try reading as text directly
180
- if hasattr(f, "name"):
181
- name = os.path.basename(f.name)
 
 
 
 
 
182
  else:
183
- name = "uploaded.txt"
184
-
185
- # Open file path safely
186
- if hasattr(f, "read"):
187
- content = f.read()
188
- if isinstance(content, bytes):
189
- content = content.decode("utf-8", errors="ignore")
190
- elif hasattr(f, "decode"):
191
- content = f.decode("utf-8", errors="ignore")
192
- else:
193
- with open(f, "r", encoding="utf-8", errors="ignore") as fh:
194
- content = fh.read()
195
-
196
- docs.append((name, content))
197
- except Exception as e:
198
- return f"Error reading {f}: {e}"
199
- if not docs:
200
- return "No files processed."
201
- return add_docs(namespace, docs)
202
  add_btn.click(add_action, [ns2, up], [add_out])
203
 
204
  q2 = gr.Textbox(label="Question", lines=3)
@@ -207,9 +211,16 @@ with gr.Blocks(title="Vertical AI: Agriculture • Homesteading • Off-Grid •
207
  ask2 = gr.Button("Answer with RAG")
208
  ans2 = gr.Textbox(label="Answer", lines=12)
209
  src2 = gr.Textbox(label="Sources", lines=8)
210
- ask2.click(lambda ns, q, t, topk: rag_answer(ns, q, t, int(topk)), [ns2, q2, t2, k], [ans2, src2])
211
 
 
 
 
 
 
 
 
212
  with gr.Tab("Tools"):
 
213
  crop = gr.Textbox(label="Crop", value="Carrot")
214
  bl = gr.Number(label="Bed length (ft)", value=50)
215
  bw = gr.Number(label="Bed width (ft)", value=2.5)
@@ -219,6 +230,7 @@ with gr.Blocks(title="Vertical AI: Agriculture • Homesteading • Off-Grid •
219
  out1 = gr.JSON(label="Plan")
220
  calc1.click(seed_bed_planner, [crop, bl, bw, ir, rs], [out1])
221
 
 
222
  dwh = gr.Number(label="Daily energy use (Wh/day)", value=3000)
223
  sun = gr.Number(label="Sun hours/day", value=5.0)
224
  volt = gr.Number(label="System voltage (V)", value=24.0)
@@ -226,13 +238,19 @@ with gr.Blocks(title="Vertical AI: Agriculture • Homesteading • Off-Grid •
226
  out2 = gr.JSON(label="Sizing")
227
  calc2.click(solar_offgrid_sizer, [dwh, sun, volt], [out2])
228
 
 
229
  days = gr.Number(label="Days", value=7)
230
  ppl = gr.Number(label="People", value=2)
231
  calc3 = gr.Button("Build Checklist")
232
  out3 = gr.Textbox(label="Checklist", lines=10)
233
  calc3.click(survival_checklist, [days, ppl], [out3])
234
 
 
235
  with gr.Tab("About & Safety"):
236
- gr.Markdown("**Safety:** General guidance only. Follow local laws, building/electrical codes, and best practices.")
 
 
 
 
237
 
238
- demo.launch()
 
154
  return "\n".join(items)
155
 
156
  # ============ Gradio UI ============
157
+ with gr.Blocks(title="Vertical AI: Agriculture - Homesteading - Off-Grid - Preparedness") as demo:
158
+ gr.Markdown("## Vertical AI Copilot — Agriculture · Homesteading · Off-Grid · Preparedness")
159
 
160
+ # ---------------- Agri Copilot (Chat) ----------------
161
  with gr.Tab("Agri Copilot (Chat)"):
162
  chat_in = gr.Textbox(label="Question", lines=3)
163
  temp = gr.Slider(0.0, 1.0, value=0.2, step=0.05, label="Creativity")
164
  out = gr.Textbox(label="Response", lines=12)
165
  ask_btn = gr.Button("Answer")
166
+
167
+ def answer_plain(q, t):
168
+ prompt = "You are a practical agriculture/off-grid assistant.\n\nQ: {}\nA:".format(q)
169
+ return llm(prompt, temperature=t)
170
+
171
  ask_btn.click(answer_plain, [chat_in, temp], [out])
172
 
173
+ # ---------------- Knowledge (RAG) ----------------
174
  with gr.Tab("Knowledge (RAG)"):
175
  ns2 = gr.Textbox(value="farm-commons", label="Namespace")
176
  up = gr.Files(label="Upload .txt/.md", file_types=["text"])
177
  add_btn = gr.Button("Add to Knowledge")
178
  add_out = gr.Textbox(label="Indexer Output")
179
+
180
  def add_action(namespace, files):
181
  docs = []
182
  if files:
183
  for f in files:
184
  try:
185
+ # Name
186
+ name = os.path.basename(getattr(f, "name", "uploaded.txt"))
187
+
188
+ # Read as text safely
189
+ if hasattr(f, "read"):
190
+ content = f.read()
191
+ if isinstance(content, bytes):
192
+ content = content.decode("utf-8", errors="ignore")
193
  else:
194
+ # f may be a path-like
195
+ with open(f, "r", encoding="utf-8", errors="ignore") as fh:
196
+ content = fh.read()
197
+
198
+ docs.append((name, content))
199
+ except Exception as e:
200
+ return f"Error reading {name}: {e}"
201
+
202
+ if not docs:
203
+ return "No files processed."
204
+ return add_docs(namespace, docs)
205
+
 
 
 
 
 
 
 
206
  add_btn.click(add_action, [ns2, up], [add_out])
207
 
208
  q2 = gr.Textbox(label="Question", lines=3)
 
211
  ask2 = gr.Button("Answer with RAG")
212
  ans2 = gr.Textbox(label="Answer", lines=12)
213
  src2 = gr.Textbox(label="Sources", lines=8)
 
214
 
215
+ ask2.click(
216
+ lambda ns, q, t, topk: rag_answer(ns, q, t, int(topk)),
217
+ [ns2, q2, t2, k],
218
+ [ans2, src2]
219
+ )
220
+
221
+ # ---------------- Tools ----------------
222
  with gr.Tab("Tools"):
223
+ # Seed/Bed Planner
224
  crop = gr.Textbox(label="Crop", value="Carrot")
225
  bl = gr.Number(label="Bed length (ft)", value=50)
226
  bw = gr.Number(label="Bed width (ft)", value=2.5)
 
230
  out1 = gr.JSON(label="Plan")
231
  calc1.click(seed_bed_planner, [crop, bl, bw, ir, rs], [out1])
232
 
233
+ # Off-Grid Solar Sizer
234
  dwh = gr.Number(label="Daily energy use (Wh/day)", value=3000)
235
  sun = gr.Number(label="Sun hours/day", value=5.0)
236
  volt = gr.Number(label="System voltage (V)", value=24.0)
 
238
  out2 = gr.JSON(label="Sizing")
239
  calc2.click(solar_offgrid_sizer, [dwh, sun, volt], [out2])
240
 
241
+ # Survival Checklist
242
  days = gr.Number(label="Days", value=7)
243
  ppl = gr.Number(label="People", value=2)
244
  calc3 = gr.Button("Build Checklist")
245
  out3 = gr.Textbox(label="Checklist", lines=10)
246
  calc3.click(survival_checklist, [days, ppl], [out3])
247
 
248
+ # ---------------- About ----------------
249
  with gr.Tab("About & Safety"):
250
+ gr.Markdown(
251
+ "**Safety:** General guidance only. Follow local laws, building/electrical codes, and best practices."
252
+ )
253
+
254
+ demo.launch()
255
 
256
+