Sirapatsorn commited on
Commit
ce5cc2b
·
verified ·
1 Parent(s): 0839f6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -19
app.py CHANGED
@@ -3,33 +3,42 @@ import torch
3
  import os
4
  from transformers import BertTokenizer, BertForSequenceClassification, XLNetTokenizer, XLNetForSequenceClassification
5
 
6
- # โหลดโมเดลของคุณ
7
- logbert_model = BertForSequenceClassification.from_pretrained("Sirapatsorn/Spark_Log_Analysis", token=os.getenv("HUGGINGFACE_TOKEN"))
8
- xlnet_model = XLNetForSequenceClassification.from_pretrained("Sirapatsorn/Spark_Log_Analysis", token=os.getenv("HUGGINGFACE_TOKEN"))
 
 
 
 
 
 
 
 
9
  logbert_tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
10
  xlnet_tokenizer = XLNetTokenizer.from_pretrained("xlnet-base-cased")
11
 
12
- # ตรวจสอบว่ามี GPU หรือไม่
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  logbert_model.to(device)
15
  xlnet_model.to(device)
16
 
17
- # ฟังก์ชันการพยากรณ์
18
  def predict_log(text):
 
19
  logbert_inputs = logbert_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
20
  with torch.no_grad():
21
  logbert_outputs = logbert_model(**logbert_inputs)
22
- log_level = torch.argmax(logbert_outputs.logits, dim=1).item()
23
- log_level_confidence = torch.softmax(logbert_outputs.logits, dim=1)[0][log_level].item()
24
-
25
  log_levels = ["INFO", "WARN", "ERROR"]
26
  log_level_result = log_levels[log_level]
27
 
 
28
  xlnet_inputs = xlnet_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
29
  with torch.no_grad():
30
  xlnet_outputs = xlnet_model(**xlnet_inputs)
31
- performance_value = xlnet_outputs.logits.item()
32
-
 
33
  if performance_value < 0:
34
  performance_status = "Good Performance"
35
  elif performance_value < 3.0:
@@ -44,7 +53,6 @@ def predict_log(text):
44
  "Performance Status": performance_status
45
  }
46
 
47
- # ฟังก์ชันสำหรับ Gradio: พยากรณ์จากข้อความโดยตรง
48
  def predict_from_text(text):
49
  prediction = predict_log(text)
