Spaces:
Running
Running
| import gradio as gr | |
| import logging | |
| from ai_service import AIService | |
| logger = logging.getLogger(__name__) | |
| logging.basicConfig(level=logging.INFO) | |
| # Initialize the AI Service | |
| ai_service = AIService() | |
| # Store conversation history | |
| conversation_history = [] | |
| def chat_interface(user_message): | |
| global conversation_history | |
| if not user_message.strip(): | |
| return "", "No intent detected", "Start a conversation..." | |
| # Generate response | |
| response, intent_obj, reasoning_details = ai_service.generate_response( | |
| user_message, | |
| conversation_history | |
| ) | |
| # Add to conversation history | |
| conversation_history.append({ | |
| "role": "user", | |
| "content": user_message | |
| }) | |
| conversation_history.append({ | |
| "role": "assistant", | |
| "content": response, | |
| "reasoning_details": reasoning_details | |
| }) | |
| # Format intent display | |
| intent1 = intent_obj.get("intent1", "unknown") | |
| conf1 = intent_obj.get("confidence1", 0) | |
| intent2 = intent_obj.get("intent2", "unknown") | |
| conf2 = intent_obj.get("confidence2", 0) | |
| intent_display = f"**Primary Intent:** {intent1} ({int(conf1*100)}%)\n\n**Secondary Intent:** {intent2} ({int(conf2*100)}%)" | |
| # Format reasoning display | |
| reasoning_display = "No reasoning available" | |
| if reasoning_details: | |
| reasoning_display = f"**Reasoning:** {reasoning_details}" | |
| return response, intent_display, reasoning_display | |
| def clear_history(): | |
| """Clear conversation history""" | |
| global conversation_history | |
| conversation_history = [] | |
| return "", "", "Conversation cleared", "No intent detected", "Start a conversation..." | |
| def format_conversation(): | |
| """Format and display full conversation history""" | |
| if not conversation_history: | |
| return "No conversation history yet." | |
| formatted = "" | |
| for msg in conversation_history: | |
| role = msg.get("role", "unknown").upper() | |
| content = msg.get("content", "") | |
| formatted += f"**{role}:** {content}\n\n" | |
| return formatted | |
| # Create the Gradio interface | |
| with gr.Blocks(title="SAK Informatics", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown( | |
| """ | |
| # SAK Informatics Customer Responce Support Chatbot | |
| Welcome! This chatbot helps with customer support inquiries including orders, payments, refunds, shipping, and more. | |
| **Developed by Namani Vamshi Krishna** | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| gr.Markdown("### Chat Interface") | |
| chatbox = gr.Textbox( | |
| label="Your Message", | |
| placeholder="Type your question or concern here...", | |
| lines=3 | |
| ) | |
| submit_btn = gr.Button("Send", variant="primary", size="lg") | |
| clear_btn = gr.Button("Clear History", variant="secondary") | |
| with gr.Column(scale=1): | |
| gr.Markdown("### Intent Classification") | |
| intent_output = gr.Markdown("No intent detected") | |
| gr.Markdown("### Reasoning Details") | |
| reasoning_output = gr.Markdown("Start a conversation...") | |
| with gr.Row(): | |
| response_output = gr.Textbox( | |
| label="Chatbot Response", | |
| lines=5, | |
| interactive=False | |
| ) | |
| with gr.Row(): | |
| gr.Markdown("### Conversation History") | |
| history_output = gr.Textbox( | |
| label="Full Conversation", | |
| lines=8, | |
| interactive=False | |
| ) | |
| # Connect button click to chat function | |
| submit_btn.click( | |
| fn=chat_interface, | |
| inputs=[chatbox], | |
| outputs=[response_output, intent_output, reasoning_output] | |
| ).then( | |
| fn=format_conversation, | |
| inputs=[], | |
| outputs=[history_output] | |
| ).then( | |
| fn=lambda: "", | |
| inputs=[], | |
| outputs=[chatbox] | |
| ) | |
| # Handle Enter key | |
| chatbox.submit( | |
| fn=chat_interface, | |
| inputs=[chatbox], | |
| outputs=[response_output, intent_output, reasoning_output] | |
| ).then( | |
| fn=format_conversation, | |
| inputs=[], | |
| outputs=[history_output] | |
| ).then( | |
| fn=lambda: "", | |
| inputs=[], | |
| outputs=[chatbox] | |
| ) | |
| # Clear button functionality | |
| clear_btn.click( | |
| fn=clear_history, | |
| inputs=[], | |
| outputs=[chatbox, response_output, history_output, intent_output, reasoning_output] | |
| ) | |
| gr.Markdown( | |
| """ | |
| --- | |
| **Support Categories:** | |
| - Order Management (place, cancel, change) | |
| - Payment & Invoicing | |
| - Refunds & Returns | |
| - Shipping & Delivery | |
| - Account Management | |
| - Customer Service | |
| """ | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) |