Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Hybrid AI Assistant - General Purpose + Healthcare Billing Expert
|
| 4 |
-
A ChatGPT-style assistant
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
@@ -9,13 +9,14 @@ import sys
|
|
| 9 |
import json
|
| 10 |
import logging
|
| 11 |
import re
|
| 12 |
-
from typing import Dict, Optional, Tuple, List, Any
|
| 13 |
from dataclasses import dataclass, field
|
| 14 |
from enum import Enum
|
| 15 |
import requests
|
| 16 |
import gradio as gr
|
| 17 |
from datetime import datetime
|
| 18 |
import random
|
|
|
|
| 19 |
|
| 20 |
# Set up environment
|
| 21 |
os.environ['OPENROUTER_API_KEY'] = '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'
|
|
@@ -269,7 +270,8 @@ class HybridAIAssistant:
|
|
| 269 |
'model': 'openai/gpt-3.5-turbo',
|
| 270 |
'messages': messages,
|
| 271 |
'temperature': 0.7,
|
| 272 |
-
'max_tokens': 500
|
|
|
|
| 273 |
},
|
| 274 |
timeout=30
|
| 275 |
)
|
|
@@ -295,6 +297,81 @@ class HybridAIAssistant:
|
|
| 295 |
logger.error(f"Request failed: {e}")
|
| 296 |
return self.get_fallback_response(message)
|
| 297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
def get_fallback_response(self, message: str) -> str:
|
| 299 |
"""Fallback responses when API fails"""
|
| 300 |
fallbacks = [
|
|
@@ -305,337 +382,213 @@ class HybridAIAssistant:
|
|
| 305 |
]
|
| 306 |
return random.choice(fallbacks)
|
| 307 |
|
| 308 |
-
def
|
| 309 |
-
"""Main method to process any message"""
|
| 310 |
if not message.strip():
|
| 311 |
-
|
|
|
|
| 312 |
|
| 313 |
# Detect intent
|
| 314 |
intent = self.detect_intent(message)
|
| 315 |
|
| 316 |
# Route to appropriate handler
|
| 317 |
if intent['is_billing'] and intent['codes_found']:
|
| 318 |
-
|
| 319 |
else:
|
| 320 |
-
|
| 321 |
|
| 322 |
def reset_context(self):
|
| 323 |
"""Reset conversation context"""
|
| 324 |
self.context = ConversationContext()
|
| 325 |
|
| 326 |
-
# =============
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
def create_interface():
|
| 329 |
-
|
| 330 |
|
| 331 |
-
#
|
| 332 |
custom_css = """
|
| 333 |
-
/* Main container */
|
| 334 |
.gradio-container {
|
| 335 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif !important;
|
| 336 |
-
max-width:
|
| 337 |
margin: auto !important;
|
| 338 |
-
background: #ffffff !important;
|
| 339 |
}
|
| 340 |
|
| 341 |
-
|
| 342 |
-
|
| 343 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
|
|
| 344 |
padding: 2rem;
|
| 345 |
-
border-radius: 15px
|
| 346 |
-
margin-bottom:
|
| 347 |
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 348 |
}
|
| 349 |
|
| 350 |
-
.header-
|
| 351 |
-
color: white;
|
| 352 |
-
font-size: 2rem;
|
| 353 |
-
font-weight: 700;
|
| 354 |
margin: 0;
|
|
|
|
|
|
|
| 355 |
display: flex;
|
| 356 |
align-items: center;
|
| 357 |
justify-content: center;
|
| 358 |
gap: 0.5rem;
|
| 359 |
}
|
| 360 |
|
| 361 |
-
.header-
|
| 362 |
-
|
| 363 |
-
font-size: 1rem;
|
| 364 |
-
|
| 365 |
-
text-align: center;
|
| 366 |
}
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
border-radius: 12px
|
| 373 |
-
|
| 374 |
-
background: #ffffff !important;
|
| 375 |
-
}
|
| 376 |
-
|
| 377 |
-
/* Message styling */
|
| 378 |
-
.message {
|
| 379 |
-
padding: 1rem !important;
|
| 380 |
-
margin: 0.5rem !important;
|
| 381 |
-
border-radius: 12px !important;
|
| 382 |
-
font-size: 15px !important;
|
| 383 |
-
line-height: 1.6 !important;
|
| 384 |
-
}
|
| 385 |
-
|
| 386 |
-
.user-message {
|
| 387 |
-
background: #f3f4f6 !important;
|
| 388 |
-
border: 1px solid #e5e7eb !important;
|
| 389 |
-
margin-left: 20% !important;
|
| 390 |
-
}
|
| 391 |
-
|
| 392 |
-
.bot-message {
|
| 393 |
-
background: #ffffff !important;
|
| 394 |
-
border: 1px solid #e5e7eb !important;
|
| 395 |
-
margin-right: 20% !important;
|
| 396 |
-
}
|
| 397 |
-
|
| 398 |
-
/* Input area */
|
| 399 |
-
#input-box {
|
| 400 |
-
border: 2px solid #e5e7eb !important;
|
| 401 |
-
border-radius: 12px !important;
|
| 402 |
-
padding: 14px 16px !important;
|
| 403 |
-
font-size: 15px !important;
|
| 404 |
-
transition: all 0.3s ease !important;
|
| 405 |
-
background: #ffffff !important;
|
| 406 |
}
|
| 407 |
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
|
|
|
| 412 |
}
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 417 |
color: white !important;
|
| 418 |
border: none !important;
|
| 419 |
-
border-radius:
|
| 420 |
-
padding:
|
| 421 |
font-weight: 600 !important;
|
| 422 |
-
|
| 423 |
-
cursor: pointer !important;
|
| 424 |
-
transition: transform 0.2s ease !important;
|
| 425 |
}
|
| 426 |
|
| 427 |
-
.
|
|
|
|
| 428 |
transform: translateY(-1px) !important;
|
| 429 |
-
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3) !important;
|
| 430 |
-
}
|
| 431 |
-
|
| 432 |
-
.secondary-btn {
|
| 433 |
-
background: #f3f4f6 !important;
|
| 434 |
-
color: #374151 !important;
|
| 435 |
-
border: 1px solid #e5e7eb !important;
|
| 436 |
-
border-radius: 10px !important;
|
| 437 |
-
padding: 10px 20px !important;
|
| 438 |
-
font-weight: 500 !important;
|
| 439 |
-
cursor: pointer !important;
|
| 440 |
-
transition: all 0.2s ease !important;
|
| 441 |
-
}
|
| 442 |
-
|
| 443 |
-
.secondary-btn:hover {
|
| 444 |
-
background: #e5e7eb !important;
|
| 445 |
-
border-color: #d1d5db !important;
|
| 446 |
-
}
|
| 447 |
-
|
| 448 |
-
/* Example chips */
|
| 449 |
-
.example-chip {
|
| 450 |
-
display: inline-block !important;
|
| 451 |
-
background: #ffffff !important;
|
| 452 |
-
border: 1px solid #e5e7eb !important;
|
| 453 |
-
border-radius: 20px !important;
|
| 454 |
-
padding: 8px 16px !important;
|
| 455 |
-
margin: 4px !important;
|
| 456 |
-
font-size: 14px !important;
|
| 457 |
-
color: #4b5563 !important;
|
| 458 |
-
cursor: pointer !important;
|
| 459 |
-
transition: all 0.2s ease !important;
|
| 460 |
-
}
|
| 461 |
-
|
| 462 |
-
.example-chip:hover {
|
| 463 |
-
background: #f9fafb !important;
|
| 464 |
-
border-color: #667eea !important;
|
| 465 |
-
color: #667eea !important;
|
| 466 |
-
transform: translateY(-1px) !important;
|
| 467 |
-
}
|
| 468 |
-
|
| 469 |
-
/* Info cards */
|
| 470 |
-
.info-card {
|
| 471 |
-
background: linear-gradient(135deg, #f6f8fb 0%, #f1f5f9 100%);
|
| 472 |
-
border: 1px solid #e5e7eb;
|
| 473 |
-
border-radius: 12px;
|
| 474 |
-
padding: 1rem;
|
| 475 |
-
margin: 1rem 0;
|
| 476 |
-
}
|
| 477 |
-
|
| 478 |
-
/* Responsive design */
|
| 479 |
-
@media (max-width: 768px) {
|
| 480 |
-
.gradio-container {
|
| 481 |
-
padding: 0 !important;
|
| 482 |
-
}
|
| 483 |
-
|
| 484 |
-
.header-title {
|
| 485 |
-
font-size: 1.5rem;
|
| 486 |
-
}
|
| 487 |
-
|
| 488 |
-
.user-message, .bot-message {
|
| 489 |
-
margin-left: 5% !important;
|
| 490 |
-
margin-right: 5% !important;
|
| 491 |
-
}
|
| 492 |
}
|
| 493 |
"""
|
| 494 |
|
| 495 |
-
with gr.Blocks(css=custom_css,
|
| 496 |
# Header
|
| 497 |
gr.HTML("""
|
| 498 |
-
<div class="header-
|
| 499 |
-
<h1
|
| 500 |
-
|
| 501 |
-
<span>AI Assistant</span>
|
| 502 |
-
<span style="font-size: 0.8em; background: rgba(255,255,255,0.2); padding: 2px 8px; border-radius: 12px;">PLUS</span>
|
| 503 |
-
</h1>
|
| 504 |
-
<p class="header-subtitle">Your intelligent companion for any question + Healthcare Billing Expert</p>
|
| 505 |
</div>
|
| 506 |
""")
|
| 507 |
|
| 508 |
-
# Main
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
|
|
|
| 525 |
placeholder="Ask me anything... (e.g., 'Explain code 99213' or 'Help me write a story')",
|
| 526 |
-
|
| 527 |
-
elem_id="input-box",
|
| 528 |
-
scale=5,
|
| 529 |
lines=1,
|
| 530 |
-
max_lines=
|
| 531 |
)
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
# Quick examples
|
| 535 |
-
gr.HTML("<div style='text-align: center; margin: 1rem 0; color: #6b7280; font-size: 14px;'>Quick Examples</div>")
|
| 536 |
|
|
|
|
| 537 |
with gr.Row():
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
gr.HTML("<div style='color: #667eea; font-weight: 600; font-size: 13px; margin-bottom: 8px;'>π General Questions</div>")
|
| 550 |
-
ex4 = gr.Button("How does AI work?", elem_classes="example-chip", size="sm")
|
| 551 |
-
ex5 = gr.Button("Recipe for pasta", elem_classes="example-chip", size="sm")
|
| 552 |
-
ex6 = gr.Button("Python tutorial", elem_classes="example-chip", size="sm")
|
| 553 |
-
|
| 554 |
-
with ex_col3:
|
| 555 |
-
gr.HTML("<div style='color: #667eea; font-weight: 600; font-size: 13px; margin-bottom: 8px;'>βοΈ Creative Help</div>")
|
| 556 |
-
ex7 = gr.Button("Write a poem", elem_classes="example-chip", size="sm")
|
| 557 |
-
ex8 = gr.Button("Email template", elem_classes="example-chip", size="sm")
|
| 558 |
-
ex9 = gr.Button("Story ideas", elem_classes="example-chip", size="sm")
|
| 559 |
|
| 560 |
-
#
|
| 561 |
-
with gr.
|
| 562 |
-
clear_btn = gr.Button("π New Chat", elem_classes="secondary-btn", size="sm")
|
| 563 |
-
gr.HTML("<div style='flex-grow: 1;'></div>")
|
| 564 |
gr.HTML("""
|
| 565 |
-
<div style=
|
| 566 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
</div>
|
| 568 |
""")
|
| 569 |
|
| 570 |
-
#
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
<div>
|
| 579 |
-
<div style="color: #667eea; font-size: 24px; font-weight: bold;">β</div>
|
| 580 |
-
<div style="color: #6b7280; font-size: 12px;">Topics</div>
|
| 581 |
-
</div>
|
| 582 |
-
<div>
|
| 583 |
-
<div style="color: #667eea; font-size: 24px; font-weight: bold;">24/7</div>
|
| 584 |
-
<div style="color: #6b7280; font-size: 12px;">Available</div>
|
| 585 |
-
</div>
|
| 586 |
-
<div>
|
| 587 |
-
<div style="color: #667eea; font-size: 24px; font-weight: bold;">Fast</div>
|
| 588 |
-
<div style="color: #6b7280; font-size: 12px;">Responses</div>
|
| 589 |
-
</div>
|
| 590 |
-
</div>
|
| 591 |
-
</div>
|
| 592 |
-
""")
|
| 593 |
-
|
| 594 |
-
# Event handlers
|
| 595 |
-
def respond(message, chat_history):
|
| 596 |
-
if not message.strip():
|
| 597 |
-
return "", chat_history
|
| 598 |
-
|
| 599 |
-
# Process message
|
| 600 |
-
response = assistant.process_message(message)
|
| 601 |
-
|
| 602 |
-
# Update chat history
|
| 603 |
-
chat_history.append({"role": "user", "content": message})
|
| 604 |
-
chat_history.append({"role": "assistant", "content": response})
|
| 605 |
-
|
| 606 |
-
return "", chat_history
|
| 607 |
-
|
| 608 |
-
def clear_chat():
|
| 609 |
-
assistant.reset_context()
|
| 610 |
-
welcome_msg = {
|
| 611 |
-
"role": "assistant",
|
| 612 |
-
"content": "π **Chat cleared! Ready for a new conversation.**\n\nI'm here to help with anything you need - from healthcare billing codes to general questions!\n\nWhat would you like to know? π"
|
| 613 |
-
}
|
| 614 |
-
return [welcome_msg]
|
| 615 |
-
|
| 616 |
-
# Connect events
|
| 617 |
-
msg.submit(respond, [msg, chatbot_ui], [msg, chatbot_ui])
|
| 618 |
-
send_btn.click(respond, [msg, chatbot_ui], [msg, chatbot_ui])
|
| 619 |
-
clear_btn.click(clear_chat, outputs=[chatbot_ui])
|
| 620 |
-
|
| 621 |
-
# Example button handlers
|
| 622 |
-
ex1.click(lambda: "What is healthcare billing code A0429?", outputs=msg)
|
| 623 |
-
ex2.click(lambda: "Can you explain CPT code 99213 in detail?", outputs=msg)
|
| 624 |
-
ex3.click(lambda: "Tell me about DRG 470", outputs=msg)
|
| 625 |
-
ex4.click(lambda: "How does artificial intelligence work?", outputs=msg)
|
| 626 |
-
ex5.click(lambda: "Give me a simple pasta recipe", outputs=msg)
|
| 627 |
-
ex6.click(lambda: "Teach me Python basics", outputs=msg)
|
| 628 |
-
ex7.click(lambda: "Write a short poem about nature", outputs=msg)
|
| 629 |
-
ex8.click(lambda: "Help me write a professional email template", outputs=msg)
|
| 630 |
-
ex9.click(lambda: "Give me creative story ideas", outputs=msg)
|
| 631 |
|
| 632 |
-
return
|
|
|
|
|
|
|
| 633 |
|
| 634 |
-
# Launch
|
| 635 |
if __name__ == "__main__":
|
| 636 |
app = create_interface()
|
| 637 |
app.launch(
|
| 638 |
server_name="0.0.0.0",
|
| 639 |
server_port=7860,
|
| 640 |
-
share=False
|
|
|
|
| 641 |
)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Hybrid AI Assistant - General Purpose + Healthcare Billing Expert
|
| 4 |
+
A ChatGPT-style assistant using Gradio ChatInterface for simplicity
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 9 |
import json
|
| 10 |
import logging
|
| 11 |
import re
|
| 12 |
+
from typing import Dict, Optional, Tuple, List, Any, Iterator
|
| 13 |
from dataclasses import dataclass, field
|
| 14 |
from enum import Enum
|
| 15 |
import requests
|
| 16 |
import gradio as gr
|
| 17 |
from datetime import datetime
|
| 18 |
import random
|
| 19 |
+
import time
|
| 20 |
|
| 21 |
# Set up environment
|
| 22 |
os.environ['OPENROUTER_API_KEY'] = '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'
|
|
|
|
| 270 |
'model': 'openai/gpt-3.5-turbo',
|
| 271 |
'messages': messages,
|
| 272 |
'temperature': 0.7,
|
| 273 |
+
'max_tokens': 500,
|
| 274 |
+
'stream': False
|
| 275 |
},
|
| 276 |
timeout=30
|
| 277 |
)
|
|
|
|
| 297 |
logger.error(f"Request failed: {e}")
|
| 298 |
return self.get_fallback_response(message)
|
| 299 |
|
| 300 |
+
def get_streaming_response(self, message: str, billing_context: bool = False) -> Iterator[str]:
|
| 301 |
+
"""Get streaming response from OpenRouter API"""
|
| 302 |
+
|
| 303 |
+
# Prepare system prompt
|
| 304 |
+
system_prompt = """You are a helpful, friendly AI assistant with expertise in healthcare billing codes.
|
| 305 |
+
You can assist with any topic - from casual conversation to complex questions.
|
| 306 |
+
When discussing medical billing codes, you provide accurate, detailed information.
|
| 307 |
+
Be conversational, helpful, and engaging. Use emojis occasionally to be friendly.
|
| 308 |
+
Keep responses concise but informative."""
|
| 309 |
+
|
| 310 |
+
if billing_context:
|
| 311 |
+
system_prompt += "\nThe user is asking about medical billing. Provide helpful information even if you don't have specific code details."
|
| 312 |
+
|
| 313 |
+
# Build conversation history for context
|
| 314 |
+
messages = [{'role': 'system', 'content': system_prompt}]
|
| 315 |
+
|
| 316 |
+
# Add recent conversation history
|
| 317 |
+
for msg in self.context.messages[-10:]:
|
| 318 |
+
messages.append(msg)
|
| 319 |
+
|
| 320 |
+
# Add current message
|
| 321 |
+
messages.append({'role': 'user', 'content': message})
|
| 322 |
+
|
| 323 |
+
try:
|
| 324 |
+
response = requests.post(
|
| 325 |
+
'https://openrouter.ai/api/v1/chat/completions',
|
| 326 |
+
headers=self.headers,
|
| 327 |
+
json={
|
| 328 |
+
'model': 'openai/gpt-3.5-turbo',
|
| 329 |
+
'messages': messages,
|
| 330 |
+
'temperature': 0.7,
|
| 331 |
+
'max_tokens': 500,
|
| 332 |
+
'stream': True
|
| 333 |
+
},
|
| 334 |
+
timeout=30,
|
| 335 |
+
stream=True
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
if response.status_code == 200:
|
| 339 |
+
full_response = ""
|
| 340 |
+
for line in response.iter_lines():
|
| 341 |
+
if line:
|
| 342 |
+
line = line.decode('utf-8')
|
| 343 |
+
if line.startswith('data: '):
|
| 344 |
+
line = line[6:]
|
| 345 |
+
if line.strip() == '[DONE]':
|
| 346 |
+
break
|
| 347 |
+
try:
|
| 348 |
+
chunk = json.loads(line)
|
| 349 |
+
if 'choices' in chunk and len(chunk['choices']) > 0:
|
| 350 |
+
delta = chunk['choices'][0].get('delta', {})
|
| 351 |
+
if 'content' in delta:
|
| 352 |
+
content = delta['content']
|
| 353 |
+
full_response += content
|
| 354 |
+
yield full_response
|
| 355 |
+
except json.JSONDecodeError:
|
| 356 |
+
continue
|
| 357 |
+
|
| 358 |
+
# Update context with full response
|
| 359 |
+
self.context.messages.append({'role': 'user', 'content': message})
|
| 360 |
+
self.context.messages.append({'role': 'assistant', 'content': full_response})
|
| 361 |
+
|
| 362 |
+
# Keep only last 20 messages in context
|
| 363 |
+
if len(self.context.messages) > 20:
|
| 364 |
+
self.context.messages = self.context.messages[-20:]
|
| 365 |
+
|
| 366 |
+
else:
|
| 367 |
+
fallback = self.get_fallback_response(message)
|
| 368 |
+
yield fallback
|
| 369 |
+
|
| 370 |
+
except Exception as e:
|
| 371 |
+
logger.error(f"Streaming request failed: {e}")
|
| 372 |
+
fallback = self.get_fallback_response(message)
|
| 373 |
+
yield fallback
|
| 374 |
+
|
| 375 |
def get_fallback_response(self, message: str) -> str:
|
| 376 |
"""Fallback responses when API fails"""
|
| 377 |
fallbacks = [
|
|
|
|
| 382 |
]
|
| 383 |
return random.choice(fallbacks)
|
| 384 |
|
| 385 |
+
def process_message_streaming(self, message: str) -> Iterator[str]:
|
| 386 |
+
"""Main method to process any message with streaming"""
|
| 387 |
if not message.strip():
|
| 388 |
+
yield "Feel free to ask me anything! I can help with general questions or healthcare billing codes. π"
|
| 389 |
+
return
|
| 390 |
|
| 391 |
# Detect intent
|
| 392 |
intent = self.detect_intent(message)
|
| 393 |
|
| 394 |
# Route to appropriate handler
|
| 395 |
if intent['is_billing'] and intent['codes_found']:
|
| 396 |
+
yield self.handle_billing_query(message, intent['codes_found'])
|
| 397 |
else:
|
| 398 |
+
yield from self.get_streaming_response(message, billing_context=intent['is_billing'])
|
| 399 |
|
| 400 |
def reset_context(self):
|
| 401 |
"""Reset conversation context"""
|
| 402 |
self.context = ConversationContext()
|
| 403 |
|
| 404 |
+
# ============= Global Assistant Instance =============
|
| 405 |
+
assistant = HybridAIAssistant()
|
| 406 |
+
|
| 407 |
+
# ============= Chat Functions =============
|
| 408 |
+
|
| 409 |
+
def respond_stream(message, history):
|
| 410 |
+
"""Streaming response function for ChatInterface"""
|
| 411 |
+
if not message.strip():
|
| 412 |
+
yield "Feel free to ask me anything! I can help with general questions or healthcare billing codes. π"
|
| 413 |
+
return
|
| 414 |
+
|
| 415 |
+
# Process message with streaming
|
| 416 |
+
for partial_response in assistant.process_message_streaming(message):
|
| 417 |
+
yield partial_response
|
| 418 |
+
|
| 419 |
+
def reset_chat():
|
| 420 |
+
"""Reset the conversation context"""
|
| 421 |
+
assistant.reset_context()
|
| 422 |
+
return None
|
| 423 |
+
|
| 424 |
+
# ============= Examples and Additional Buttons =============
|
| 425 |
+
|
| 426 |
+
examples = [
|
| 427 |
+
"What is healthcare billing code A0429?",
|
| 428 |
+
"Can you explain CPT code 99213 in detail?",
|
| 429 |
+
"Tell me about DRG 470",
|
| 430 |
+
"How does artificial intelligence work?",
|
| 431 |
+
"Give me a simple pasta recipe",
|
| 432 |
+
"Teach me Python basics",
|
| 433 |
+
"Write a short poem about nature",
|
| 434 |
+
"Help me write a professional email template",
|
| 435 |
+
"Give me creative story ideas"
|
| 436 |
+
]
|
| 437 |
+
|
| 438 |
+
# ============= Create Interface =============
|
| 439 |
|
| 440 |
def create_interface():
|
| 441 |
+
"""Create the Gradio ChatInterface"""
|
| 442 |
|
| 443 |
+
# Custom CSS for better styling
|
| 444 |
custom_css = """
|
|
|
|
| 445 |
.gradio-container {
|
| 446 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif !important;
|
| 447 |
+
max-width: 1000px !important;
|
| 448 |
margin: auto !important;
|
|
|
|
| 449 |
}
|
| 450 |
|
| 451 |
+
.header-text {
|
| 452 |
+
text-align: center;
|
| 453 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 454 |
+
color: white;
|
| 455 |
padding: 2rem;
|
| 456 |
+
border-radius: 15px;
|
| 457 |
+
margin-bottom: 1rem;
|
| 458 |
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 459 |
}
|
| 460 |
|
| 461 |
+
.header-text h1 {
|
|
|
|
|
|
|
|
|
|
| 462 |
margin: 0;
|
| 463 |
+
font-size: 2.5rem;
|
| 464 |
+
font-weight: 700;
|
| 465 |
display: flex;
|
| 466 |
align-items: center;
|
| 467 |
justify-content: center;
|
| 468 |
gap: 0.5rem;
|
| 469 |
}
|
| 470 |
|
| 471 |
+
.header-text p {
|
| 472 |
+
margin: 0.5rem 0 0 0;
|
| 473 |
+
font-size: 1.1rem;
|
| 474 |
+
opacity: 0.9;
|
|
|
|
| 475 |
}
|
| 476 |
|
| 477 |
+
.examples-container {
|
| 478 |
+
margin: 1rem 0;
|
| 479 |
+
padding: 1rem;
|
| 480 |
+
background: #f8fafc;
|
| 481 |
+
border-radius: 12px;
|
| 482 |
+
border: 1px solid #e2e8f0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
}
|
| 484 |
|
| 485 |
+
.examples-title {
|
| 486 |
+
color: #4a5568;
|
| 487 |
+
font-weight: 600;
|
| 488 |
+
margin-bottom: 0.5rem;
|
| 489 |
+
text-align: center;
|
| 490 |
}
|
| 491 |
|
| 492 |
+
.reset-btn {
|
| 493 |
+
background: #f56565 !important;
|
|
|
|
| 494 |
color: white !important;
|
| 495 |
border: none !important;
|
| 496 |
+
border-radius: 8px !important;
|
| 497 |
+
padding: 0.5rem 1rem !important;
|
| 498 |
font-weight: 600 !important;
|
| 499 |
+
margin: 0.5rem 0 !important;
|
|
|
|
|
|
|
| 500 |
}
|
| 501 |
|
| 502 |
+
.reset-btn:hover {
|
| 503 |
+
background: #e53e3e !important;
|
| 504 |
transform: translateY(-1px) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
}
|
| 506 |
"""
|
| 507 |
|
| 508 |
+
with gr.Blocks(css=custom_css, title="AI Assistant + Healthcare Billing Expert") as demo:
|
| 509 |
# Header
|
| 510 |
gr.HTML("""
|
| 511 |
+
<div class="header-text">
|
| 512 |
+
<h1>π€ AI Assistant <span style="font-size: 0.6em; background: rgba(255,255,255,0.2); padding: 4px 12px; border-radius: 16px;">PLUS</span></h1>
|
| 513 |
+
<p>Your intelligent companion for any question + Healthcare Billing Expert</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
</div>
|
| 515 |
""")
|
| 516 |
|
| 517 |
+
# Main Chat Interface
|
| 518 |
+
chat_interface = gr.ChatInterface(
|
| 519 |
+
fn=respond_stream,
|
| 520 |
+
title="", # We have custom header
|
| 521 |
+
description="", # We have custom header
|
| 522 |
+
examples=examples,
|
| 523 |
+
cache_examples=False,
|
| 524 |
+
retry_btn="π Retry",
|
| 525 |
+
undo_btn="β©οΈ Undo",
|
| 526 |
+
clear_btn="ποΈ Clear",
|
| 527 |
+
submit_btn="Send π€",
|
| 528 |
+
chatbot=gr.Chatbot(
|
| 529 |
+
height=600,
|
| 530 |
+
show_copy_button=True,
|
| 531 |
+
show_share_button=False,
|
| 532 |
+
avatar_images=["π€", "π€"]
|
| 533 |
+
),
|
| 534 |
+
textbox=gr.Textbox(
|
| 535 |
placeholder="Ask me anything... (e.g., 'Explain code 99213' or 'Help me write a story')",
|
| 536 |
+
scale=7,
|
|
|
|
|
|
|
| 537 |
lines=1,
|
| 538 |
+
max_lines=8
|
| 539 |
)
|
| 540 |
+
)
|
|
|
|
|
|
|
|
|
|
| 541 |
|
| 542 |
+
# Additional controls
|
| 543 |
with gr.Row():
|
| 544 |
+
with gr.Column(scale=1):
|
| 545 |
+
reset_context_btn = gr.Button("π Reset Context", elem_classes="reset-btn", size="sm")
|
| 546 |
+
with gr.Column(scale=3):
|
| 547 |
+
gr.HTML("") # Spacer
|
| 548 |
+
with gr.Column(scale=1):
|
| 549 |
+
gr.HTML("""
|
| 550 |
+
<div style='text-align: right; color: #718096; font-size: 12px; margin-top: 0.5rem;'>
|
| 551 |
+
Powered by GPT-3.5 Turbo<br>
|
| 552 |
+
Healthcare Billing Database
|
| 553 |
+
</div>
|
| 554 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
|
| 556 |
+
# Info section
|
| 557 |
+
with gr.Accordion("βΉοΈ About This Assistant", open=False):
|
|
|
|
|
|
|
| 558 |
gr.HTML("""
|
| 559 |
+
<div style="padding: 1rem; background: #f7fafc; border-radius: 8px; margin: 0.5rem 0;">
|
| 560 |
+
<h4 style="color: #2d3748; margin-top: 0;">π₯ Healthcare Billing Expert</h4>
|
| 561 |
+
<p style="color: #4a5568; margin-bottom: 1rem;">I'm specialized in healthcare billing codes including:</p>
|
| 562 |
+
<ul style="color: #4a5568; margin: 0.5rem 0;">
|
| 563 |
+
<li><strong>CPT Codes</strong> - Current Procedural Terminology</li>
|
| 564 |
+
<li><strong>HCPCS</strong> - Healthcare Common Procedure Coding System</li>
|
| 565 |
+
<li><strong>ICD-10</strong> - International Classification of Diseases</li>
|
| 566 |
+
<li><strong>DRG</strong> - Diagnosis-Related Groups</li>
|
| 567 |
+
</ul>
|
| 568 |
+
|
| 569 |
+
<h4 style="color: #2d3748;">π¬ General AI Assistant</h4>
|
| 570 |
+
<p style="color: #4a5568; margin: 0;">I can also help with general questions, writing, coding, learning, and creative tasks!</p>
|
| 571 |
</div>
|
| 572 |
""")
|
| 573 |
|
| 574 |
+
# Connect reset button
|
| 575 |
+
reset_context_btn.click(
|
| 576 |
+
fn=reset_chat,
|
| 577 |
+
inputs=None,
|
| 578 |
+
outputs=None
|
| 579 |
+
).then(
|
| 580 |
+
lambda: gr.Info("Context reset! Starting fresh conversation.")
|
| 581 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
|
| 583 |
+
return demo
|
| 584 |
+
|
| 585 |
+
# ============= Launch =============
|
| 586 |
|
|
|
|
| 587 |
if __name__ == "__main__":
|
| 588 |
app = create_interface()
|
| 589 |
app.launch(
|
| 590 |
server_name="0.0.0.0",
|
| 591 |
server_port=7860,
|
| 592 |
+
share=False,
|
| 593 |
+
show_error=True
|
| 594 |
)
|