Dinusha-Ekanayake commited on
Commit
a17fac3
·
verified ·
1 Parent(s): ac23e1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -1,52 +1,69 @@
1
  import gradio as gr
2
  import os
3
- from transformers import pipeline
 
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
- model_id = cat_path if os.path.exists(cat_path) else "Dinusha-Ekanayake/predictix-ticket_categorization_model"
13
- categorizer = pipeline("text-classification", model=model_id, top_k=None, token=hf_token)
 
14
  except Exception as e:
15
- categorizer = None
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
- model_id = sum_path if os.path.exists(sum_path) else "Dinusha-Ekanayake/predictix-ticket_summarization_model"
22
- # Use text2text-generation to bypass ALL pipeline_tag and path-checking bugs!
23
- ticket_summarizer = pipeline("text2text-generation", model=model_id, token=hf_token)
24
  except Exception as e:
25
- ticket_summarizer = None
26
  print(f"Failed to load ticket summarizer: {e}")
27
 
28
  # 3. Load Asset Summarization Model
29
  try:
30
- # Use text2text-generation to bypass the missing pipeline_tag!
31
- asset_summarizer = pipeline("text2text-generation", model="Dinusha-Ekanayake/predictix-asset_summarization_model", token=hf_token)
 
32
  except Exception as e:
33
- asset_summarizer = None
34
  print(f"Failed to load asset summarizer: {e}")
35
 
36
  # --- API Functions ---
37
  def categorize(text):
38
- if not categorizer: return {"error": "Categorization model not loaded."}
39
- return categorizer(text)
 
 
 
 
 
 
 
 
40
 
41
  def summarize_ticket(text):
42
- if not ticket_summarizer: return {"error": "Ticket Summarization model not loaded."}
43
- res = ticket_summarizer(text, min_length=15, max_length=150)
44
- return {"summary": res[0].get("generated_text", str(res[0]))}
 
 
 
45
 
46
  def summarize_asset(text):
47
- if not asset_summarizer: return {"error": "Asset Summarization model not loaded."}
48
- res = asset_summarizer(text, min_length=20, max_length=150)
49
- return {"summary": res[0].get("generated_text", str(res[0]))}
 
 
 
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: