Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -159,7 +159,6 @@ def handle_pdf_question(question, session_id):
|
|
| 159 |
{"role": "user", "content": f"PDF chunk:\n{chunk}\n\nQuestion: {question}"}
|
| 160 |
]
|
| 161 |
assistant_text = groq_chat_completion(messages)
|
| 162 |
-
# Add snippet highlighting for wow factor
|
| 163 |
assistant_text = f"**Snippet from PDF:**\n{chunk[:200]}...\n\n**Answer:**\n{assistant_text}"
|
| 164 |
if session_id not in SESSION_HISTORY:
|
| 165 |
SESSION_HISTORY[session_id] = []
|
|
@@ -233,6 +232,31 @@ def download_pdf_summary(session_id):
|
|
| 233 |
summary_text = "No summary available."
|
| 234 |
return generate_pdf_file(summary_text, "summary")
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
# ------------------ Voice & Chat Handlers ------------------
|
| 237 |
def _append_chat_display(session_id, user_text, assistant_text):
|
| 238 |
if session_id not in CHAT_DISPLAY:
|
|
@@ -286,8 +310,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 286 |
<style>
|
| 287 |
/* Change height + width of the audio recorder box */
|
| 288 |
#mic_box audio {
|
| 289 |
-
height: 50px !important;
|
| 290 |
-
width: 200px !important;
|
| 291 |
}
|
| 292 |
</style>
|
| 293 |
""")
|
|
@@ -296,8 +320,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 296 |
session_voice = gr.State(str(uuid.uuid4()))
|
| 297 |
session_pdf = gr.State(str(uuid.uuid4()))
|
| 298 |
session_image = gr.State(str(uuid.uuid4()))
|
| 299 |
-
# FIX: define pdf_summary_file BEFORE it is used
|
| 300 |
-
#pdf_summary_file = gr.File(label="Download Summary", visible=False)
|
| 301 |
|
| 302 |
with gr.Tab("🎤 Voice Chat"):
|
| 303 |
chat_voice = gr.Chatbot(type="messages", height=300)
|
|
@@ -316,13 +338,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 316 |
btn_reset_logs = gr.Button("♻ Reset LOGs")
|
| 317 |
btn_download_logs = gr.Button("📥 Download Summary")
|
| 318 |
Voice_summary_file = gr.File(label="📥Download Summary File", interactive=False,scale =1)
|
| 319 |
-
#btn_general = gr.Button("⚡Ask General 🎯")
|
| 320 |
-
#btn_pdf = gr.Button("⚡Ask PDF 📄")
|
| 321 |
-
#btn_image = gr.Button("⚡Ask Image 🖼")
|
| 322 |
-
#with gr.Row():
|
| 323 |
-
#text_input = gr.Textbox(label="Or type a question (General)",visible=False)
|
| 324 |
-
#btn_send_text = gr.Button("Send (Text General)",visible=False)
|
| 325 |
-
#btn_reset_logs = gr.Button("♻ Reset LOGs")
|
| 326 |
answer_voice = gr.Textbox(label="Assistant Answer (text)", lines=2, visible=False)
|
| 327 |
|
| 328 |
btn_general.click(fn=handle_voice_general,
|
|
@@ -330,9 +345,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 330 |
outputs=[answer_voice, audio_output, chat_voice])
|
| 331 |
btn_pdf.click(fn=handle_voice_pdf, inputs=[mic, session_pdf, tts_lang], outputs=[answer_voice, audio_output, chat_voice])
|
| 332 |
btn_image.click(fn=handle_voice_image, inputs=[mic, session_image, tts_lang], outputs=[answer_voice, audio_output, chat_voice])
|
| 333 |
-
# btn_send_text.click(fn=handle_text_general, inputs=[text_input, session_voice, enhancer_toggle, tone_dropdown], outputs=[answer_voice, chat_voice])
|
| 334 |
btn_reset_logs.click(lambda: (str(uuid.uuid4()), [], None, None, ""), outputs=[session_voice, chat_voice, mic, audio_output, answer_voice])
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
with gr.Tab("📄 PDF Summarizer"):
|
| 338 |
pdf_output = gr.Textbox(label="Answer (Text Only)", lines=5)
|
|
@@ -369,8 +389,3 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 369 |
|
| 370 |
image_upload_btn.upload(handle_image_upload, inputs=[image_upload_btn, session_image], outputs=[image_upload_msg, image_output])
|
| 371 |
image_send_btn.click(handle_text_image, inputs=[image_question, session_image], outputs=[image_output])
|
| 372 |
-
image_reset_btn.click(lambda: (str(uuid.uuid4()), ""), outputs=[session_image, image_output])
|
| 373 |
-
image_download_btn.click(download_pdf_summary, inputs=[session_image], outputs=[image_summary_file])
|
| 374 |
-
|
| 375 |
-
if __name__ == "__main__":
|
| 376 |
-
demo.launch()
|
|
|
|
| 159 |
{"role": "user", "content": f"PDF chunk:\n{chunk}\n\nQuestion: {question}"}
|
| 160 |
]
|
| 161 |
assistant_text = groq_chat_completion(messages)
|
|
|
|
| 162 |
assistant_text = f"**Snippet from PDF:**\n{chunk[:200]}...\n\n**Answer:**\n{assistant_text}"
|
| 163 |
if session_id not in SESSION_HISTORY:
|
| 164 |
SESSION_HISTORY[session_id] = []
|
|
|
|
| 232 |
summary_text = "No summary available."
|
| 233 |
return generate_pdf_file(summary_text, "summary")
|
| 234 |
|
| 235 |
+
# ------------------ NEW: Full merged summary ------------------
|
| 236 |
+
def download_full_summary(session_voice, session_pdf, session_image):
|
| 237 |
+
combined = []
|
| 238 |
+
|
| 239 |
+
# Voice
|
| 240 |
+
for m in SESSION_HISTORY.get(session_voice, []):
|
| 241 |
+
if m["role"] == "assistant":
|
| 242 |
+
combined.append(f"### VOICE ({datetime.now().strftime('%Y-%m-%d %H:%M')}):\n{m['content']}")
|
| 243 |
+
|
| 244 |
+
# PDF
|
| 245 |
+
for m in SESSION_HISTORY.get(session_pdf, []):
|
| 246 |
+
if m["role"] == "assistant":
|
| 247 |
+
combined.append(f"### PDF ({datetime.now().strftime('%Y-%m-%d %H:%M')}):\n{m['content']}")
|
| 248 |
+
|
| 249 |
+
# Image
|
| 250 |
+
for m in SESSION_HISTORY.get(session_image, []):
|
| 251 |
+
if m["role"] == "assistant":
|
| 252 |
+
combined.append(f"### IMAGE ({datetime.now().strftime('%Y-%m-%d %H:%M')}):\n{m['content']}")
|
| 253 |
+
|
| 254 |
+
summary_text = "\n\n".join(combined)
|
| 255 |
+
if not summary_text.strip():
|
| 256 |
+
summary_text = "No summary available."
|
| 257 |
+
|
| 258 |
+
return generate_pdf_file(summary_text, "full_summary")
|
| 259 |
+
|
| 260 |
# ------------------ Voice & Chat Handlers ------------------
|
| 261 |
def _append_chat_display(session_id, user_text, assistant_text):
|
| 262 |
if session_id not in CHAT_DISPLAY:
|
|
|
|
| 310 |
<style>
|
| 311 |
/* Change height + width of the audio recorder box */
|
| 312 |
#mic_box audio {
|
| 313 |
+
height: 50px !important;
|
| 314 |
+
width: 200px !important;
|
| 315 |
}
|
| 316 |
</style>
|
| 317 |
""")
|
|
|
|
| 320 |
session_voice = gr.State(str(uuid.uuid4()))
|
| 321 |
session_pdf = gr.State(str(uuid.uuid4()))
|
| 322 |
session_image = gr.State(str(uuid.uuid4()))
|
|
|
|
|
|
|
| 323 |
|
| 324 |
with gr.Tab("🎤 Voice Chat"):
|
| 325 |
chat_voice = gr.Chatbot(type="messages", height=300)
|
|
|
|
| 338 |
btn_reset_logs = gr.Button("♻ Reset LOGs")
|
| 339 |
btn_download_logs = gr.Button("📥 Download Summary")
|
| 340 |
Voice_summary_file = gr.File(label="📥Download Summary File", interactive=False,scale =1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
answer_voice = gr.Textbox(label="Assistant Answer (text)", lines=2, visible=False)
|
| 342 |
|
| 343 |
btn_general.click(fn=handle_voice_general,
|
|
|
|
| 345 |
outputs=[answer_voice, audio_output, chat_voice])
|
| 346 |
btn_pdf.click(fn=handle_voice_pdf, inputs=[mic, session_pdf, tts_lang], outputs=[answer_voice, audio_output, chat_voice])
|
| 347 |
btn_image.click(fn=handle_voice_image, inputs=[mic, session_image, tts_lang], outputs=[answer_voice, audio_output, chat_voice])
|
|
|
|
| 348 |
btn_reset_logs.click(lambda: (str(uuid.uuid4()), [], None, None, ""), outputs=[session_voice, chat_voice, mic, audio_output, answer_voice])
|
| 349 |
+
|
| 350 |
+
# <-- UPDATED: Full merged summary download
|
| 351 |
+
btn_download_logs.click(
|
| 352 |
+
download_full_summary,
|
| 353 |
+
inputs=[session_voice, session_pdf, session_image],
|
| 354 |
+
outputs=[Voice_summary_file]
|
| 355 |
+
)
|
| 356 |
|
| 357 |
with gr.Tab("📄 PDF Summarizer"):
|
| 358 |
pdf_output = gr.Textbox(label="Answer (Text Only)", lines=5)
|
|
|
|
| 389 |
|
| 390 |
image_upload_btn.upload(handle_image_upload, inputs=[image_upload_btn, session_image], outputs=[image_upload_msg, image_output])
|
| 391 |
image_send_btn.click(handle_text_image, inputs=[image_question, session_image], outputs=[image_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|