IAMTFRMZA commited on
Commit
d3aedb5
Β·
verified Β·
1 Parent(s): 9ee7546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -37
app.py CHANGED
@@ -135,69 +135,58 @@ def update_image_display(image_url):
135
  with gr.Blocks(theme=gr.themes.Soft()) as app:
136
  gr.Markdown("# πŸ“„ Document AI Assistant")
137
 
138
- gr.HTML("""
139
- <style>
140
- .big-btn {
141
- font-size: 18px !important;
142
- padding: 14px 28px !important;
143
- border-radius: 8px !important;
144
- width: 100% !important;
145
- margin-top: 10px;
146
- white-space: nowrap;
147
- }
148
- .toggle-btn { margin-bottom: 8px; }
149
- </style>
150
- """)
151
 
152
  chat_state = gr.State([])
153
  thread_state = gr.State()
154
  image_state = gr.State()
155
  client_id = gr.State()
156
 
157
- with gr.Row():
158
- toggle_left = gr.Button("πŸ“„ Toggle Document Panel", elem_classes="toggle-btn")
159
- toggle_right = gr.Button("πŸŽ™οΈ Toggle Voice Panel", elem_classes="toggle-btn")
160
-
161
  with gr.Row(equal_height=True) as layout_row:
162
- with gr.Column(scale=1, visible=True) as left_col:
 
163
  image_display = gr.Image(label="πŸ–ΌοΈ Document", type="filepath", show_download_button=False, height=480)
164
 
165
- with gr.Column(scale=2, visible=True) as center_col:
 
166
  chat = gr.Chatbot(label="πŸ’¬ Chat", height=480)
167
  with gr.Row():
168
  user_prompt = gr.Textbox(placeholder="Ask your question...", show_label=False, scale=8)
169
  send_btn = gr.Button("Send", variant="primary", scale=2)
170
  with gr.Row():
171
- clear_chat_btn = gr.Button("πŸ—‘οΈ Clear Chat", elem_classes="big-btn")
172
 
173
- with gr.Column(scale=1, visible=True) as right_col:
 
174
  gr.Markdown("### πŸŽ™οΈ Voice Input")
175
  voice_input = gr.Audio(label="Tap to Record", streaming=True, type="numpy", show_label=True)
176
  voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
177
- with gr.Row(equal_height=True):
178
- with gr.Column(scale=1):
179
- voice_send_btn = gr.Button("🟒 Send Voice to Assistant", elem_classes="big-btn")
180
- with gr.Column(scale=1):
181
- clear_transcript_btn = gr.Button("🧹 Clear Transcript", elem_classes="big-btn")
182
 
183
- def toggle_column_visibility(current_vis):
184
- return gr.update(visible=not current_vis)
 
 
 
 
 
185
 
186
- toggle_left.click(lambda v: toggle_column_visibility(v), inputs=[left_col], outputs=[left_col])
187
- toggle_right.click(lambda v: toggle_column_visibility(v), inputs=[right_col], outputs=[right_col])
188
 
189
  send_btn.click(fn=handle_chat, inputs=[user_prompt, chat_state, thread_state, image_state],
190
  outputs=[user_prompt, chat, thread_state, image_state])
191
-
192
- voice_input.stream(fn=send_audio, inputs=[voice_input, client_id],
193
- outputs=voice_transcript, stream_every=0.5)
194
-
195
- voice_send_btn.click(fn=feed_transcript, inputs=[voice_transcript, chat_state, thread_state, image_state, client_id],
196
  outputs=[user_prompt, chat, thread_state, image_state])
197
-
198
  clear_transcript_btn.click(fn=clear_transcript_only, inputs=[client_id], outputs=voice_transcript)
199
  clear_chat_btn.click(fn=clear_chat_only, outputs=[chat, thread_state, image_state])
200
  image_state.change(fn=update_image_display, inputs=image_state, outputs=image_display)
201
  app.load(fn=create_ws, outputs=[client_id])
202
 
203
- app.launch()
 
135
  with gr.Blocks(theme=gr.themes.Soft()) as app:
136
  gr.Markdown("# πŸ“„ Document AI Assistant")
137
 
138
+ with gr.Row():
139
+ toggle_left = gr.Checkbox(label="πŸ“„ Show Document Panel", value=True)
140
+ toggle_right = gr.Checkbox(label="πŸŽ™οΈ Show Voice Panel", value=True)
 
 
 
 
 
 
 
 
 
 
141
 
142
  chat_state = gr.State([])
143
  thread_state = gr.State()
144
  image_state = gr.State()
145
  client_id = gr.State()
146
 
 
 
 
 
147
  with gr.Row(equal_height=True) as layout_row:
148
+ left_col = gr.Column(visible=True)
149
+ with left_col:
150
  image_display = gr.Image(label="πŸ–ΌοΈ Document", type="filepath", show_download_button=False, height=480)
151
 
152
+ center_col = gr.Column(scale=2)
153
+ with center_col:
154
  chat = gr.Chatbot(label="πŸ’¬ Chat", height=480)
155
  with gr.Row():
156
  user_prompt = gr.Textbox(placeholder="Ask your question...", show_label=False, scale=8)
157
  send_btn = gr.Button("Send", variant="primary", scale=2)
158
  with gr.Row():
159
+ clear_chat_btn = gr.Button("πŸ—‘οΈ Clear Chat")
160
 
161
+ right_col = gr.Column(visible=True)
162
+ with right_col:
163
  gr.Markdown("### πŸŽ™οΈ Voice Input")
164
  voice_input = gr.Audio(label="Tap to Record", streaming=True, type="numpy", show_label=True)
165
  voice_transcript = gr.Textbox(label="Transcript", lines=2, interactive=False)
166
+ with gr.Row():
167
+ voice_send_btn = gr.Button("🟒 Send Voice to Assistant")
168
+ clear_transcript_btn = gr.Button("🧹 Clear Transcript")
 
 
169
 
170
+ def update_layout(show_left, show_right):
171
+ new_layout = {
172
+ left_col: gr.update(visible=show_left),
173
+ right_col: gr.update(visible=show_right),
174
+ center_col: gr.update(scale=2 if (show_left and show_right) else (3 if (show_left or show_right) else 4))
175
+ }
176
+ return new_layout
177
 
178
+ toggle_left.change(fn=update_layout, inputs=[toggle_left, toggle_right], outputs=[left_col, right_col, center_col])
179
+ toggle_right.change(fn=update_layout, inputs=[toggle_left, toggle_right], outputs=[left_col, right_col, center_col])
180
 
181
  send_btn.click(fn=handle_chat, inputs=[user_prompt, chat_state, thread_state, image_state],
182
  outputs=[user_prompt, chat, thread_state, image_state])
183
+ voice_input.stream(fn=send_audio, inputs=[voice_input, client_id], outputs=voice_transcript, stream_every=0.5)
184
+ voice_send_btn.click(fn=feed_transcript,
185
+ inputs=[voice_transcript, chat_state, thread_state, image_state, client_id],
 
 
186
  outputs=[user_prompt, chat, thread_state, image_state])
 
187
  clear_transcript_btn.click(fn=clear_transcript_only, inputs=[client_id], outputs=voice_transcript)
188
  clear_chat_btn.click(fn=clear_chat_only, outputs=[chat, thread_state, image_state])
189
  image_state.change(fn=update_image_display, inputs=image_state, outputs=image_display)
190
  app.load(fn=create_ws, outputs=[client_id])
191
 
192
+ app.launch()