Spaces:
Build error
Build error
Download app.py from vinayabc1824/AI-Customer-Ticket-Resolution-Bot: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/vinayabc1824/AI-Customer-Ticket-Resolution-Bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/vinayabc1824/AI-Customer-Ticket-Resolution-Bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/vinayabc1824/AI-Customer-Ticket-Resolution-Bot/resolve/main/app.py
10.8 kB
| import gradio as gr | |
| import os | |
| import sys | |
| from pathlib import Path | |
| # Add the current directory to Python path | |
| sys.path.append(str(Path(__file__).parent)) | |
| # Import only what we need for the Gradio interface | |
| from config import settings | |
| import threading | |
| import time | |
| from loguru import logger | |
| # Initialize AI Engine | |
| ai_engine = None | |
| initialization_status = "β³ Initializing..." | |
| def initialize_ai_engine(): | |
| """Initialize the AI engine in a separate thread""" | |
| global ai_engine, initialization_status | |
| try: | |
| initialization_status = "π Loading AI Models..." | |
| logger.info("Initializing AI Engine...") | |
| # Set environment variables for faster loading | |
| os.environ["TOKENIZERS_PARALLELISM"] = "false" | |
| os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache" | |
| # Import here to avoid startup issues | |
| from ai_engine import AIEngine | |
| ai_engine = AIEngine() | |
| initialization_status = "β AI Engine Ready!" | |
| logger.info("AI Engine initialized successfully!") | |
| except Exception as e: | |
| initialization_status = f"β Error: {str(e)}" | |
| logger.error(f"Error initializing AI Engine: {e}") | |
| def test_ticket_classification(subject, description): | |
| """Test ticket classification with the AI engine""" | |
| global ai_engine, initialization_status | |
| if ai_engine is None: | |
| return f""" | |
| ## β³ **AI Engine Status** | |
| {initialization_status} | |
| **Please wait a moment and try again.** The AI models are being loaded for the first time, which may take 1-2 minutes. | |
| ### **What's happening:** | |
| - Loading classification model (`facebook/bart-large-mnli`) | |
| - Loading embedding model (`sentence-transformers/all-MiniLM-L6-v2`) | |
| - Initializing knowledge base | |
| - Setting up RAG system | |
| **Try again in 30-60 seconds!** π | |
| """ | |
| try: | |
| # Categorize the ticket | |
| tier, confidence, category = ai_engine.categorize_ticket(subject, description) | |
| # Get RAG response | |
| rag_response = ai_engine.get_rag_response(f"{subject} {description}") | |
| result = f""" | |
| ## π― **Ticket Classification Results** | |
| **Subject:** {subject} | |
| **Description:** {description} | |
| ### π **Classification:** | |
| - **Tier:** {tier.upper()} | |
| - **Category:** {category} | |
| - **Confidence:** {confidence:.2%} | |
| ### π€ **AI Response:** | |
| {rag_response} | |
| ### π **Next Steps:** | |
| """ | |
| if tier == "tier_1": | |
| result += "- β **Auto-resolved** - Simple issue handled automatically" | |
| elif tier == "tier_2": | |
| result += "- π **Escalated to Tier 2** - Moderate complexity requiring specialized attention" | |
| else: | |
| result += "- π¨ **Escalated to Human Agent** - Complex issue requiring human intervention" | |
| return result | |
| except Exception as e: | |
| logger.error(f"Error in ticket classification: {e}") | |
| return f""" | |
| ## β **Error Processing Ticket** | |
| **Error:** {str(e)} | |
| **Please try again or check the system status tab for more information.** | |
| """ | |
| def test_rag_query(query): | |
| """Test RAG functionality with a query""" | |
| global ai_engine, initialization_status | |
| if ai_engine is None: | |
| return f""" | |
| ## β³ **AI Engine Status** | |
| {initialization_status} | |
| **Please wait a moment and try again.** The AI models are being loaded for the first time. | |
| **Try again in 30-60 seconds!** π | |
| """ | |
| try: | |
| response = ai_engine.get_rag_response(query) | |
| return f""" | |
| ## π **RAG Query Results** | |
| **Query:** {query} | |
| ### π **Response:** | |
| {response} | |
| """ | |
| except Exception as e: | |
| logger.error(f"Error in RAG query: {e}") | |
| return f""" | |
| ## β **Error Processing Query** | |
| **Error:** {str(e)} | |
| **Please try again or check the system status tab for more information.** | |
| """ | |
| def get_system_status(): | |
| """Get system status and health""" | |
| global ai_engine, initialization_status | |
| status = { | |
| "AI Engine": "β Ready" if ai_engine is not None else initialization_status, | |
| "Models Loaded": "β Yes" if ai_engine is not None else "β No", | |
| "Knowledge Base": "β Loaded" if ai_engine and hasattr(ai_engine, 'knowledge_texts') and ai_engine.knowledge_texts else "β Not Available", | |
| "Freshdesk Integration": "β Configured" if settings.FRESHDESK_API_KEY else "β Not Configured", | |
| "Webhook URL": "β Available" if settings.WEBHOOK_URL else "β Not Set" | |
| } | |
| status_text = "\n".join([f"- **{key}:** {value}" for key, value in status.items()]) | |
| return f""" | |
| ## π₯ **System Status** | |
| {status_text} | |
| ### π **Configuration:** | |
| - **Freshdesk Domain:** {settings.FRESHDESK_DOMAIN or 'Not Set'} | |
| - **API Key:** {'β Configured' if settings.FRESHDESK_API_KEY else 'β Not Set'} | |
| - **Webhook Secret:** {'β Configured' if settings.WEBHOOK_SECRET else 'β Not Set'} | |
| ### π§ **Troubleshooting:** | |
| - If AI Engine shows "Initializing", wait 1-2 minutes and try again | |
| - First-time model loading can take up to 2 minutes | |
| - Check the logs tab for detailed error information | |
| """ | |
| # Create Gradio interface | |
| with gr.Blocks( | |
| title="AI Customer Ticket Resolution Bot", | |
| theme=gr.themes.Soft(), | |
| css=""" | |
| .gradio-container { | |
| max-width: 1200px !important; | |
| } | |
| """ | |
| ) as demo: | |
| gr.Markdown(""" | |
| # π€ AI Customer Ticket Resolution Bot | |
| **Real-time AI-powered customer support automation with Freshdesk integration** | |
| --- | |
| """) | |
| with gr.Tabs(): | |
| # Tab 1: Ticket Classification | |
| with gr.TabItem("π― Ticket Classification"): | |
| gr.Markdown(""" | |
| ### Test Ticket Classification | |
| Enter a ticket subject and description to see how the AI categorizes it. | |
| **Note:** On first load, AI models may take 1-2 minutes to initialize. | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| subject_input = gr.Textbox( | |
| label="Ticket Subject", | |
| placeholder="e.g., Password reset not working", | |
| lines=2 | |
| ) | |
| description_input = gr.Textbox( | |
| label="Ticket Description", | |
| placeholder="e.g., I've been trying to reset my password for 2 hours but the link in the email doesn't work...", | |
| lines=4 | |
| ) | |
| classify_btn = gr.Button("π― Classify Ticket", variant="primary") | |
| with gr.Column(): | |
| classification_output = gr.Markdown(label="Classification Results") | |
| classify_btn.click( | |
| fn=test_ticket_classification, | |
| inputs=[subject_input, description_input], | |
| outputs=classification_output | |
| ) | |
| # Tab 2: RAG Testing | |
| with gr.TabItem("π RAG Query Testing"): | |
| gr.Markdown(""" | |
| ### Test Retrieval-Augmented Generation (RAG) | |
| Ask questions to test the knowledge base retrieval system. | |
| **Note:** On first load, AI models may take 1-2 minutes to initialize. | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| rag_query = gr.Textbox( | |
| label="Query", | |
| placeholder="e.g., How do I reset my password?", | |
| lines=3 | |
| ) | |
| rag_btn = gr.Button("π Get RAG Response", variant="primary") | |
| with gr.Column(): | |
| rag_output = gr.Markdown(label="RAG Response") | |
| rag_btn.click( | |
| fn=test_rag_query, | |
| inputs=[rag_query], | |
| outputs=rag_output | |
| ) | |
| # Tab 3: System Status | |
| with gr.TabItem("π₯ System Status"): | |
| gr.Markdown(""" | |
| ### System Health and Configuration | |
| Check the status of all system components. | |
| """) | |
| status_btn = gr.Button("π Refresh Status", variant="secondary") | |
| status_output = gr.Markdown(label="System Status") | |
| status_btn.click( | |
| fn=get_system_status, | |
| inputs=[], | |
| outputs=status_output | |
| ) | |
| # Tab 4: Documentation | |
| with gr.TabItem("π Documentation"): | |
| gr.Markdown(""" | |
| ## π **AI Customer Ticket Resolution Bot** | |
| ### **Features:** | |
| - π€ **AI-Powered Classification:** Automatically categorizes tickets into Tier 1, Tier 2, or Complex | |
| - π **RAG System:** Retrieves relevant information from knowledge base | |
| - π **Freshdesk Integration:** Seamless webhook integration | |
| - π **Real-time Processing:** Instant ticket analysis and response | |
| ### **How It Works:** | |
| 1. **Ticket Reception:** Freshdesk sends webhook notifications | |
| 2. **AI Analysis:** Ticket is analyzed using zero-shot classification | |
| 3. **Knowledge Retrieval:** RAG system finds relevant documentation | |
| 4. **Response Generation:** AI generates appropriate responses | |
| 5. **Escalation:** Complex tickets are escalated to human agents | |
| ### **Deployment:** | |
| - **Backend:** FastAPI with uvicorn | |
| - **Frontend:** Gradio interface | |
| - **Platform:** Hugging Face Spaces | |
| - **Database:** SQLite for ticket storage | |
| ### **Models Used:** | |
| - **Classification:** `facebook/bart-large-mnli` | |
| - **Embeddings:** `sentence-transformers/all-MiniLM-L6-v2` | |
| - **Similarity:** Cosine similarity with scikit-learn | |
| --- | |
| **Built with β€οΈ for automated customer support** | |
| """) | |
| # Initialize AI Engine in background | |
| gr.Markdown(""" | |
| <div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin: 20px 0;"> | |
| <h3>π Ready for Production Deployment</h3> | |
| <p>This bot is designed to handle real customer support tickets with AI-powered automation.</p> | |
| </div> | |
| """) | |
| # Start AI Engine initialization in background | |
| threading.Thread(target=initialize_ai_engine, daemon=True).start() | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| show_error=True | |
| ) |