Dinusha-Ekanayake commited on
Commit
181f20a
·
verified ·
1 Parent(s): c739ae1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -4,27 +4,32 @@ from transformers import pipeline
4
 
5
  print("Booting up PredictiX Inference API...")
6
 
7
- # 1. Load Ticket Categorization Model (From local folder)
 
 
 
8
  try:
9
  cat_path = "./distilbert_category_model"
10
  model_id = cat_path if os.path.exists(cat_path) else "Dinusha-Ekanayake/predictix-ticket_categorization_model"
11
- categorizer = pipeline("text-classification", model=model_id, top_k=None)
12
  except Exception as e:
13
  categorizer = None
14
  print(f"Failed to load categorizer: {e}")
15
 
16
- # 2. Load Ticket Summarization Model (From local folder)
17
  try:
18
  sum_path = "./predictix-ticket_summarization_model"
19
  model_id = sum_path if os.path.exists(sum_path) else "Dinusha-Ekanayake/predictix-ticket_summarization_model"
20
- ticket_summarizer = pipeline("summarization", model=model_id)
 
21
  except Exception as e:
22
  ticket_summarizer = None
23
  print(f"Failed to load ticket summarizer: {e}")
24
 
25
- # 3. Load Asset Summarization Model (From Hub)
26
  try:
27
- asset_summarizer = pipeline("summarization", model="Dinusha-Ekanayake/predictix-asset_summarization_model")
 
28
  except Exception as e:
29
  asset_summarizer = None
30
  print(f"Failed to load asset summarizer: {e}")
@@ -37,17 +42,16 @@ def categorize(text):
37
  def summarize_ticket(text):
38
  if not ticket_summarizer: return {"error": "Ticket Summarization model not loaded."}
39
  res = ticket_summarizer(text, min_length=15, max_length=150)
40
- return {"summary": res[0]["summary_text"]}
41
 
42
  def summarize_asset(text):
43
  if not asset_summarizer: return {"error": "Asset Summarization model not loaded."}
44
  res = asset_summarizer(text, min_length=20, max_length=150)
45
- return {"summary": res[0]["summary_text"]}
46
 
47
  # --- Server API Interface ---
48
  with gr.Blocks(title="PredictiX API") as demo:
49
  gr.Markdown("# PredictiX Internal Inference Server 🚀")
50
- gr.Markdown("This space holds the models in RAM and exposes them via REST API to the PredictiX backend.")
51
 
52
  with gr.Tab("Ticket Categorization"):
53
  cat_in = gr.Textbox(label="Ticket Title & Description")
 
4
 
5
  print("Booting up PredictiX Inference API...")
6
 
7
+ # Grab the secret token you added in the Space Settings
8
+ hf_token = os.environ.get("HF_TOKEN")
9
+
10
+ # 1. Load Ticket Categorization Model
11
  try:
12
  cat_path = "./distilbert_category_model"
13
  model_id = cat_path if os.path.exists(cat_path) else "Dinusha-Ekanayake/predictix-ticket_categorization_model"
14
+ categorizer = pipeline("text-classification", model=model_id, top_k=None, token=hf_token)
15
  except Exception as e:
16
  categorizer = None
17
  print(f"Failed to load categorizer: {e}")
18
 
19
+ # 2. Load Ticket Summarization Model
20
  try:
21
  sum_path = "./predictix-ticket_summarization_model"
22
  model_id = sum_path if os.path.exists(sum_path) else "Dinusha-Ekanayake/predictix-ticket_summarization_model"
23
+ # Auto-detect task to avoid "Unknown task" error!
24
+ ticket_summarizer = pipeline(model=model_id, token=hf_token)
25
  except Exception as e:
26
  ticket_summarizer = None
27
  print(f"Failed to load ticket summarizer: {e}")
28
 
29
+ # 3. Load Asset Summarization Model (Gated Repo)
30
  try:
31
+ # Auto-detect task and pass token to bypass the gate!
32
+ asset_summarizer = pipeline(model="Dinusha-Ekanayake/predictix-asset_summarization_model", token=hf_token)
33
  except Exception as e:
34
  asset_summarizer = None
35
  print(f"Failed to load asset summarizer: {e}")
 
42
  def summarize_ticket(text):
43
  if not ticket_summarizer: return {"error": "Ticket Summarization model not loaded."}
44
  res = ticket_summarizer(text, min_length=15, max_length=150)
45
+ return {"summary": res[0].get("summary_text", res[0].get("generated_text", str(res[0])))}
46
 
47
  def summarize_asset(text):
48
  if not asset_summarizer: return {"error": "Asset Summarization model not loaded."}
49
  res = asset_summarizer(text, min_length=20, max_length=150)
50
+ return {"summary": res[0].get("summary_text", res[0].get("generated_text", str(res[0])))}
51
 
52
  # --- Server API Interface ---
53
  with gr.Blocks(title="PredictiX API") as demo:
54
  gr.Markdown("# PredictiX Internal Inference Server 🚀")
 
55
 
56
  with gr.Tab("Ticket Categorization"):
57
  cat_in = gr.Textbox(label="Ticket Title & Description")