-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradio_ui.py
More file actions
663 lines (554 loc) · 29.1 KB
/
gradio_ui.py
File metadata and controls
663 lines (554 loc) · 29.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import gradio as gr
import httpx
import json
from typing import List, Tuple
# API Configuration
API_BASE_URL = "http://127.0.0.1:8000"
# Custom CSS for clean, modern design
custom_css = """
.gradio-container {
max-width: 1200px !important;
margin: auto !important;
}
.tab-nav {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%) !important;
}
.upload-area {
border: 2px dashed #e0e7ff !important;
border-radius: 12px !important;
background: #f8fafc !important;
transition: all 0.3s ease !important;
}
.upload-area:hover {
border-color: #4f46e5 !important;
background: #f1f5f9 !important;
}
.status-success {
background: #ecfdf5 !important;
border: 1px solid #86efac !important;
color: #166534 !important;
padding: 12px !important;
border-radius: 8px !important;
}
.status-error {
background: #fef2f2 !important;
border: 1px solid #fca5a5 !important;
color: #991b1b !important;
padding: 12px !important;
border-radius: 8px !important;
}
.chat-container {
max-height: 500px !important;
overflow-y: auto !important;
}
.button-primary {
background: linear-gradient(90deg, #4f46e5 0%, #7c3aed 100%) !important;
border: none !important;
color: white !important;
border-radius: 8px !important;
padding: 10px 20px !important;
transition: all 0.3s ease !important;
}
.button-primary:hover {
transform: translateY(-2px) !important;
box-shadow: 0 4px 12px rgba(79, 70, 229, 0.4) !important;
}
.extracted-text {
font-family: 'Courier New', monospace !important;
background: #f8fafc !important;
border: 1px solid #e2e8f0 !important;
border-radius: 8px !important;
max-height: 600px !important;
overflow-y: auto !important;
}
"""
class GradioUI:
def __init__(self):
self.question_session_state = {}
self.summary_session_state = {}
self.qa_session_state = {}
def upload_file(self, file) -> Tuple[str, str, str]:
"""Upload file and return asset ID, status message, and extracted text"""
if file is None:
return "", "❌ Please select a file to upload", ""
try:
# Set timeout for large file uploads (5 minutes)
timeout = httpx.Timeout(300.0, connect=60.0)
with httpx.Client(timeout=timeout) as client:
with open(file.name, "rb") as f:
files = {"file": (file.name.split("/")[-1], f, "application/octet-stream")}
response = client.post(f"{API_BASE_URL}/upload_file/", files=files)
if response.status_code == 200:
result = response.json()
asset_id = result["id"]
# Fetch the extracted text
extracted_text = self.get_extracted_text(asset_id)
status_msg = f"✅ File uploaded successfully!\n📁 Asset ID: {asset_id}\n📄 File: {file.name.split('/')[-1]}"
return asset_id, status_msg, extracted_text
else:
return "", f"❌ Upload failed: {response.text}", ""
except httpx.TimeoutException:
return "", "❌ Upload timed out. Please try with a smaller file or check your connection.", ""
except httpx.ConnectError:
return "", "❌ Cannot connect to the API server. Please ensure the server is running.", ""
except Exception as e:
return "", f"❌ Error uploading file: {str(e)}", ""
def get_extracted_text(self, asset_id: str) -> str:
"""Retrieve extracted text for a given asset ID"""
if not asset_id.strip():
return ""
try:
timeout = httpx.Timeout(30.0, connect=10.0)
with httpx.Client(timeout=timeout) as client:
response = client.get(f"{API_BASE_URL}/get_extracted_text/{asset_id.strip()}")
if response.status_code == 200:
result = response.json()
return result.get("extracted_text", "")
else:
return f"❌ Failed to retrieve extracted text: {response.text}"
except httpx.TimeoutException:
return "❌ Request timed out while retrieving extracted text."
except httpx.ConnectError:
return "❌ Cannot connect to the API server."
except Exception as e:
return f"❌ Error retrieving extracted text: {str(e)}"
def start_question_session(self, asset_id: str, question_type: str) -> Tuple[str, str, str, str, str]:
"""Start a question generation session"""
if not asset_id.strip():
return "", "", "", "", "❌ Please enter an Asset ID"
try:
payload = {"asset_id": asset_id.strip(), "question_type": question_type}
timeout = httpx.Timeout(60.0, connect=10.0)
with httpx.Client(timeout=timeout) as client:
response = client.post(f"{API_BASE_URL}/api/v1/graph/qg/start_session", json=payload)
if response.status_code == 200:
result = response.json()
# print(f"Response from question generation: {result}")
state = result
self.question_session_state = {
"thread_id": state["thread_id"],
"asset_id": asset_id.strip(),
"question_type": question_type
}
question = state["data_for_feedback"].get("generated_question", "")
options = state["data_for_feedback"].get("options", [])
answer = state["data_for_feedback"].get("answer", "")
explanation = state["data_for_feedback"].get("explanation", "")
options_text = "\n".join([f"{opt}" for i, opt in enumerate(options)]) if options else ""
status = f"✅ Question generated successfully!\n🎯 Type: {question_type}\n🔗 Session ID: {state['thread_id']}..."
return question, options_text, answer, explanation, status
else:
return "", "", "", "", f"❌ Failed to generate question: {response.text}"
except httpx.TimeoutException:
return "", "", "", "", "❌ Request timed out. Please try again."
except httpx.ConnectError:
return "", "", "", "", "❌ Cannot connect to the API server. Please ensure the server is running."
except Exception as e:
return "", "", "", "", f"❌ Error generating question: {str(e)}"
def update_question(self, feedback: str) -> Tuple[str, str, str, str, str]:
"""Update question based on feedback"""
if not self.question_session_state:
return "", "", "", "", "❌ No active session. Please start a new session first."
if not feedback.strip():
return "", "", "", "", "❌ Please provide feedback for the question"
try:
payload = {
"thread_id": self.question_session_state["thread_id"],
"feedback": feedback.strip()
}
timeout = httpx.Timeout(60.0, connect=10.0)
with httpx.Client(timeout=timeout) as client:
response = client.post(f"{API_BASE_URL}/api/v1/graph/qg/provide_feedback", json=payload)
if response.status_code == 200:
result = response.json()
# print(f"Response from question update: {result}")
state = result
question = state["data_for_feedback"].get("generated_question", "")
options = state["data_for_feedback"].get("options", [])
answer = state["data_for_feedback"].get("answer", "")
explanation = state["data_for_feedback"].get("explanation", "")
options_text = "\n".join([f"{opt}" for i, opt in enumerate(options)]) if options else ""
status = "✅ Question updated based on your feedback!"
return question, options_text, answer, explanation, status
else:
return "", "", "", "", f"❌ Failed to update question: {response.text}"
except httpx.TimeoutException:
return "", "", "", "", "❌ Request timed out. Please try again."
except httpx.ConnectError:
return "", "", "", "", "❌ Cannot connect to the API server. Please ensure the server is running."
except Exception as e:
return "", "", "", "", f"❌ Error updating question: {str(e)}"
def start_summary_session(self, asset_id: str):
"""Start a summary generation session with streaming"""
if not asset_id.strip():
return "", "", "❌ Please enter an Asset ID"
try:
payload = {"asset_id": asset_id.strip()}
# Use streaming endpoint
with httpx.Client(timeout=httpx.Timeout(300.0)) as client:
with client.stream("POST", f"{API_BASE_URL}/api/v1/graph/summarizer/start_session_streaming", json=payload) as response:
if response.status_code != 200:
return "", "", f"❌ Failed to start summary session: {response.text}"
main_points = ""
summary = ""
thread_id = ""
for chunk in response.iter_text():
if chunk.strip():
if chunk.startswith("data: "):
chunk = chunk[6:] # Remove 'data: ' prefix
try:
event = json.loads(chunk)
if event.get("thread_id"):
thread_id = event["thread_id"]
self.summary_session_state = {
"thread_id": thread_id,
"asset_id": asset_id.strip()
}
if event.get("event") == "token" and event.get("status_update") == "main_point_summarizer":
main_points += event["token"]
yield main_points, summary, "🔄 Generating main points..."
elif event.get("event") == "token" and event.get("status_update") == "summarizer_writer":
summary += event["token"]
yield main_points, summary, "🔄 Generating detailed summary..."
except json.JSONDecodeError as e:
print(f"JSON decode error: {e}, chunk: {chunk}")
continue
status = f"✅ Summary generated successfully!\n🔗 Session ID: {thread_id[:8]}..."
yield main_points, summary, status
except Exception as e:
yield "", "", f"❌ Error generating summary: {str(e)}"
def update_summary(self, feedback: str):
"""Update summary based on feedback"""
if not self.summary_session_state:
yield "", "❌ No active session. Please start a new session first."
return
if not feedback.strip():
yield "", "❌ Please provide feedback for the summary"
return
try:
payload = {
"thread_id": self.summary_session_state["thread_id"],
"feedback": feedback.strip()
}
with httpx.Client(timeout=httpx.Timeout(300.0)) as client:
with client.stream("POST", f"{API_BASE_URL}/api/v1/graph/summarizer/provide_feedback_streaming", json=payload) as response:
if response.status_code != 200:
yield "", f"❌ Failed to update summary: {response.text}"
return
summary = ""
for chunk in response.iter_text():
if chunk.strip():
if chunk.startswith("data: "):
chunk = chunk[6:]
try:
event = json.loads(chunk)
if event.get("event") == "token" and event.get("status_update") == "summarizer_rewriter":
summary += event["token"]
yield summary, "🔄 Updating summary based on feedback..."
except json.JSONDecodeError:
continue
status = "✅ Summary updated based on your feedback!"
yield summary, status
except Exception as e:
yield "", f"❌ Error updating summary: {str(e)}"
def start_qa_session_streaming(self, asset_id: str, question: str):
"""Start a Q&A session with streaming response"""
if not asset_id.strip():
yield [], "❌ Please enter an Asset ID"
return
if not question.strip():
yield [], "❌ Please enter a question"
return
try:
payload = {"asset_id": asset_id.strip(), "initial_question": question.strip()}
with httpx.Client(timeout=httpx.Timeout(300.0)) as client:
with client.stream("POST", f"{API_BASE_URL}/api/v1/graph/qa/start_session_stream", json=payload) as response:
if response.status_code != 200:
yield [], f"❌ Failed to start Q&A session: {response.text}"
return
thread_id = ""
ai_response = ""
for chunk in response.iter_text():
if chunk.strip():
if chunk.startswith("data: "):
chunk = chunk[6:] # Remove 'data: ' prefix
try:
event = json.loads(chunk)
if event.get("type") == "metadata":
thread_id = event.get("thread_id", "")
self.qa_session_state = {
"thread_id": thread_id,
"asset_id": asset_id.strip()
}
yield [], f"🔄 Starting Q&A session... ID: {thread_id[:8]}..."
elif event.get("type") == "token":
ai_response += event.get("content", "")
chat_history = [(question.strip(), ai_response)]
yield chat_history, f"🔄 Generating response..."
elif event.get("type") == "complete":
chat_history = [(question.strip(), ai_response)]
yield chat_history, f"✅ Q&A session started! ID: {thread_id[:8]}..."
elif event.get("type") == "error":
yield [], f"❌ Error: {event.get('content', 'Unknown error')}"
return
except json.JSONDecodeError:
continue
except Exception as e:
yield [], f"❌ Error starting Q&A session: {str(e)}"
def continue_qa_chat_streaming(self, message: str, history: List[Tuple[str, str]]):
"""Continue the Q&A conversation with streaming"""
if not self.qa_session_state:
yield history, "❌ No active session. Please start a new session first."
return
if not message.strip():
yield history, "❌ Please enter a message"
return
try:
payload = {
"thread_id": self.qa_session_state["thread_id"],
"next_question": message.strip()
}
with httpx.Client(timeout=httpx.Timeout(300.0)) as client:
with client.stream("POST", f"{API_BASE_URL}/api/v1/graph/qa/continue_conversation_stream", json=payload) as response:
if response.status_code != 200:
yield history, f"❌ Failed to get response: {response.text}"
return
ai_response = ""
for chunk in response.iter_text():
if chunk.strip():
if chunk.startswith("data: "):
chunk = chunk[6:] # Remove 'data: ' prefix
try:
event = json.loads(chunk)
if event.get("type") == "token":
ai_response += event.get("content", "")
new_history = history + [(message.strip(), ai_response)]
yield new_history, "🔄 Generating response..."
elif event.get("type") == "complete":
new_history = history + [(message.strip(), ai_response)]
yield new_history, "✅ Response complete"
elif event.get("type") == "error":
yield history, f"❌ Error: {event.get('content', 'Unknown error')}"
return
except json.JSONDecodeError:
continue
except Exception as e:
yield history, f"❌ Error in conversation: {str(e)}"
def create_gradio_interface():
ui = GradioUI()
with gr.Blocks(css=custom_css, title="CourseTA - AI Teaching Assistant", theme=gr.themes.Soft()) as app:
gr.Markdown(
"""
# 🎓 CourseTA - AI Teaching Assistant
### Upload content, generate questions, create summaries, and ask questions about your educational materials
""",
elem_classes="header"
)
with gr.Tabs(): # Tab 1: File Upload
with gr.Tab("📁 Upload Content", elem_id="upload-tab"):
gr.Markdown("### Upload PDF documents or audio/video files for processing")
with gr.Row():
with gr.Column(scale=1):
file_input = gr.File(
label="Select File",
file_types=[".pdf", ".mp3", ".mp4", ".wav", ".avi", ".mov", ".mkv", ".flv"],
elem_classes="upload-area"
)
upload_btn = gr.Button("🚀 Upload File", variant="primary", elem_classes="button-primary")
asset_id_display = gr.Textbox(
label="Generated Asset ID",
placeholder="Asset ID will appear here after upload...",
interactive=True,
lines=1
)
with gr.Row():
load_text_btn = gr.Button("📄 Load Extracted Text", variant="secondary", scale=2)
upload_status = gr.Markdown("📋 Ready to upload files")
with gr.Column(scale=2):
extracted_text_display = gr.Textbox(
label="Extracted Text",
placeholder="Extracted text will appear here after upload...",
lines=15,
max_lines=20,
interactive=False,
show_copy_button=True
)
# Event handlers for upload tab
upload_btn.click(
fn=ui.upload_file,
inputs=[file_input],
outputs=[asset_id_display, upload_status, extracted_text_display]
)
load_text_btn.click(
fn=lambda asset_id: ("", ui.get_extracted_text(asset_id)) if asset_id.strip() else ("❌ Please enter an Asset ID", ""),
inputs=[asset_id_display],
outputs=[upload_status, extracted_text_display]
)
# Tab 2: Question Generation
with gr.Tab("❓ Question Generation", elem_id="question-tab"):
gr.Markdown("### Generate questions from your uploaded content")
with gr.Row():
with gr.Column(scale=1):
q_asset_id = gr.Textbox(
label="Asset ID",
placeholder="Enter or paste your Asset ID here...",
lines=1
)
question_type = gr.Radio(
choices=["T/F", "MCQ"],
label="Question Type",
value="MCQ"
)
generate_q_btn = gr.Button("🎯 Generate Question", variant="primary", elem_classes="button-primary")
with gr.Column(scale=2):
question_display = gr.Textbox(
label="Generated Question",
lines=3,
interactive=False
)
options_display = gr.Textbox(
label="Answer Options",
lines=4,
interactive=False
)
answer_display = gr.Textbox(
label="Correct Answer",
lines=1,
interactive=False
)
explanation_display = gr.Textbox(
label="Explanation",
lines=3,
interactive=False
)
with gr.Row():
feedback_input = gr.Textbox(
label="Feedback",
placeholder="Provide feedback to improve the question...",
lines=2
)
update_q_btn = gr.Button("🔄 Update Question", variant="secondary")
q_status = gr.Markdown("📋 Ready to generate questions")
# Event handlers for question generation
generate_q_btn.click(
fn=ui.start_question_session,
inputs=[q_asset_id, question_type],
outputs=[question_display, options_display, answer_display, explanation_display, q_status]
)
update_q_btn.click(
fn=ui.update_question,
inputs=[feedback_input],
outputs=[question_display, options_display, answer_display, explanation_display, q_status]
)
# Tab 3: Summarization
with gr.Tab("📝 Content Summarization", elem_id="summary-tab"):
gr.Markdown("### Create summaries and main points from your content")
with gr.Row():
with gr.Column(scale=1):
s_asset_id = gr.Textbox(
label="Asset ID",
placeholder="Enter or paste your Asset ID here...",
lines=1
)
generate_s_btn = gr.Button("📊 Generate Summary", variant="primary", elem_classes="button-primary")
summary_feedback = gr.Textbox(
label="Feedback",
placeholder="Provide feedback to improve the summary...",
lines=3
)
update_s_btn = gr.Button("🔄 Update Summary", variant="secondary")
with gr.Column(scale=2):
main_points_display = gr.Textbox(
label="Main Points",
lines=6,
interactive=False
)
summary_display = gr.Textbox(
label="Detailed Summary",
lines=8,
interactive=False
)
s_status = gr.Markdown("📋 Ready to generate summaries")
# Event handlers for summarization
generate_s_btn.click(
fn=ui.start_summary_session,
inputs=[s_asset_id],
outputs=[main_points_display, summary_display, s_status]
)
update_s_btn.click(
fn=ui.update_summary,
inputs=[summary_feedback],
outputs=[summary_display, s_status]
)
# Tab 4: Question Answering Chat
with gr.Tab("💬 Question Answering", elem_id="qa-tab"):
gr.Markdown("### Ask questions about your uploaded content")
with gr.Row():
with gr.Column(scale=1):
qa_asset_id = gr.Textbox(
label="Asset ID",
placeholder="Enter or paste your Asset ID here...",
lines=1
)
initial_question = gr.Textbox(
label="Initial Question",
placeholder="Ask your first question about the content...",
lines=2
)
start_qa_btn = gr.Button("🚀 Start Q&A Session", variant="primary", elem_classes="button-primary")
qa_status = gr.Markdown("📋 Ready to start Q&A session")
with gr.Column(scale=2):
chatbot = gr.Chatbot(
label="Conversation",
height=400,
elem_classes="chat-container"
)
with gr.Row():
msg_input = gr.Textbox(
label="Your Message",
placeholder="Continue the conversation...",
lines=1,
scale=4
)
send_btn = gr.Button("📤 Send", variant="secondary", scale=1) # Event handlers for Q&A
start_qa_btn.click(
fn=ui.start_qa_session_streaming,
inputs=[qa_asset_id, initial_question],
outputs=[chatbot, qa_status]
)
send_btn.click(
fn=ui.continue_qa_chat_streaming,
inputs=[msg_input, chatbot],
outputs=[chatbot, qa_status]
).then(
lambda: "",
outputs=[msg_input]
)
# Allow Enter key to send messages
msg_input.submit(
fn=ui.continue_qa_chat_streaming,
inputs=[msg_input, chatbot],
outputs=[chatbot, qa_status]
).then(
lambda: "",
outputs=[msg_input]
)
gr.Markdown(
"""
---
### 💡 How to Use:
1. **Upload**: Upload your PDF or video files to get an Asset ID
2. **Questions**: Use the Asset ID to generate and refine questions
3. **Summary**: Create main points and detailed summaries
4. **Chat**: Have interactive conversations about your content
"""
)
return app
if __name__ == "__main__":
app = create_gradio_interface()
app.launch(
server_name="127.0.0.1",
server_port=7860,
share=False,
show_error=True
)