yogies commited on
Commit
d072ec5
·
verified ·
1 Parent(s): f7fa49f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -32
app.py CHANGED
@@ -6,6 +6,9 @@ import gradio as gr
6
  import importlib.util
7
 
8
  from huggingface_hub import hf_hub_download
 
 
 
9
 
10
  # ----------------------------------------------------------------------
11
  # Helper to read secrets from the HF Space environment
@@ -57,24 +60,17 @@ print("--- [UI App] Bootstrap complete. Gradio UI is starting. ---")
57
  # 3. Core Gradio Chat Logic
58
  # ----------------------------------------------------------------------
59
  def add_message_and_respond(message: str, history: list):
60
- """
61
- Adds the user's message to the chat history and gets the bot's streaming response.
62
- """
63
- # Add the user's message to the UI immediately
64
  history.append([message, None])
65
  yield history
66
 
67
- # Convert chat history to the format the engine expects
68
  history_for_engine = []
69
- for user_msg, bot_msg in history[:-1]: # Exclude the current turn
70
  history_for_engine.append({"role": "user", "content": user_msg})
71
  if bot_msg:
72
  history_for_engine.append({"role": "assistant", "content": bot_msg})
73
 
74
- # Get the response from the agent
75
  final_response = engine.get_response(message, history_for_engine)
76
 
77
- # Stream the complete response back to the UI
78
  history[-1][1] = ""
79
  for char in final_response:
80
  history[-1][1] += char
@@ -85,60 +81,49 @@ def add_message_and_respond(message: str, history: list):
85
  # 4. UI Layout and Launch (Beautified with gr.Blocks)
86
  # ----------------------------------------------------------------------
87
 
88
- # Custom CSS for a cleaner look
89
  css = """
90
  #chatbot { min-height: 600px; }
91
  .footer { text-align: center; color: #777; font-size: 0.8em; padding: 10px 0; }
92
  """
93
 
 
94
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
95
- # Header
96
  gr.Markdown("<h1>🤖 PRECISE RAG Agent</h1>")
97
  gr.Markdown("Tanya Jawab Cerdas Mengenai Dokumentasi Sistem PRECISE")
98
 
99
- # Main Chatbot Interface
100
- chatbot = gr.Chatbot(
101
  elem_id="chatbot",
102
  label="Conversation",
103
- render_math=True # <-- THE FIX FOR FORMULAS!
104
  )
105
 
106
- # Input area
107
  with gr.Row():
108
- msg = gr.Textbox(
109
  label="Your Question",
110
  placeholder="Ketik pertanyaan Anda di sini dan tekan Enter...",
111
- scale=4, # Make the textbox wider
112
  )
113
  submit_btn = gr.Button("Send", variant="primary", scale=1)
114
 
115
- # Example questions
116
  gr.Examples(
117
  examples=[
 
118
  "Apa tujuan pengadaan PRECISE?",
119
- "Apa keunggulan utama PRECISE dibandingkan sistem perhitungan target promosi yang lama?",
120
- "Apakah PRECISE sudah terbukti memberikan manfaat finansial? Bisakah diberikan contohnya?",
121
- "Selain akurasi, aspek teknis apa yang membuat PRECISE unik?"
122
  ],
123
  inputs=msg,
124
  )
125
 
126
- # Footer
127
  gr.Markdown("<hr><p class='footer'>Powered by OpenRouter and LangChain. All rights reserved.</p>")
128
 
129
  # --- Event Handlers ---
130
 
131
- # Define the action for submitting a message (either by pressing Enter or clicking the button)
132
- def submit_action(message, history):
133
- yield from add_message_and_respond(message, history)
134
-
135
- # When the user submits, call the action and update the chatbot
136
- # Then, clear the input textbox
137
- msg.submit(submit_action, [msg, chatbot], chatbot)
138
- msg.submit(lambda: "", None, msg) # Clears textbox
139
 
140
- submit_btn.click(submit_action, [msg, chatbot], chatbot)
141
- submit_btn.click(lambda: "", None, msg) # Clears textbox
142
 
143
  # ----------------------------------------------------------------------
