sickcell commited on
Commit
04c1d54
1 Parent(s): 721cbd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -58
app.py CHANGED
@@ -51,8 +51,7 @@ def semantic_search(query, top_k=3):
51
  results.append({
52
  'text': data[idx]['text'],
53
  'similarity_score': 1 - distances[0][i] / 2,
54
- 'entity_groups': get_entity_groups(data[idx]['entities']),
55
- 'entities': data[idx]['entities']
56
  })
57
  return results
58
 
@@ -60,42 +59,10 @@ def search_and_format(query):
60
  results = semantic_search(query)
61
  formatted_results = ""
62
  for i, result in enumerate(results, 1):
63
- formatted_results += f"<h3>結果 {i}:</h3>"
64
- formatted_results += "<h4>NER 定義</h4>"
65
-
66
- # Split the text into words
67
- words = result['text'].split()
68
-
69
- # Create a dictionary to map entity groups to colors
70
- color_map = {
71
- 'PERSON': 'lightpink',
72
- 'ORG': 'lightblue',
73
- 'PLACE': 'lightyellow',
74
- 'TECHNOLOGY': 'lightgreen',
75
- 'MALWARE': 'plum',
76
- 'ATTACK': 'peachpuff'
77
- }
78
-
79
- # Format each word based on its entity group
80
- formatted_text = []
81
- for word in words:
82
- found = False
83
- for entity in result['entities']:
84
- if word in entity['word']:
85
- color = color_map.get(entity['entity_group'], 'lightgray')
86
- formatted_word = f'<span style="background-color: {color};">{word} <sup>{entity["entity_group"]}</sup></span>'
87
- formatted_text.append(formatted_word)
88
- found = True
89
- break
90
- if not found:
91
- formatted_text.append(word)
92
-
93
- # Join the formatted words back into a sentence
94
- formatted_results += ' '.join(formatted_text) + "<br><br>"
95
- formatted_results += f"<strong>相似度分數:</strong> {result['similarity_score']:.4f}<br><br>"
96
-
97
  return formatted_results
98
-
99
  # 示例問題
100
  example_queries = [
101
  "Tell me about recent cyber attacks from Russia",
@@ -112,35 +79,38 @@ example_queries = [
112
 
113
  # 自定義 CSS
114
  custom_css = """
115
- body {font-family: Arial, sans-serif;}
116
- .container {max-width: 900px; margin: auto;}
117
- .input-row {display: flex; gap: 10px; margin-bottom: 20px;}
118
- .query-input {flex-grow: 1;}
119
- .output-area {border: 1px solid #ddd; padding: 15px; border-radius: 5px;}
120
- .examples-grid {display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; margin-top: 20px;}
121
- .example-button {width: 100%;}
122
- span sup {font-size: 0.7em; font-weight: bold;}
123
  """
124
 
125
  # 創建Gradio界面
126
  with gr.Blocks(css=custom_css) as iface:
127
  gr.Markdown("# AskCTI")
128
- gr.Markdown("輸入查詢以搜索相關威脅情報,將顯示前3個最相關的結果,包括實體標註。")
129
-
130
- with gr.Row(class_name="input-row"):
131
- query_input = gr.Textbox(lines=3, label="", placeholder="輸入你的查詢...", class_name="query-input")
132
- submit_btn = gr.Button("查詢", variant="primary")
133
 
134
- output = gr.HTML(class_name="output-area")
135
-
136
- gr.Markdown("### 範例查詢")
137
- with gr.Row(class_name="examples-grid"):
138
- for query in example_queries:
139
- gr.Button(query, class_name="example-button").click(
140
- lambda x: x, inputs=[gr.Textbox(value=query, visible=False)], outputs=[query_input]
141
- )
 
 
 
 
 
 
 
 
 
142
 
143
  submit_btn.click(search_and_format, inputs=[query_input], outputs=[output])
 
144
 
145
  # 啟動Gradio界面
146
  iface.launch()
 
51
  results.append({
52
  'text': data[idx]['text'],
53
  'similarity_score': 1 - distances[0][i] / 2,
54
+ 'entity_groups': get_entity_groups(data[idx]['entities'])
 
55
  })
56
  return results
57
 
 
59
  results = semantic_search(query)
60
  formatted_results = ""
61
  for i, result in enumerate(results, 1):
62
+ formatted_results += f"{i}. 相似度分數: {result['similarity_score']:.4f}\n"
63
+ formatted_results += f" 情資: {result['text']}\n"
64
+ formatted_results += f" 命名實體: {', '.join(result['entity_groups'])}\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  return formatted_results
 
66
  # 示例問題
67
  example_queries = [
68
  "Tell me about recent cyber attacks from Russia",
 
79
 
80
  # 自定義 CSS
81
  custom_css = """
82
+ .container {display: flex; flex-direction: row;}
83
+ .input-column {flex: 1; padding-right: 20px;}
84
+ .output-column {flex: 2;}
85
+ .examples-list {display: flex; flex-wrap: wrap; gap: 10px;}
86
+ .examples-list > * {flex-basis: calc(50% - 5px);}
 
 
 
87
  """
88
 
89
  # 創建Gradio界面
90
  with gr.Blocks(css=custom_css) as iface:
91
  gr.Markdown("# AskCTI")
92
+ gr.Markdown("輸入查詢以搜索相關威脅情報,將顯示前3個最相關的結果,包括實體組。")
 
 
 
 
93
 
94
+ with gr.Row(equal_height=True):
95
+ with gr.Column(scale=1, min_width=300):
96
+ query_input = gr.Textbox(lines=3, label="")
97
+ submit_btn = gr.Button("查詢")
98
+ #clear_btn = gr.Button("清除")
99
+
100
+ gr.Markdown("### 範例查詢")
101
+ for i in range(0, len(example_queries), 2):
102
+ with gr.Row():
103
+ for j in range(2):
104
+ if i + j < len(example_queries):
105
+ gr.Button(example_queries[i+j]).click(
106
+ lambda x: x, inputs=[gr.Textbox(value=example_queries[i+j], visible=False)], outputs=[query_input]
107
+ )
108
+
109
+ with gr.Column(scale=2):
110
+ output = gr.Textbox(lines=20, label="")
111
 
112
  submit_btn.click(search_and_format, inputs=[query_input], outputs=[output])
113
+ #clear_btn.click(lambda: "", outputs=[query_input])
114
 
115
  # 啟動Gradio界面
116
  iface.launch()