import gradio as gr from src.inference import predict_ticket # Uses the fixed inference.py with nltk fix def predict_interface(ticket_text): try: result = predict_ticket(ticket_text) issue = result.get("issue_type", "Unknown") urgency = result.get("urgency_level", "Unknown") entities = result.get("entities", {}) # Format entity output lines = [] for key in ["products", "dates", "complaints"]: vals = entities.get(key, []) lines.append(f"{key.capitalize()}: {', '.join(vals) if vals else 'None'}") entities_str = "\n".join(lines) return issue, urgency, entities_str except Exception as e: return f"Prediction error: {str(e)}", "Prediction error", "Prediction error" # Build the Gradio interface iface = gr.Interface( fn=predict_interface, inputs=gr.Textbox( label="📝 Customer Support Ticket", lines=6, placeholder=( "Describe your issue clearly.\n" "Example: 'I returned the washing machine on 10th May but no refund received.'" ) ), outputs=[ gr.Textbox(label="📌 Predicted Issue Type"), gr.Textbox(label="⏱️ Predicted Urgency Level"), gr.Textbox(label="🧠 Extracted Entities"), ], title="📬 Customer Support Ticket Analyzer", description=( "Paste a customer support ticket. This tool uses machine learning to predict:\n\n" "- 📌 Issue Type (e.g., Late Delivery, Refund)\n" "- ⏱️ Urgency Level (Low / Medium / High)\n" "- 🧠 Extracted Entities (Products, Dates, Complaints)" ), allow_flagging="never" ) if __name__ == "__main__": iface.launch()