TheHickman commited on
Commit
bcdf54b
·
verified ·
1 Parent(s): 66b1fce

Adding switching between 4

Browse files
Files changed (1) hide show
  1. app.py +80 -20
app.py CHANGED
@@ -206,33 +206,93 @@ IMAGE_GENERATION_HTML = """
206
 
207
  # ---- Page switching function ----
208
  def switch_content(choice):
 
209
  pages = {
210
  "Text Generation": TEXT_GENERATION_HTML,
211
  "Question and Answer": QUESTION_ANSWER_HTML,
212
  "Summarisation": SUMMARISATION_HTML,
213
  "Image Generation": IMAGE_GENERATION_HTML,
214
  }
 
215
  return pages.get(choice, TEXT_GENERATION_HTML)
216
-
217
- nav_bar = gr.Radio(
218
- choices=[
219
- "Text Generation",
220
- "Question and Answer",
221
- "Summarisation",
222
- "Image Generation"
223
- ],
224
- value="Text Generation",
225
- label="Topics",
226
- interactive=True
227
- )
228
-
229
- content_display = gr.HTML(TEXT_GENERATION_HTML)
230
-
231
- nav_bar.change(
232
- fn=switch_content,
233
- inputs=nav_bar,
234
- outputs=content_display
235
- )
236
 
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  demo.launch(allowed_paths=["."])
 
206
 
207
  # ---- Page switching function ----
208
  def switch_content(choice):
209
+
210
  pages = {
211
  "Text Generation": TEXT_GENERATION_HTML,
212
  "Question and Answer": QUESTION_ANSWER_HTML,
213
  "Summarisation": SUMMARISATION_HTML,
214
  "Image Generation": IMAGE_GENERATION_HTML,
215
  }
216
+
217
  return pages.get(choice, TEXT_GENERATION_HTML)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
 
220
+ # =========================================================
221
+ # UI
222
+ # =========================================================
223
+
224
+ with gr.Blocks(head="""
225
+ <script>
226
+ document.addEventListener("mouseup", () => {
227
+
228
+ const selection = window.getSelection().toString().trim();
229
+
230
+ if (selection.length > 0) {
231
+
232
+ const textbox = document.querySelector(
233
+ 'textarea[data-testid="textbox"]'
234
+ );
235
+
236
+ if (textbox) {
237
+
238
+ const nativeInputValueSetter =
239
+ Object.getOwnPropertyDescriptor(
240
+ window.HTMLTextAreaElement.prototype,
241
+ "value"
242
+ ).set;
243
+
244
+ nativeInputValueSetter.call(textbox, selection);
245
+
246
+ textbox.dispatchEvent(
247
+ new Event("input", { bubbles: true })
248
+ );
249
+ }
250
+ }
251
+ });
252
+ </script>
253
+ """) as demo:
254
+
255
+ gr.Markdown("### 📘 Highlight text and ask GPT for help")
256
+
257
+ # ---- Navigation bar ----
258
+ nav_bar = gr.Radio(
259
+ choices=[
260
+ "Text Generation",
261
+ "Question and Answer",
262
+ "Summarisation",
263
+ "Image Generation"
264
+ ],
265
+ value="Text Generation",
266
+ label="Topics",
267
+ interactive=True
268
+ )
269
+
270
+ # ---- Main content ----
271
+ content_display = gr.HTML(TEXT_GENERATION_HTML)
272
+
273
+ nav_bar.change(
274
+ fn=switch_content,
275
+ inputs=nav_bar,
276
+ outputs=content_display
277
+ )
278
+
279
+ # ---- Selected text ----
280
+ selected_text = gr.Textbox(
281
+ label="Selected text",
282
+ placeholder="Highlight text above...",
283
+ lines=3,
284
+ interactive=True
285
+ )
286
+
287
+ # ---- Explain button ----
288
+ explain_btn = gr.Button("Explain selection 🧠")
289
+
290
+ output = gr.Markdown()
291
+
292
+ explain_btn.click(
293
+ fn=explain_text,
294
+ inputs=selected_text,
295
+ outputs=output,
296
+ )
297
+
298
  demo.launch(allowed_paths=["."])