Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
print("Booting up PredictiX Inference API...")
|
| 6 |
|
| 7 |
hf_token = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
# 1. Load Ticket Categorization Model
|
| 10 |
try:
|
| 11 |
cat_path = "./distilbert_category_model"
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
-
|
| 16 |
print(f"Failed to load categorizer: {e}")
|
| 17 |
|
| 18 |
# 2. Load Ticket Summarization Model
|
| 19 |
try:
|
| 20 |
sum_path = "./predictix-ticket_summarization_model"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
except Exception as e:
|
| 25 |
-
|
| 26 |
print(f"Failed to load ticket summarizer: {e}")
|
| 27 |
|
| 28 |
# 3. Load Asset Summarization Model
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
|
| 34 |
print(f"Failed to load asset summarizer: {e}")
|
| 35 |
|
| 36 |
# --- API Functions ---
|
| 37 |
def categorize(text):
|
| 38 |
-
if not
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def summarize_ticket(text):
|
| 42 |
-
if not
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def summarize_asset(text):
|
| 47 |
-
if not
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# --- Server API Interface ---
|
| 52 |
with gr.Blocks(title="PredictiX API") as demo:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
|
| 6 |
+
print("Booting up PredictiX Inference API (Direct Model Load)...")
|
| 7 |
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
|
| 10 |
# 1. Load Ticket Categorization Model
|
| 11 |
try:
|
| 12 |
cat_path = "./distilbert_category_model"
|
| 13 |
+
cat_id = cat_path if os.path.exists(cat_path) else "Dinusha-Ekanayake/predictix-ticket_categorization_model"
|
| 14 |
+
cat_tokenizer = AutoTokenizer.from_pretrained(cat_id, token=hf_token)
|
| 15 |
+
cat_model = AutoModelForSequenceClassification.from_pretrained(cat_id, token=hf_token)
|
| 16 |
except Exception as e:
|
| 17 |
+
cat_model = None
|
| 18 |
print(f"Failed to load categorizer: {e}")
|
| 19 |
|
| 20 |
# 2. Load Ticket Summarization Model
|
| 21 |
try:
|
| 22 |
sum_path = "./predictix-ticket_summarization_model"
|
| 23 |
+
ts_id = sum_path if os.path.exists(sum_path) else "Dinusha-Ekanayake/predictix-ticket_summarization_model"
|
| 24 |
+
ts_tokenizer = AutoTokenizer.from_pretrained(ts_id, token=hf_token)
|
| 25 |
+
ts_model = AutoModelForSeq2SeqLM.from_pretrained(ts_id, token=hf_token)
|
| 26 |
except Exception as e:
|
| 27 |
+
ts_model = None
|
| 28 |
print(f"Failed to load ticket summarizer: {e}")
|
| 29 |
|
| 30 |
# 3. Load Asset Summarization Model
|
| 31 |
try:
|
| 32 |
+
as_id = "Dinusha-Ekanayake/predictix-asset_summarization_model"
|
| 33 |
+
as_tokenizer = AutoTokenizer.from_pretrained(as_id, token=hf_token)
|
| 34 |
+
as_model = AutoModelForSeq2SeqLM.from_pretrained(as_id, token=hf_token)
|
| 35 |
except Exception as e:
|
| 36 |
+
as_model = None
|
| 37 |
print(f"Failed to load asset summarizer: {e}")
|
| 38 |
|
| 39 |
# --- API Functions ---
|
| 40 |
def categorize(text):
|
| 41 |
+
if not cat_model: return {"error": "Categorization model not loaded."}
|
| 42 |
+
inputs = cat_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 43 |
+
with torch.no_grad():
|
| 44 |
+
logits = cat_model(**inputs).logits
|
| 45 |
+
# Get highest score
|
| 46 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 47 |
+
best_idx = torch.argmax(probs).item()
|
| 48 |
+
label = cat_model.config.id2label[best_idx]
|
| 49 |
+
score = probs[best_idx].item()
|
| 50 |
+
return [{"label": label, "score": score}]
|
| 51 |
|
| 52 |
def summarize_ticket(text):
|
| 53 |
+
if not ts_model: return {"error": "Ticket Summarization model not loaded."}
|
| 54 |
+
inputs = ts_tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
|
| 55 |
+
with torch.no_grad():
|
| 56 |
+
outputs = ts_model.generate(**inputs, min_length=15, max_length=150, num_beams=4, early_stopping=True)
|
| 57 |
+
summary = ts_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 58 |
+
return {"summary": summary}
|
| 59 |
|
| 60 |
def summarize_asset(text):
|
| 61 |
+
if not as_model: return {"error": "Asset Summarization model not loaded."}
|
| 62 |
+
inputs = as_tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
outputs = as_model.generate(**inputs, min_length=20, max_length=150, num_beams=4, early_stopping=True)
|
| 65 |
+
summary = as_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 66 |
+
return {"summary": summary}
|
| 67 |
|
| 68 |
# --- Server API Interface ---
|
| 69 |
with gr.Blocks(title="PredictiX API") as demo:
|