MaduRox commited on
Commit
9bf8aab
·
1 Parent(s): bfa849f

Fix: use uvicorn entry point & gr.Code for json output

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -679,7 +679,7 @@ def health():
679
 
680
  def ui_chat_wrapper(message, model_id, pack_id, max_tokens, temperature):
681
  if not message.strip():
682
- return "Please enter a message.", {}
683
 
684
  msgs = [ChatMessage(role="user", content=message)]
685
  req = ChatRequest(
@@ -697,9 +697,10 @@ def ui_chat_wrapper(message, model_id, pack_id, max_tokens, temperature):
697
  try:
698
  res = chat_completions(req, request=FakeRequest(), _rate={"ip": "gradio_ui"})
699
  answer = res["choices"][0]["message"]["content"]
700
- return answer, res
701
  except Exception as e:
702
- return f"Error: {str(e)}", {"error": str(e), "trace": traceback.format_exc()}
 
703
 
704
  def ui_compile_text(text, bandwidth):
705
  if not text.strip():
@@ -781,7 +782,6 @@ with gr.Blocks(title="Kalpanā AI — Testing Suite & Studio", theme=gr.themes.S
781
  )
782
 
783
  with gr.Tabs():
784
- # TAB 1: CHAT & TEST
785
  with gr.TabItem("💬 Chat & Inference Test"):
786
  gr.Markdown("### Test RIF-Enhanced Chat Inference with any LLM")
787
  with gr.Row():
@@ -802,7 +802,7 @@ with gr.Blocks(title="Kalpanā AI — Testing Suite & Studio", theme=gr.themes.S
802
 
803
  with gr.Column(scale=3):
804
  bot_output = gr.Textbox(lines=8, label="LLM Assistant Response", interactive=False)
805
- json_output = gr.JSON(label="Full API Response & RIF Context Metrics")
806
 
807
  submit_btn.click(
808
  fn=ui_chat_wrapper,
@@ -810,7 +810,6 @@ with gr.Blocks(title="Kalpanā AI — Testing Suite & Studio", theme=gr.themes.S
810
  outputs=[bot_output, json_output]
811
  )
812
 
813
- # TAB 2: KNOWLEDGE PACK STUDIO
814
  with gr.TabItem("📚 Knowledge Pack Studio"):
815
  gr.Markdown("### Compile Documents into Portable .kp Knowledge Packs")
816
  with gr.Row():
@@ -847,7 +846,6 @@ with gr.Blocks(title="Kalpanā AI — Testing Suite & Studio", theme=gr.themes.S
847
  packs_md = gr.Markdown("Click Refresh to load active packs.")
848
  refresh_btn.click(fn=ui_refresh_packs, inputs=[], outputs=[packs_md])
849
 
850
- # TAB 3: REGISTER CUSTOM LLM PROVIDER
851
  with gr.TabItem("🔌 Register Custom LLM Provider"):
852
  gr.Markdown("### Bring Your Own API Key (Groq, OpenAI, Together, Cerebras, OpenRouter)")
853
  with gr.Row():
@@ -870,7 +868,6 @@ with gr.Blocks(title="Kalpanā AI — Testing Suite & Studio", theme=gr.themes.S
870
  outputs=[reg_status]
871
  )
872
 
873
- # TAB 4: CONTEXT EXTENSION BENCHMARK MATRIX
874
  with gr.TabItem("📊 Verified Context Extension Matrix"):
875
  gr.Markdown(
876
  """
@@ -894,11 +891,14 @@ with gr.Blocks(title="Kalpanā AI — Testing Suite & Studio", theme=gr.themes.S
894
  """
895
  )
896
 
897
- app = gr.mount_gradio_app(app, demo, path="/ui")
898
-
899
  @app.get("/", include_in_schema=False)
900
  def root_redirect():
901
  return RedirectResponse(url="/docs")
902
 
 
 
 
903
  if __name__ == "__main__":
904
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
679
 
680
  def ui_chat_wrapper(message, model_id, pack_id, max_tokens, temperature):
681
  if not message.strip():
682
+ return "Please enter a message.", "{}"
683
 
684
  msgs = [ChatMessage(role="user", content=message)]
685
  req = ChatRequest(
 
697
  try:
698
  res = chat_completions(req, request=FakeRequest(), _rate={"ip": "gradio_ui"})
699
  answer = res["choices"][0]["message"]["content"]
700
+ return answer, json.dumps(res, indent=2)
701
  except Exception as e:
702
+ err_res = {"error": str(e), "trace": traceback.format_exc()}
703
+ return f"Error: {str(e)}", json.dumps(err_res, indent=2)
704
 
705
  def ui_compile_text(text, bandwidth):
706
  if not text.strip():
 
782
  )
783
 
784
  with gr.Tabs():
 
785
  with gr.TabItem("💬 Chat & Inference Test"):
786
  gr.Markdown("### Test RIF-Enhanced Chat Inference with any LLM")
787
  with gr.Row():
 
802
 
803
  with gr.Column(scale=3):
804
  bot_output = gr.Textbox(lines=8, label="LLM Assistant Response", interactive=False)
805
+ json_output = gr.Code(language="json", label="Full API Response & RIF Context Metrics")
806
 
807
  submit_btn.click(
808
  fn=ui_chat_wrapper,
 
810
  outputs=[bot_output, json_output]
811
  )
812
 
 
813
  with gr.TabItem("📚 Knowledge Pack Studio"):
814
  gr.Markdown("### Compile Documents into Portable .kp Knowledge Packs")
815
  with gr.Row():
 
846
  packs_md = gr.Markdown("Click Refresh to load active packs.")
847
  refresh_btn.click(fn=ui_refresh_packs, inputs=[], outputs=[packs_md])
848
 
 
849
  with gr.TabItem("🔌 Register Custom LLM Provider"):
850
  gr.Markdown("### Bring Your Own API Key (Groq, OpenAI, Together, Cerebras, OpenRouter)")
851
  with gr.Row():
 
868
  outputs=[reg_status]
869
  )
870
 
 
871
  with gr.TabItem("📊 Verified Context Extension Matrix"):
872
  gr.Markdown(
873
  """
 
891
  """
892
  )
893
 
 
 
894
  @app.get("/", include_in_schema=False)
895
  def root_redirect():
896
  return RedirectResponse(url="/docs")
897
 
898
+ # Mount Gradio UI on FastAPI app
899
+ app = gr.mount_gradio_app(app, demo, path="/ui")
900
+
901
  if __name__ == "__main__":
902
+ import uvicorn
903
+ uvicorn.run(app, host="0.0.0.0", port=7860)
904
+