50
  return (f"Log Level: {prediction['Log Level']} (Confidence: {prediction['Confidence']})\n"
@@ -57,31 +65,36 @@ def predict_from_file(file):
57
  for line in f:
58
  prediction = predict_log(line.strip())
59
  result_text = (f"Log: {line.strip()}\n"
60
- f"Log Level: {prediction['Log Level']} (Confidence: {prediction['Confidence']})\n"
61
- f"Performance Value: {prediction['Performance Value']}\n"
62
- f"Performance Status: {prediction['Performance Status']}")
63
  results.append(result_text)
64
  return "\n\n".join(results)
65
 
 
66
  custom_css = """
67
  .gr-button {
68
- background-color: #FFA500 !important;
69
  color: #FFFFFF !important;
70
  border: none !important;
71
  }
72
  """
73
 
74
  with gr.Blocks(css=custom_css) as demo:
 
 
 
75
  with gr.Tabs():
76
  with gr.TabItem("Upload File"):
77
  file_upload = gr.File(label="Upload Log File")
78
  file_btn = gr.Button("Predict")
79
- file_output = gr.Textbox(label="Output")
80
  file_btn.click(predict_from_file, inputs=file_upload, outputs=file_output)
 
81
  with gr.TabItem("Text Input"):
82
- text_input = gr.Textbox(label="Enter Log Message")
83
  text_btn = gr.Button("Predict")
84
- text_output = gr.Textbox(label="Output")
85
  text_btn.click(predict_from_text, inputs=text_input, outputs=text_output)
86
 
87
  demo.css += """
@@ -90,4 +103,5 @@ with gr.Blocks(css=custom_css) as demo:
90
  }
91
  """
92
 
93
- demo.launch()
 
 
3
  import os
4
  from transformers import BertTokenizer, BertForSequenceClassification, XLNetTokenizer, XLNetForSequenceClassification
5
 
6
+ # Load models
7
+ logbert_model = BertForSequenceClassification.from_pretrained(
8
+ "Sirapatsorn/Spark_Log_Analysis-logbert", # แก้เป็น path ที่ถูกต้อง
9
+ token=os.getenv("HUGGINGFACE_TOKEN")
10
+ )
11
+ xlnet_model = XLNetForSequenceClassification.from_pretrained(
12
+ "Sirapatsorn/Spark_Log_Analysis-xlnet", # แก้เป็น path ที่ถูกต้อง
13
+ token=os.getenv("HUGGINGFACE_TOKEN")
14
+ )
15
+
16
+ # Load tokenizers
17
  logbert_tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
18
  xlnet_tokenizer = XLNetTokenizer.from_pretrained("xlnet-base-cased")
19
 
20
+ # Check for GPU
21
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
  logbert_model.to(device)
23
  xlnet_model.to(device)
24
 
 
25
  def predict_log(text):
26
+ # LogBERT prediction
27
  logbert_inputs = logbert_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
28
  with torch.no_grad():
29
  logbert_outputs = logbert_model(**logbert_inputs)
30
+ log_level = torch.argmax(logbert_outputs.logits, dim=1).item()
31
+ log_level_confidence = torch.softmax(logbert_outputs.logits, dim=1)[0][log_level].item()
 
32
  log_levels = ["INFO", "WARN", "ERROR"]
33
  log_level_result = log_levels[log_level]
34
 
35
+ # XLNet prediction
36
  xlnet_inputs = xlnet_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
37
  with torch.no_grad():
38
  xlnet_outputs = xlnet_model(**xlnet_inputs)
39
+ performance_value = xlnet_outputs.logits.item()
40
+
41
+ # Determine performance status
42
  if performance_value < 0:
43
  performance_status = "Good Performance"
44
  elif performance_value < 3.0:
 
53
  "Performance Status": performance_status
54
  }
55
 
 
56
  def predict_from_text(text):
57
  prediction = predict_log(text)
58
  return (f"Log Level: {prediction['Log Level']} (Confidence: {prediction['Confidence']})\n"
 
65
  for line in f:
66
  prediction = predict_log(line.strip())
67
  result_text = (f"Log: {line.strip()}\n"
68
+ f"Log Level: {prediction['Log Level']} (Confidence: {prediction['Confidence']})\n"
69
+ f"Performance Value: {prediction['Performance Value']}\n"
70
+ f"Performance Status: {prediction['Performance Status']}")
71
  results.append(result_text)
72
  return "\n\n".join(results)
73
 
74
+ # Create Gradio interface
75
  custom_css = """
76
  .gr-button {
77
+ background-color: #FFA500 !important;
78
  color: #FFFFFF !important;
79
  border: none !important;
80
  }
81
  """
82
 
83
  with gr.Blocks(css=custom_css) as demo:
84
+ gr.Markdown("# Spark Log Analysis")
85
+ gr.Markdown("Analyze your log messages for log level and performance prediction")
86
+
87
  with gr.Tabs():
88
  with gr.TabItem("Upload File"):
89
  file_upload = gr.File(label="Upload Log File")
90
  file_btn = gr.Button("Predict")
91
+ file_output = gr.Textbox(label="Output", lines=10)
92
  file_btn.click(predict_from_file, inputs=file_upload, outputs=file_output)
93
+
94
  with gr.TabItem("Text Input"):
95
+ text_input = gr.Textbox(label="Enter Log Message", lines=3)
96
  text_btn = gr.Button("Predict")
97
+ text_output = gr.Textbox(label="Output", lines=5)
98
  text_btn.click(predict_from_text, inputs=text_input, outputs=text_output)
99
 
100
  demo.css += """
 
103
  }
104
  """
105
 
106
+ # Launch the app
107
+ demo.launch()