Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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.
|
139 |
-
|
140 |
-
|
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 |
-
|
|
|
163 |
image_display = gr.Image(label="πΌοΈ Document", type="filepath", show_download_button=False, height=480)
|
164 |
|
165 |
-
|
|
|
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"
|
172 |
|
173 |
-
|
|
|
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(
|
178 |
-
|
179 |
-
|
180 |
-
with gr.Column(scale=1):
|
181 |
-
clear_transcript_btn = gr.Button("π§Ή Clear Transcript", elem_classes="big-btn")
|
182 |
|
183 |
-
def
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
-
toggle_left.
|
187 |
-
toggle_right.
|
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 |
-
|
193 |
-
|
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()
|