VirtualOasis commited on
Commit
a5510cb
·
verified ·
1 Parent(s): 3283abf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -7
app.py CHANGED
@@ -14,9 +14,11 @@ from urllib.parse import urlparse
14
  import warnings
15
 
16
  # Configure matplotlib for better font handling
17
- plt.rcParams['font.family'] = ['DejaVu Sans', 'Arial', 'Liberation Sans']
18
  plt.rcParams['font.size'] = 10
 
19
  warnings.filterwarnings('ignore', category=UserWarning)
 
20
 
21
  def clean_text_for_display(text):
22
  """Clean text to remove characters that might cause font issues."""
@@ -263,8 +265,28 @@ def build_knowledge_graph(entities_data):
263
 
264
  # Convert to PIL Image
265
  fig.canvas.draw()
266
- img_array = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
267
- img_array = img_array.reshape(fig.canvas.get_width_height()[::-1] + (3,))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
  from PIL import Image
270
  pil_image = Image.fromarray(img_array)
@@ -280,9 +302,28 @@ def build_knowledge_graph(entities_data):
280
  ax.set_title("Knowledge Graph Error")
281
  ax.axis('off')
282
 
283
- fig.canvas.draw()
284
- img_array = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
285
- img_array = img_array.reshape(fig.canvas.get_width_height()[::-1] + (3,))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
  from PIL import Image
288
  pil_image = Image.fromarray(img_array)
@@ -421,4 +462,4 @@ if __name__ == "__main__":
421
  show_error=True
422
  )
423
  except Exception as e2:
424
- print(f"Complete failure: {e2}")
 
14
  import warnings
15
 
16
  # Configure matplotlib for better font handling
17
+ plt.rcParams['font.family'] = ['DejaVu Sans']
18
  plt.rcParams['font.size'] = 10
19
+ plt.rcParams['font.weight'] = 'normal'
20
  warnings.filterwarnings('ignore', category=UserWarning)
21
+ warnings.filterwarnings('ignore', message='.*Font family.*not found.*')
22
 
23
  def clean_text_for_display(text):
24
  """Clean text to remove characters that might cause font issues."""
 
265
 
266
  # Convert to PIL Image
267
  fig.canvas.draw()
268
+
269
+ # Handle different matplotlib versions
270
+ try:
271
+ # Try newer method first
272
+ img_array = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8)
273
+ img_array = img_array.reshape(fig.canvas.get_width_height()[::-1] + (4,))
274
+ # Convert RGBA to RGB
275
+ img_array = img_array[:, :, :3]
276
+ except AttributeError:
277
+ try:
278
+ # Fallback to older method
279
+ img_array = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
280
+ img_array = img_array.reshape(fig.canvas.get_width_height()[::-1] + (3,))
281
+ except AttributeError:
282
+ # Final fallback - save to buffer
283
+ buf = io.BytesIO()
284
+ fig.savefig(buf, format='png', bbox_inches='tight')
285
+ buf.seek(0)
286
+ from PIL import Image
287
+ pil_image = Image.open(buf).convert('RGB')
288
+ plt.close(fig)
289
+ return pil_image
290
 
291
  from PIL import Image
292
  pil_image = Image.fromarray(img_array)
 
302
  ax.set_title("Knowledge Graph Error")
303
  ax.axis('off')
304
 
305
+ # Handle different matplotlib versions for error image
306
+ try:
307
+ # Try newer method first
308
+ fig.canvas.draw()
309
+ img_array = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8)
310
+ img_array = img_array.reshape(fig.canvas.get_width_height()[::-1] + (4,))
311
+ img_array = img_array[:, :, :3] # Convert RGBA to RGB
312
+ except AttributeError:
313
+ try:
314
+ # Fallback to older method
315
+ fig.canvas.draw()
316
+ img_array = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
317
+ img_array = img_array.reshape(fig.canvas.get_width_height()[::-1] + (3,))
318
+ except AttributeError:
319
+ # Final fallback - save to buffer
320
+ buf = io.BytesIO()
321
+ fig.savefig(buf, format='png', bbox_inches='tight')
322
+ buf.seek(0)
323
+ from PIL import Image
324
+ pil_image = Image.open(buf).convert('RGB')
325
+ plt.close(fig)
326
+ return pil_image
327
 
328
  from PIL import Image
329
  pil_image = Image.fromarray(img_array)
 
462
  show_error=True
463
  )
464
  except Exception as e2:
465
+ print(f"Complete failure: {e2}")