BotXT commited on
Commit
7817e26
·
verified ·
1 Parent(s): f0f59e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -2,11 +2,10 @@ import os
2
  from openai import OpenAI
3
  from shiny import App, render, ui, reactive
4
 
5
- # Ultra-safe, stripped layout to guarantee a clean launch
6
  app_ui = ui.page_fluid(
7
  ui.h2("NexusAI Assistant"),
8
  ui.hr(),
9
- # Fixed Input Layouts (No custom keywords that break compiler engines)
10
  ui.input_text_area("user_prompt", "Enter your prompt below:", "What is the meaning of life?"),
11
  ui.input_action_button("submit_btn", "Send Query"),
12
  ui.hr(),
@@ -16,7 +15,7 @@ app_ui = ui.page_fluid(
16
 
17
  def server(input, output, session):
18
 
19
- # Asynchronous task handling OpenRouter latency to stop infinite hangs
20
  @reactive.extended_task
21
  async def fetch_ai_response(prompt_text):
22
  try:
@@ -34,18 +33,20 @@ def server(input, output, session):
34
  messages=[{"role": "user", "content": prompt_text}]
35
  )
36
 
37
- # Target the content string directly inside the payload list
38
- return {"status": "success", "content": completion.choices[0].message.content}
 
 
39
  except Exception as e:
40
  return {"status": "error", "content": str(e)}
41
 
42
- # Trigger async routine when user clicks submit button
43
  @reactive.effect
44
  @reactive.event(input.submit_btn)
45
  def _():
46
  fetch_ai_response.trigger(input.user_prompt())
47
 
48
- # Dynamically update the app interface state
49
  @render.ui
50
  def ai_display_area():
51
  if fetch_ai_response.status() == "initial":
@@ -61,4 +62,4 @@ def server(input, output, session):
61
  else:
62
  return ui.p(f"Connection Error: {res['content']}")
63
 
64
- app = App(app_ui, server)
 
2
  from openai import OpenAI
3
  from shiny import App, render, ui, reactive
4
 
5
+ # Clean, error-free layout framework
6
  app_ui = ui.page_fluid(
7
  ui.h2("NexusAI Assistant"),
8
  ui.hr(),
 
9
  ui.input_text_area("user_prompt", "Enter your prompt below:", "What is the meaning of life?"),
10
  ui.input_action_button("submit_btn", "Send Query"),
11
  ui.hr(),
 
15
 
16
  def server(input, output, session):
17
 
18
+ # Asynchronous background thread to manage the network waiting time
19
  @reactive.extended_task
20
  async def fetch_ai_response(prompt_text):
21
  try:
 
33
  messages=[{"role": "user", "content": prompt_text}]
34
  )
35
 
36
+ # CORE FIX: Added [0] to extract the text from the first choice object array index
37
+ ai_text = completion.choices[0].message.content
38
+ return {"status": "success", "content": ai_text}
39
+
40
  except Exception as e:
41
  return {"status": "error", "content": str(e)}
42
 
43
+ # Listen for button triggers
44
  @reactive.effect
45
  @reactive.event(input.submit_btn)
46
  def _():
47
  fetch_ai_response.trigger(input.user_prompt())
48
 
49
+ # Safely swap display states without causing the web browser to hang
50
  @render.ui
51
  def ai_display_area():
52
  if fetch_ai_response.status() == "initial":
 
62
  else:
63
  return ui.p(f"Connection Error: {res['content']}")
64
 
65
+ app = App(app_ui, server)