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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import logging
|
| 3 |
+
from ai_service import AIService
|
| 4 |
+
|
| 5 |
+
logger = logging.getLogger(__name__)
|
| 6 |
+
logging.basicConfig(level=logging.INFO)
|
| 7 |
+
|
| 8 |
+
# Initialize the AI Service
|
| 9 |
+
ai_service = AIService()
|
| 10 |
+
|
| 11 |
+
# Store conversation history
|
| 12 |
+
conversation_history = []
|
| 13 |
+
|
| 14 |
+
def chat_interface(user_message):
|
| 15 |
+
|
| 16 |
+
global conversation_history
|
| 17 |
+
|
| 18 |
+
if not user_message.strip():
|
| 19 |
+
return "", "No intent detected", "Start a conversation..."
|
| 20 |
+
|
| 21 |
+
# Generate response
|
| 22 |
+
response, intent_obj, reasoning_details = ai_service.generate_response(
|
| 23 |
+
user_message,
|
| 24 |
+
conversation_history
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Add to conversation history
|
| 28 |
+
conversation_history.append({
|
| 29 |
+
"role": "user",
|
| 30 |
+
"content": user_message
|
| 31 |
+
})
|
| 32 |
+
conversation_history.append({
|
| 33 |
+
"role": "assistant",
|
| 34 |
+
"content": response,
|
| 35 |
+
"reasoning_details": reasoning_details
|
| 36 |
+
})
|
| 37 |
+
|
| 38 |
+
# Format intent display
|
| 39 |
+
intent1 = intent_obj.get("intent1", "unknown")
|
| 40 |
+
conf1 = intent_obj.get("confidence1", 0)
|
| 41 |
+
intent2 = intent_obj.get("intent2", "unknown")
|
| 42 |
+
conf2 = intent_obj.get("confidence2", 0)
|
| 43 |
+
|
| 44 |
+
intent_display = f"**Primary Intent:** {intent1} ({int(conf1*100)}%)\n\n**Secondary Intent:** {intent2} ({int(conf2*100)}%)"
|
| 45 |
+
|
| 46 |
+
# Format reasoning display
|
| 47 |
+
reasoning_display = "No reasoning available"
|
| 48 |
+
if reasoning_details:
|
| 49 |
+
reasoning_display = f"**Reasoning:** {reasoning_details}"
|
| 50 |
+
|
| 51 |
+
return response, intent_display, reasoning_display
|
| 52 |
+
|
| 53 |
+
def clear_history():
|
| 54 |
+
"""Clear conversation history"""
|
| 55 |
+
global conversation_history
|
| 56 |
+
conversation_history = []
|
| 57 |
+
return "", "", "Conversation cleared", "No intent detected", "Start a conversation..."
|
| 58 |
+
|
| 59 |
+
def format_conversation():
|
| 60 |
+
"""Format and display full conversation history"""
|
| 61 |
+
if not conversation_history:
|
| 62 |
+
return "No conversation history yet."
|
| 63 |
+
|
| 64 |
+
formatted = ""
|
| 65 |
+
for msg in conversation_history:
|
| 66 |
+
role = msg.get("role", "unknown").upper()
|
| 67 |
+
content = msg.get("content", "")
|
| 68 |
+
formatted += f"**{role}:** {content}\n\n"
|
| 69 |
+
|
| 70 |
+
return formatted
|
| 71 |
+
|
| 72 |
+
# Create the Gradio interface
|
| 73 |
+
with gr.Blocks(title="SAK Informatics", theme=gr.themes.Soft()) as demo:
|
| 74 |
+
gr.Markdown(
|
| 75 |
+
"""
|
| 76 |
+
# SAK Informatics Customer Responce Support Chatbot
|
| 77 |
+
|
| 78 |
+
Welcome! This chatbot helps with customer support inquiries including orders, payments, refunds, shipping, and more.
|
| 79 |
+
|
| 80 |
+
**Developed by Namani Vamshi Krishna**
|
| 81 |
+
"""
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column(scale=2):
|
| 86 |
+
gr.Markdown("### Chat Interface")
|
| 87 |
+
chatbox = gr.Textbox(
|
| 88 |
+
label="Your Message",
|
| 89 |
+
placeholder="Type your question or concern here...",
|
| 90 |
+
lines=3
|
| 91 |
+
)
|
| 92 |
+
submit_btn = gr.Button("Send", variant="primary", size="lg")
|
| 93 |
+
clear_btn = gr.Button("Clear History", variant="secondary")
|
| 94 |
+
|
| 95 |
+
with gr.Column(scale=1):
|
| 96 |
+
gr.Markdown("### Intent Classification")
|
| 97 |
+
intent_output = gr.Markdown("No intent detected")
|
| 98 |
+
|
| 99 |
+
gr.Markdown("### Reasoning Details")
|
| 100 |
+
reasoning_output = gr.Markdown("Start a conversation...")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
response_output = gr.Textbox(
|
| 104 |
+
label="Chatbot Response",
|
| 105 |
+
lines=5,
|
| 106 |
+
interactive=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
with gr.Row():
|
| 110 |
+
gr.Markdown("### Conversation History")
|
| 111 |
+
history_output = gr.Textbox(
|
| 112 |
+
label="Full Conversation",
|
| 113 |
+
lines=8,
|
| 114 |
+
interactive=False
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Connect button click to chat function
|
| 118 |
+
submit_btn.click(
|
| 119 |
+
fn=chat_interface,
|
| 120 |
+
inputs=[chatbox],
|
| 121 |
+
outputs=[response_output, intent_output, reasoning_output]
|
| 122 |
+
).then(
|
| 123 |
+
fn=format_conversation,
|
| 124 |
+
inputs=[],
|
| 125 |
+
outputs=[history_output]
|
| 126 |
+
).then(
|
| 127 |
+
fn=lambda: "",
|
| 128 |
+
inputs=[],
|
| 129 |
+
outputs=[chatbox]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Handle Enter key
|
| 133 |
+
chatbox.submit(
|
| 134 |
+
fn=chat_interface,
|
| 135 |
+
inputs=[chatbox],
|
| 136 |
+
outputs=[response_output, intent_output, reasoning_output]
|
| 137 |
+
).then(
|
| 138 |
+
fn=format_conversation,
|
| 139 |
+
inputs=[],
|
| 140 |
+
outputs=[history_output]
|
| 141 |
+
).then(
|
| 142 |
+
fn=lambda: "",
|
| 143 |
+
inputs=[],
|
| 144 |
+
outputs=[chatbox]
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Clear button functionality
|
| 148 |
+
clear_btn.click(
|
| 149 |
+
fn=clear_history,
|
| 150 |
+
inputs=[],
|
| 151 |
+
outputs=[chatbox, response_output, history_output, intent_output, reasoning_output]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
gr.Markdown(
|
| 155 |
+
"""
|
| 156 |
+
---
|
| 157 |
+
**Support Categories:**
|
| 158 |
+
- Order Management (place, cancel, change)
|
| 159 |
+
- Payment & Invoicing
|
| 160 |
+
- Refunds & Returns
|
| 161 |
+
- Shipping & Delivery
|
| 162 |
+
- Account Management
|
| 163 |
+
- Customer Service
|
| 164 |
+
"""
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.26.0
|
| 2 |
+
requests==2.31.0
|