omkar334 commited on
Commit
390f830
·
1 Parent(s): 2dbb737

agent debug capture

Browse files
Files changed (1) hide show
  1. app.py +84 -9
app.py CHANGED
@@ -1,4 +1,7 @@
1
  import base64
 
 
 
2
 
3
  import gradio as gr
4
  from fastapi import FastAPI
@@ -21,6 +24,33 @@ app.add_middleware(
21
  )
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  class ChatQuery(BaseModel):
25
  query: str
26
  grade: str
@@ -39,6 +69,7 @@ class TTSQuery(BaseModel):
39
  src: str
40
 
41
 
 
42
  @app.get("/agent")
43
  async def agent(query: ChatQuery):
44
  collection = f"{query.grade}_{query.subject.lower()}_{query.chapter}"
@@ -61,6 +92,7 @@ async def tts(query: TTSQuery):
61
  return await speaker(query.text, query.src)
62
 
63
 
 
64
  async def gradio_interface(input_text, grade, subject, chapter, history):
65
  response = await agent(ChatQuery(query=input_text, grade=grade, subject=subject, chapter=chapter))
66
 
@@ -74,7 +106,6 @@ async def gradio_interface(input_text, grade, subject, chapter, history):
74
  else:
75
  output = "Unexpected response format"
76
  history.append((input_text, {"type": "text", "content": output}))
77
-
78
  return "", history
79
 
80
 
@@ -86,24 +117,68 @@ def format_history(history):
86
  formatted_history.append((None, assistant["content"]))
87
  elif assistant["type"] == "audio":
88
  formatted_history.append((None, gr.Audio(value=assistant["content"], visible=True)))
 
 
 
89
  return formatted_history
90
 
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  with gr.Blocks() as iface:
93
  gr.Markdown("# Agentic RAG Chatbot")
94
- gr.Markdown("Ask a question and get an answer from the chatbot. The response may be text or audio.")
95
 
 
96
  with gr.Row():
97
- grade = gr.Dropdown(choices=["1", "2", "3", "4", "5", "6", "7", "9", "10", "11", "12"], label="Grade", value="9", interactive=True)
98
- subject = gr.Dropdown(choices=["Math", "Science", "History"], label="Subject", value="Science", interactive=True)
99
- chapter = gr.Dropdown(choices=["1", "2", "3", "4", "5", "6", "7", "9", "10", "11", "12", "13", "14", "15", "16"], label="Chapter", value="11", interactive=True)
100
-
101
- chatbot = gr.Chatbot(label="Chat History")
102
- msg = gr.Textbox(label="Your message", placeholder="Type your message here...")
103
- state = gr.State([])
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  msg.submit(gradio_interface, inputs=[msg, grade, subject, chapter, state], outputs=[msg, state]).then(format_history, inputs=[state], outputs=[chatbot])
106
 
 
 
 
 
 
 
 
 
107
  app = gr.mount_gradio_app(app, iface, path="/")
108
 
109
  if __name__ == "__main__":
 
1
  import base64
2
+ import sys
3
+ from datetime import datetime
4
+ from io import StringIO
5
 
6
  import gradio as gr
7
  from fastapi import FastAPI
 
24
  )
25
 
26
 
27
+ class DebugCapture(StringIO):
28
+ def __init__(self):
29
+ super().__init__()
30
+ self.debug_history = []
31
+ self.new_entry = True
32
+
33
+ def write(self, s):
34
+ if s.strip():
35
+ if self.new_entry:
36
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
37
+ self.debug_history.append(f"[{timestamp}] {s.strip()}")
38
+ self.new_entry = False
39
+ else:
40
+ self.debug_history[-1] += f"\n{s.strip()}"
41
+ else:
42
+ self.new_entry = True
43
+
44
+ if len(self.debug_history) > 10: # Limit log history memory consumption
45
+ self.debug_history.pop(0)
46
+
47
+ return super().write(s)
48
+
49
+
50
+ debug_capture = DebugCapture()
51
+ sys.stdout = debug_capture
52
+
53
+
54
  class ChatQuery(BaseModel):
