vinayabc1824's picture
Update app.py
60a5842 verified
Raw History Blame Contribute Delete
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
)