Dinusha-Ekanayake commited on
Commit
c739ae1
·
verified ·
1 Parent(s): 355650d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ 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}")
31
+
32
+ # --- API Functions ---
33
+ def categorize(text):
34
+ if not categorizer: return {"error": "Categorization model not loaded."}
35
+ return categorizer(text)
36
+
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")
54
+ cat_out = gr.JSON(label="Categorization Result")
55
+ cat_btn = gr.Button("Categorize")
56
+ cat_btn.click(categorize, inputs=cat_in, outputs=cat_out, api_name="categorize")
57
+
58
+ with gr.Tab("Ticket Summarization"):
59
+ ts_in = gr.Textbox(label="Ticket Details")
60
+ ts_out = gr.JSON(label="Summary")
61
+ ts_btn = gr.Button("Summarize Ticket")
62
+ ts_btn.click(summarize_ticket, inputs=ts_in, outputs=ts_out, api_name="summarize_ticket")
63
+
64
+ with gr.Tab("Asset Summarization"):
65
+ as_in = gr.Textbox(label="Asset Details")
66
+ as_out = gr.JSON(label="Summary")
67
+ as_btn = gr.Button("Summarize Asset")
68
+ as_btn.click(summarize_asset, inputs=as_in, outputs=as_out, api_name="summarize_asset")
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()