55
  query: str
56
  grade: str
 
69
  src: str
70
 
71
 
72
+ # API Endpoints
73
  @app.get("/agent")
74
  async def agent(query: ChatQuery):
75
  collection = f"{query.grade}_{query.subject.lower()}_{query.chapter}"
 
92
  return await speaker(query.text, query.src)
93
 
94
 
95
+ # Gradio interface
96
  async def gradio_interface(input_text, grade, subject, chapter, history):
97
  response = await agent(ChatQuery(query=input_text, grade=grade, subject=subject, chapter=chapter))
98
 
 
106
  else:
107
  output = "Unexpected response format"
108
  history.append((input_text, {"type": "text", "content": output}))
 
109
  return "", history
110
 
111
 
 
117
  formatted_history.append((None, assistant["content"]))
118
  elif assistant["type"] == "audio":
119
  formatted_history.append((None, gr.Audio(value=assistant["content"], visible=True)))
120
+
121
+ if len(formatted_history) > 10: # Limit history memory consumption
122
+ formatted_history.pop(0)
123
  return formatted_history
124
 
125
 
126
+ # Debug functions
127
+ def update_debug_output():
128
+ return "\n".join(debug_capture.debug_history)
129
+
130
+
131
+ def clear_debug_history():
132
+ debug_capture.debug_history = []
133
+ return "Debug history cleared."
134
+
135
+
136
+ def toggle_debug_modal(visible):
137
+ return gr.update(visible=visible)
138
+
139
+
140
+ # Gradio UI setup
141
  with gr.Blocks() as iface:
142
  gr.Markdown("# Agentic RAG Chatbot")
 
143
 
144
+ # Main header row
145
  with gr.Row():
146
+ with gr.Column(scale=19):
147
+ gr.Markdown("Ask a question and get an answer from the chatbot. The response may be text or audio.")
148
+ with gr.Column(scale=1, min_width=50):
149
+ debug_button = gr.Button("🖥️", size="sm")
 
 
 
150
 
151
+ # Chat input and interaction
152
+ with gr.Row():
153
+ with gr.Column(scale=20):
154
+ with gr.Row():
155
+ grade = gr.Dropdown(choices=["1", "2", "3", "4", "5", "6", "7", "9", "10", "11", "12"], label="Grade", value="9", interactive=True)
156
+ subject = gr.Dropdown(choices=["Math", "Science", "History"], label="Subject", value="Science", interactive=True)
157
+ chapter = gr.Dropdown(choices=["1", "2", "3", "4", "5", "6", "7", "9", "10", "11", "12", "13", "14", "15", "16"], label="Chapter", value="11", interactive=True)
158
+
159
+ chatbot = gr.Chatbot(label="Chat History")
160
+ msg = gr.Textbox(label="Your message", placeholder="Type your message here...")
161
+ state = gr.State([])
162
+
163
+ # Debugging modal
164
+ with gr.Group(visible=False) as debug_modal:
165
+ debug_output = gr.TextArea(label="Debug Terminal", interactive=False)
166
+ with gr.Row():
167
+ refresh_button = gr.Button("Refresh Debug History")
168
+ clear_button = gr.Button("Clear Debug History")
169
+ close_button = gr.Button("Close")
170
+
171
+ # Submit action
172
  msg.submit(gradio_interface, inputs=[msg, grade, subject, chapter, state], outputs=[msg, state]).then(format_history, inputs=[state], outputs=[chatbot])
173
 
174
+ # Debug button click
175
+ debug_button.click(lambda: toggle_debug_modal(True), outputs=debug_modal).then(update_debug_output, inputs=[], outputs=[debug_output])
176
+
177
+ # Debug modal buttons
178
+ refresh_button.click(update_debug_output, inputs=[], outputs=[debug_output])
179
+ clear_button.click(clear_debug_history, inputs=[], outputs=[debug_output])
180
+ close_button.click(lambda: toggle_debug_modal(False), outputs=debug_modal)
181
+
182
  app = gr.mount_gradio_app(app, iface, path="/")
183
 
184
  if __name__ == "__main__":