144
  # 5. Launch the App
@@ -146,9 +131,10 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
146
  if __name__ == "__main__":
147
  allowed_user = _secret("CHAT_USER")
148
  allowed_pass = _secret("CHAT_PASS")
 
149
  demo.launch(
150
  auth=(allowed_user, allowed_pass),
151
  server_name="0.0.0.0",
152
  ssr_mode=False,
153
- server_port=7860
154
  )
 
6
  import importlib.util
7
 
8
  from huggingface_hub import hf_hub_download
9
+ from gradio.components.chatbot import Chatbot as grChatbot # Import for type hints
10
+ from gradio.components.textbox import Textbox as grTextbox
11
+ from gradio.events import EventListener
12
 
13
  # ----------------------------------------------------------------------
14
  # Helper to read secrets from the HF Space environment
 
60
  # 3. Core Gradio Chat Logic
61
  # ----------------------------------------------------------------------
62
  def add_message_and_respond(message: str, history: list):
 
 
 
 
63
  history.append([message, None])
64
  yield history
65
 
 
66
  history_for_engine = []
67
+ for user_msg, bot_msg in history[:-1]:
68
  history_for_engine.append({"role": "user", "content": user_msg})
69
  if bot_msg:
70
  history_for_engine.append({"role": "assistant", "content": bot_msg})
71
 
 
72
  final_response = engine.get_response(message, history_for_engine)
73
 
 
74
  history[-1][1] = ""
75
  for char in final_response:
76
  history[-1][1] += char
 
81
  # 4. UI Layout and Launch (Beautified with gr.Blocks)
82
  # ----------------------------------------------------------------------
83
 
 
84
  css = """
85
  #chatbot { min-height: 600px; }
86
  .footer { text-align: center; color: #777; font-size: 0.8em; padding: 10px 0; }
87
  """
88
 
89
+ # The theme must be passed to gr.Blocks now
90
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
 
91
  gr.Markdown("<h1>🤖 PRECISE RAG Agent</h1>")
92
  gr.Markdown("Tanya Jawab Cerdas Mengenai Dokumentasi Sistem PRECISE")
93
 
94
+ chatbot = grChatbot(
 
95
  elem_id="chatbot",
96
  label="Conversation",
97
+ likeable=True, # This is a valid and nice feature
98
  )
99
 
 
100
  with gr.Row():
101
+ msg = grTextbox(
102
  label="Your Question",
103
  placeholder="Ketik pertanyaan Anda di sini dan tekan Enter...",
104
+ scale=4,
105
  )
106
  submit_btn = gr.Button("Send", variant="primary", scale=1)
107
 
 
108
  gr.Examples(
109
  examples=[
110
+ "Apa rumus untuk menghitung PVR?",
111
  "Apa tujuan pengadaan PRECISE?",
112
+ "Jelaskan segmentasi *slow-moving*",
 
 
113
  ],
114
  inputs=msg,
115
  )
116
 
 
117
  gr.Markdown("<hr><p class='footer'>Powered by OpenRouter and LangChain. All rights reserved.</p>")
118
 
119
  # --- Event Handlers ---
120
 
121
+ # Create a submit action that can be triggered by button click or textbox submit
122
+ submit_event = msg.submit(add_message_and_respond, [msg, chatbot], chatbot)
123
+ submit_event.then(lambda: "", None, msg) # Clears textbox after submit
 
 
 
 
 
124
 
125
+ btn_event = submit_btn.click(add_message_and_respond, [msg, chatbot], chatbot)
126
+ btn_event.then(lambda: "", None, msg) # Clears textbox after click
127
 
128
  # ----------------------------------------------------------------------
129
  # 5. Launch the App
 
131
  if __name__ == "__main__":
132
  allowed_user = _secret("CHAT_USER")
133
  allowed_pass = _secret("CHAT_PASS")
134
+ # Tell Gradio to load the MathJax library for LaTeX rendering
135
  demo.launch(
136
  auth=(allowed_user, allowed_pass),
137
  server_name="0.0.0.0",
138
  ssr_mode=False,
139
+ server_port=7860,
140
  )