fraudTech commited on
Commit
f9dc30a
·
1 Parent(s): 6dbbcff

fix plot display

Browse files
Files changed (2) hide show
  1. web/index.html +1 -1
  2. web/src/App.tsx +67 -8
web/index.html CHANGED
@@ -5,7 +5,7 @@
5
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
  <!-- Allow data URIs for images (matplotlib plots) -->
8
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src 'self' data: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; connect-src 'self' https://deepthoughtai-evo-gateway.hf.space https://deepthoughtai-evo-backend.hf.space;">
9
  <link rel="preconnect" href="https://fonts.googleapis.com">
10
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
11
  <link href="https://fonts.googleapis.com/css2?family=Michroma&display=swap" rel="stylesheet">
 
5
  <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
  <!-- Allow data URIs for images (matplotlib plots) -->
8
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src 'self' data: blob: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; connect-src 'self' https://deepthoughtai-evo-gateway.hf.space https://deepthoughtai-evo-backend.hf.space;">
9
  <link rel="preconnect" href="https://fonts.googleapis.com">
10
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
11
  <link href="https://fonts.googleapis.com/css2?family=Michroma&display=swap" rel="stylesheet">
web/src/App.tsx CHANGED
@@ -73,6 +73,68 @@ const allowDataImageUrl = (url: string): string => {
73
  return defaultUrlTransform(url);
74
  };
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  interface Message {
77
  id: string;
78
  role: 'user' | 'assistant' | 'system';
@@ -632,6 +694,7 @@ function App() {
632
  remarkGfm
633
  ]}
634
  rehypePlugins={[rehypeKatex]}
 
635
  components={{
636
  code({ className, children, ...props }: any) {
637
  const match = /language-(\w+)/.exec(className || '');
@@ -650,6 +713,9 @@ function App() {
650
  </code>
651
  );
652
  },
 
 
 
653
  }}
654
  >
655
  {preprocessMathContent(message.content)}
@@ -723,14 +789,7 @@ function App() {
723
  },
724
  // Ensure images are rendered properly
725
  img({ src, alt, ...props }: any) {
726
- return (
727
- <img
728
- src={src}
729
- alt={alt || 'Plot'}
730
- style={{ maxWidth: '100%', height: 'auto', margin: '1rem 0' }}
731
- {...props}
732
- />
733
- );
734
  },
735
  }}
736
  >
 
73
  return defaultUrlTransform(url);
74
  };
75
 
76
+ const PlotImage: React.FC<{ src?: string; alt?: string }> = ({ src, alt, ...props }) => {
77
+ const [currentSrc, setCurrentSrc] = useState<string | undefined>(src);
78
+ const [fallbackSrc, setFallbackSrc] = useState<string | undefined>(undefined);
79
+
80
+ useEffect(() => {
81
+ if (!src) {
82
+ setCurrentSrc(undefined);
83
+ setFallbackSrc(undefined);
84
+ return;
85
+ }
86
+
87
+ if (src.startsWith('data:image/')) {
88
+ try {
89
+ const [meta, data] = src.split(',', 2);
90
+ const mimeMatch = meta.match(/^data:(image\/[^;]+);base64$/);
91
+
92
+ if (mimeMatch && data) {
93
+ const byteChars = atob(data);
94
+ const byteNumbers = new Array(byteChars.length);
95
+ for (let i = 0; i < byteChars.length; i += 1) {
96
+ byteNumbers[i] = byteChars.charCodeAt(i);
97
+ }
98
+
99
+ const blob = new Blob([new Uint8Array(byteNumbers)], { type: mimeMatch[1] });
100
+ const objectUrl = URL.createObjectURL(blob);
101
+
102
+ setCurrentSrc(objectUrl);
103
+ setFallbackSrc(src);
104
+
105
+ return () => {
106
+ URL.revokeObjectURL(objectUrl);
107
+ };
108
+ }
109
+ } catch {
110
+ setCurrentSrc(src);
111
+ setFallbackSrc(undefined);
112
+ }
113
+ }
114
+
115
+ setCurrentSrc(src);
116
+ setFallbackSrc(undefined);
117
+ }, [src]);
118
+
119
+ if (!currentSrc) {
120
+ return null;
121
+ }
122
+
123
+ return (
124
+ <img
125
+ src={currentSrc}
126
+ alt={alt || 'Plot'}
127
+ style={{ maxWidth: '100%', height: 'auto', margin: '1rem 0' }}
128
+ onError={() => {
129
+ if (fallbackSrc && fallbackSrc !== currentSrc) {
130
+ setCurrentSrc(fallbackSrc);
131
+ }
132
+ }}
133
+ {...props}
134
+ />
135
+ );
136
+ };
137
+
138
  interface Message {
139
  id: string;
140
  role: 'user' | 'assistant' | 'system';
 
694
  remarkGfm
695
  ]}
696
  rehypePlugins={[rehypeKatex]}
697
+ urlTransform={allowDataImageUrl}
698
  components={{
699
  code({ className, children, ...props }: any) {
700
  const match = /language-(\w+)/.exec(className || '');
 
713
  </code>
714
  );
715
  },
716
+ img({ src, alt, ...props }: any) {
717
+ return <PlotImage src={src} alt={alt} {...props} />;
718
+ },
719
  }}
720
  >
721
  {preprocessMathContent(message.content)}
 
789
  },
790
  // Ensure images are rendered properly
791
  img({ src, alt, ...props }: any) {
792
+ return <PlotImage src={src} alt={alt} {...props} />;
 
 
 
 
 
 
 
793
  },
794
  }}
795
  >