Update app.py
Browse files
app.py
CHANGED
|
@@ -112,9 +112,17 @@ def generate_charts(ner_output_ext: dict) -> Tuple[go.Figure, np.ndarray]:
|
|
| 112 |
return fig1, wordcloud_image
|
| 113 |
|
| 114 |
def generate_wordcloud(entities: List[Dict], color_map: Dict[str, str], file_path: str) -> np.ndarray:
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
mask_image = np.array(Image.open(image_path))
|
| 119 |
|
| 120 |
token_texts = []
|
|
@@ -123,14 +131,12 @@ def generate_wordcloud(entities: List[Dict], color_map: Dict[str, str], file_pat
|
|
| 123 |
|
| 124 |
for entity in entities:
|
| 125 |
for token in entity['tokens']:
|
| 126 |
-
# Remove any leading non-alphanumeric characters
|
| 127 |
cleaned_token = re.sub(r'^\W+', '', token)
|
| 128 |
token_texts.append(cleaned_token)
|
| 129 |
token_scores.append(entity['score'])
|
| 130 |
token_types.append(entity['entity'])
|
| 131 |
print(f"{cleaned_token} ({entity['entity']}): {entity['score']}")
|
| 132 |
|
| 133 |
-
# Create a dictionary for word cloud
|
| 134 |
word_freq = {text: score for text, score in zip(token_texts, token_scores)}
|
| 135 |
|
| 136 |
def color_func(word, font_size, position, orientation, random_state=None, **kwargs):
|
|
@@ -139,13 +145,11 @@ def generate_wordcloud(entities: List[Dict], color_map: Dict[str, str], file_pat
|
|
| 139 |
|
| 140 |
wordcloud = WordCloud(width=800, height=400, background_color='#121212', mask=mask_image, color_func=color_func).generate_from_frequencies(word_freq)
|
| 141 |
|
| 142 |
-
# Convert to image array
|
| 143 |
plt.figure(figsize=(10, 5))
|
| 144 |
plt.imshow(wordcloud, interpolation='bilinear')
|
| 145 |
plt.axis('off')
|
| 146 |
plt.tight_layout(pad=0)
|
| 147 |
|
| 148 |
-
# Convert plt to numpy array
|
| 149 |
plt_image = plt.gcf()
|
| 150 |
plt_image.canvas.draw()
|
| 151 |
image_array = np.frombuffer(plt_image.canvas.tostring_rgb(), dtype=np.uint8)
|
|
|
|
| 112 |
return fig1, wordcloud_image
|
| 113 |
|
| 114 |
def generate_wordcloud(entities: List[Dict], color_map: Dict[str, str], file_path: str) -> np.ndarray:
|
| 115 |
+
# Construct the absolute path
|
| 116 |
+
base_path = os.path.dirname(os.path.abspath(__file__))
|
| 117 |
+
image_path = os.path.join(base_path, file_path)
|
| 118 |
|
| 119 |
+
# Debugging statement to print the image path
|
| 120 |
+
print(f"Image path: {image_path}")
|
| 121 |
+
|
| 122 |
+
# Check if the file exists
|
| 123 |
+
if not os.path.exists(image_path):
|
| 124 |
+
raise FileNotFoundError(f"Mask image file not found: {image_path}")
|
| 125 |
+
|
| 126 |
mask_image = np.array(Image.open(image_path))
|
| 127 |
|
| 128 |
token_texts = []
|
|
|
|
| 131 |
|
| 132 |
for entity in entities:
|
| 133 |
for token in entity['tokens']:
|
|
|
|
| 134 |
cleaned_token = re.sub(r'^\W+', '', token)
|
| 135 |
token_texts.append(cleaned_token)
|
| 136 |
token_scores.append(entity['score'])
|
| 137 |
token_types.append(entity['entity'])
|
| 138 |
print(f"{cleaned_token} ({entity['entity']}): {entity['score']}")
|
| 139 |
|
|
|
|
| 140 |
word_freq = {text: score for text, score in zip(token_texts, token_scores)}
|
| 141 |
|
| 142 |
def color_func(word, font_size, position, orientation, random_state=None, **kwargs):
|
|
|
|
| 145 |
|
| 146 |
wordcloud = WordCloud(width=800, height=400, background_color='#121212', mask=mask_image, color_func=color_func).generate_from_frequencies(word_freq)
|
| 147 |
|
|
|
|
| 148 |
plt.figure(figsize=(10, 5))
|
| 149 |
plt.imshow(wordcloud, interpolation='bilinear')
|
| 150 |
plt.axis('off')
|
| 151 |
plt.tight_layout(pad=0)
|
| 152 |
|
|
|
|
| 153 |
plt_image = plt.gcf()
|
| 154 |
plt_image.canvas.draw()
|
| 155 |
image_array = np.frombuffer(plt_image.canvas.tostring_rgb(), dtype=np.uint8)
|