Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.txt +31 -0
- app.py +91 -0
- requirements.txt +8 -0
README.txt
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Queue Prediction Model API
|
| 2 |
+
|
| 3 |
+
This API predicts the appropriate queue for support tickets based on their title and description.
|
| 4 |
+
|
| 5 |
+
## API Endpoints
|
| 6 |
+
|
| 7 |
+
### POST /predict
|
| 8 |
+
|
| 9 |
+
Predicts the queue for a ticket based on its title and description.
|
| 10 |
+
|
| 11 |
+
#### Input Format:
|
| 12 |
+
```json
|
| 13 |
+
{
|
| 14 |
+
"title": "string",
|
| 15 |
+
"description": "string"
|
| 16 |
+
}
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
#### Output Format:
|
| 20 |
+
```json
|
| 21 |
+
{
|
| 22 |
+
"predicted_queue": "string",
|
| 23 |
+
"confidence": float,
|
| 24 |
+
"top_3_predictions": [
|
| 25 |
+
{
|
| 26 |
+
"queue": "string",
|
| 27 |
+
"probability": float
|
| 28 |
+
}
|
| 29 |
+
]
|
| 30 |
+
}
|
| 31 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from typing import Optional
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
# Load the model and label encoder
|
| 9 |
+
model, label_encoder = joblib.load('queue_prediction_model.joblib')
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="Queue Prediction API")
|
| 12 |
+
|
| 13 |
+
class TicketInput(BaseModel):
|
| 14 |
+
title: str
|
| 15 |
+
description: str
|
| 16 |
+
|
| 17 |
+
class TicketPrediction(BaseModel):
|
| 18 |
+
predicted_queue: str
|
| 19 |
+
confidence: float
|
| 20 |
+
top_3_predictions: list
|
| 21 |
+
|
| 22 |
+
# Copy your helper functions
|
| 23 |
+
def clean_text(text):
|
| 24 |
+
if pd.isna(text):
|
| 25 |
+
return ""
|
| 26 |
+
text = str(text)
|
| 27 |
+
text = re.sub(r'\S+@\S+', 'EMAIL', text)
|
| 28 |
+
text = re.sub(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', 'IP_ADDRESS', text)
|
| 29 |
+
text = re.sub(r'\d{1,2}/\d{1,2}/\d{4}', 'DATE', text)
|
| 30 |
+
text = re.sub(r'\b\d+\b', 'NUM', text)
|
| 31 |
+
text = text.lower()
|
| 32 |
+
return text
|
| 33 |
+
|
| 34 |
+
def extract_features(df):
|
| 35 |
+
# Copy your extract_features function here
|
| 36 |
+
# (The same function from your original code)
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
@app.post("/predict", response_model=TicketPrediction)
|
| 40 |
+
async def predict(ticket: TicketInput):
|
| 41 |
+
try:
|
| 42 |
+
# Create a DataFrame with the input data
|
| 43 |
+
sample = pd.DataFrame({
|
| 44 |
+
'Description': [ticket.description],
|
| 45 |
+
'Title': [ticket.title],
|
| 46 |
+
'Priority': ['Normal']
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
# Process features
|
| 50 |
+
sample_processed = extract_features(sample)
|
| 51 |
+
|
| 52 |
+
feature_columns = [
|
| 53 |
+
'cleaned_description', 'cleaned_title', 'has_ups', 'has_battery',
|
| 54 |
+
'has_problem', 'has_ip', 'has_serial', 'is_security_related',
|
| 55 |
+
'is_network_related', 'is_programming_related', 'is_support_related',
|
| 56 |
+
'description_length', 'title_length', 'word_count', 'is_high_priority'
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
X = sample_processed[feature_columns]
|
| 60 |
+
|
| 61 |
+
# Make prediction
|
| 62 |
+
prediction = model.predict(X)
|
| 63 |
+
probabilities = model.predict_proba(X)
|
| 64 |
+
|
| 65 |
+
predicted_queue = label_encoder.inverse_transform(prediction)[0]
|
| 66 |
+
confidence = float(np.max(probabilities[0]))
|
| 67 |
+
|
| 68 |
+
# Get top 3 predictions
|
| 69 |
+
top_3_idx = np.argsort(probabilities[0])[-3:][::-1]
|
| 70 |
+
top_3_predictions = [
|
| 71 |
+
{
|
| 72 |
+
"queue": queue,
|
| 73 |
+
"probability": float(prob)
|
| 74 |
+
}
|
| 75 |
+
for queue, prob in zip(
|
| 76 |
+
label_encoder.inverse_transform(top_3_idx),
|
| 77 |
+
probabilities[0][top_3_idx]
|
| 78 |
+
)
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
"predicted_queue": predicted_queue,
|
| 83 |
+
"confidence": confidence,
|
| 84 |
+
"top_3_predictions": top_3_predictions
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.68.0
|
| 2 |
+
uvicorn==0.15.0
|
| 3 |
+
pydantic==1.8.2
|
| 4 |
+
scikit-learn==0.24.2
|
| 5 |
+
pandas==1.3.3
|
| 6 |
+
numpy==1.21.2
|
| 7 |
+
joblib==1.0.1
|
| 8 |
+
python-multipart==0.0.5
|