yogies commited on
Commit
59a0e25
·
verified ·
1 Parent(s): 7eb1131

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -55,13 +55,27 @@ engine = agent_logic_module.RAG_Engine(
55
  print("--- [UI App] Bootstrap complete. Gradio UI is starting. ---")
56
 
57
  # ----------------------------------------------------------------------
58
- # 3. Core Gradio Chat Logic
 
 
 
 
 
59
  # ----------------------------------------------------------------------
60
  def respond(message: str, history: list[dict[str, str]]):
61
  """
62
  Called by Gradio for each user message.
63
- Streams the response back to the UI.
 
64
  """
 
 
 
 
 
 
 
 
65
  final_response = engine.get_response(message, history)
66
 
67
  # Simple "typing" effect – yield partial strings
@@ -72,9 +86,8 @@ def respond(message: str, history: list[dict[str, str]]):
72
  yield response
73
 
74
  # ----------------------------------------------------------------------
75
- # 4. UI Layout – Tips + Chat + Footer
76
  # ----------------------------------------------------------------------
77
- # 4.1 Tips (you can edit this markdown as you wish)
78
  tips_md = r"""
79
  ## 📋 Tips menggunakan **PRECISE RAG Agent**
80
 
@@ -91,7 +104,6 @@ tips_md = r"""
91
 
92
  """
93
 
94
- # 4.2 Footer – the old description / notes
95
  footer_md = r"""
96
  ---
97
  **Frameworks**: LangChain + FAISS
@@ -100,7 +112,6 @@ footer_md = r"""
100
 
101
  """
102
 
103
- # 4.3 Chat component (no description here)
104
  chatbot = gr.ChatInterface(
105
  respond,
106
  type="messages",
@@ -114,22 +125,7 @@ chatbot = gr.ChatInterface(
114
  theme=gr.themes.Soft(),
115
  )
116
 
117
- # 4.4 Assemble everything inside a Blocks container
118
  with gr.Blocks() as demo:
119
- # Optional: add a small vertical space at the top
120
- chatbot.render() # <-- Main chat UI
121
- gr.Markdown(tips_md) # <-- Tips section (renders LaTeX)
122
- gr.Markdown(footer_md) # <-- Footer (old description)
123
-
124
- # ----------------------------------------------------------------------
125
- # 5. Launch
126
- # ----------------------------------------------------------------------
127
- if __name__ == "__main__":
128
- allowed_user = _secret("CHAT_USER")
129
- allowed_pass = _secret("CHAT_PASS")
130
- demo.launch(
131
- auth=(allowed_user, allowed_pass),
132
- server_name="0.0.0.0",
133
- ssr_mode=False,
134
- server_port=7860,
135
- )
 
55
  print("--- [UI App] Bootstrap complete. Gradio UI is starting. ---")
56
 
57
  # ----------------------------------------------------------------------
58
+ # 3. Import the usage‑counter helper (the file‑lock implementation)
59
+ # ----------------------------------------------------------------------
60
+ from usage_counter import increment_and_check
61
+
62
+ # ----------------------------------------------------------------------
63
+ # 4. Core Gradio Chat Logic – now wrapped with the quota check
64
  # ----------------------------------------------------------------------
65
  def respond(message: str, history: list[dict[str, str]]):
66
  """
67
  Called by Gradio for each user message.
68
+ If the quota is exhausted we immediately return an error message.
69
+ Otherwise we stream the response from the RAG engine.
70
  """
71
+ # ---- quota check (atomic) ----
72
+ if not increment_and_check():
73
+ # The limit has been reached – we do **not** call the engine.
74
+ # Yield a single message so the UI shows it instantly.
75
+ yield "❌ **Quota exceeded** – the maximum number of allowed requests has been reached."
76
+ return
77
+
78
+ # ---- normal RAG processing ----
79
  final_response = engine.get_response(message, history)
80
 
81
  # Simple "typing" effect – yield partial strings
 
86
  yield response
87
 
88
  # ----------------------------------------------------------------------
89
+ # 5. UI Layout – Tips + Chat + Footer (unchanged)
90
  # ----------------------------------------------------------------------
 
91
  tips_md = r"""
92
  ## 📋 Tips menggunakan **PRECISE RAG Agent**
93
 
 
104
 
105
  """
106
 
 
107
  footer_md = r"""
108
  ---
109
  **Frameworks**: LangChain + FAISS
 
112
 
113
  """
114
 
 
115
  chatbot = gr.ChatInterface(
116
  respond,
117
  type="messages",
 
125
  theme=gr.themes.Soft(),
126
  )
127
 
 
128
  with gr.Blocks() as demo:
129
+ chatbot.render()
130
+ gr.Markdown(tips_md)
131
+ gr.Markdown(footer_md)