Guiyom commited on
Commit
a24e2f4
·
verified ·
1 Parent(s): 461ecca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -35
app.py CHANGED
@@ -31,9 +31,10 @@ MAX_MESSAGE_LENGTH = 1048576
31
 
32
  def generate_visual_snippet(placeholder_text: str, context: str, initial_query: str, crumbs: str) -> str:
33
  prompt = (f"""
34
- Generate a complete, self-contained HTML code snippet that includes inline CSS and JavaScript.
35
  The code should display a simple but effective and elegant visualization based on the following requirements:
36
  {placeholder_text}
 
37
  It will be integrated in a broader report (in html) about:
38
  {initial_query}
39
 
@@ -44,20 +45,54 @@ Keep in mind the:
44
  - the knowledge inputs
45
  {crumbs}
46
 
47
- // Critical Requirements
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  - There can be 2 types of visuals:
49
  o Simple management or ideation diagrams that can be generated in html directly (ex: ganntt chart or five forces, ... )
50
- o Complex diagrams requiring a library
51
- Note:
52
- For advanced ones, use ONLY SVG elements or Plotly.js for compatibility with PDF rendering.
53
- No external dependencies except CDN-hosted Plotly/D3
54
- Add 'xmlns="http://www.w3.org/2000/svg"' attribute to SVG tags
55
- - White background for all elements (#ffffff)
56
  - Include explicit width/height attributes in SVG tags (500px)
57
  - Font size minimum 12px for readability in PDF
58
- - Include all required <script> tags for libraries
59
  - Use high-contrast colors (no dark backgrounds)
60
- - Include descriptive <title> elements for accessibility
61
  - no introduction, conclusions, code fences -> Output the result directly
62
 
63
  // Important
@@ -65,20 +100,92 @@ Note:
65
  - It has to convey some relevant insights.
66
  - Take a deep breath, think step by step and think it well.
67
 
68
- // Preferred Patterns
69
- <svg width="500" height="500" xmlns="http://www.w3.org/2000/svg">
70
- <!-- SVG elements here -->
71
- </svg>
72
-
73
- OR
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- <div id="chart">
76
- <!-- Plotly chart -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  </div>
78
- <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
79
 
80
- // Example of complex visual:
81
- --- Example 1 ---
82
  <!-- Visual Placeholder 2 -->
83
  <div class=\"visual-placeholder\">
84
  <!-- Visual 2 Start -->
@@ -246,7 +353,7 @@ Keep in mind the:
246
 
247
  // Critical Requirements
248
  - Use html with css to render it, keep it sober and focused on the content
249
- - The output has to be boxed in a border of 1px Black and grey background (#b5b3b3)
250
  - Font size minimum 12px for readability in PDF
251
  - You have freedom on the structure, but it has to cover all potential aspects on the topic in between 500 and 1000 words
252
 
@@ -524,6 +631,8 @@ The report should be very detailed and lengthy — approximately the equivalent
524
  - Visual guidance:...
525
  ]]
526
  with n as the reference number
 
 
527
  - do not make reference in the report to "visual placeholders" just mention visuals.
528
  - in the placeholder, no need to add the references to the source, but make sure ALL of the data points required has a source from the learning and reference material hereafter
529
  - these placeholders text should contain:
@@ -536,17 +645,13 @@ Note: the placeholders will then be processed separtely by a llm to generate the
536
 
537
  // Focus placeholders
538
  - To drill down on specific topic that would be deserve to be developped extensively separately, create special focus placeholders in [[...]] double backets
539
- - Use this format:
540
-
541
- [[Focus Placeholder n:
542
- - Topic of this focus:...
543
- - Relevant data to generate it:...
544
- - Writing style guidance:...
545
- ]]
546
-
547
- with n as the reference number
548
  Note: outside of the placeholder, do not make reference in the report to "focus placeholders" just mention the "Focus box n".
549
- - in the placeholder, make a mention to the prescribed sources used
 
 
 
 
 
550
  - these placeholders text should contain:
551
  o the purpose of the focus box
552
  o the relevant data to generate it
@@ -555,6 +660,16 @@ Note: outside of the placeholder, do not make reference in the report to "focus
555
  - there should be at least {round(pages/6,0)} of these focus placeholders within the report, not all in the same section except if this is relevant for illustration.
556
  Note: the placeholders will then be processed separtely by a llm to generate the specific code to display each of them so the instruction need to be clear enough.
557
 
 
 
 
 
 
 
 
 
 
 
558
  // Structure of the overall report:
559
  - Abstract
560
  - Table of contents
@@ -876,9 +991,9 @@ class ReportGenerator:
876
  <meta charset="utf-8" />
877
  <style>
878
  body {{ font-family: Helvetica, sans-serif; margin: 40px; }}
879
- h1 {{ font-size: 24pt; margin-bottom: 12px; }}
880
- h2 {{ font-size: 20pt; margin-bottom: 10px; }}
881
- h3 {{ font-size: 18pt; margin-bottom: 8px; }}
882
  p {{ font-size: 11pt; line-height: 1.5; margin-bottom: 10px; }}
883
  ol, ul {{ font-size: 11pt; margin-left: 20px; line-height: 1.5; }}
884
  hr {{ border: 1px solid #ccc; margin: 20px 0; }}
 
31
 
32
  def generate_visual_snippet(placeholder_text: str, context: str, initial_query: str, crumbs: str) -> str:
33
  prompt = (f"""
34
+ Generate a complete, self-contained HTML code snippet between <div> tags that includes inline CSS and JavaScript.
35
  The code should display a simple but effective and elegant visualization based on the following requirements:
36
  {placeholder_text}
37
+
38
  It will be integrated in a broader report (in html) about:
39
  {initial_query}
40
 
 
45
  - the knowledge inputs
46
  {crumbs}
47
 
48
+ // Requirements
49
+ - use simple svg rendering
50
+ - White background (#ffffff)
51
+ - overall dimension capped at 500px x 500px
52
+ - Font size minimum 12px for readability in PDF
53
+ - Use high-contrast colors (no dark backgrounds)
54
+ - use a title on the top <h3> aligned on the left
55
+ - no introduction, conclusions or code fences -> Output the result directly
56
+
57
+ // Important
58
+ - Make the visuals content rich, there's no point having a visual if its content is pointless.
59
+ - It has to convey some relevant insights.
60
+ - Take a deep breath, think step by step and think it well.
61
+ """)
62
+
63
+ result = openai_call(prompt, model="o3-mini", max_tokens_param=10000)
64
+ result = result.strip().strip("```").strip()
65
+ logging.info(f"The code produced for this visual placeholder:\n{placeholder_text}\n\n {result}\n\n")
66
+ return result
67
+
68
+ def generate_visual_snippet_tofix(placeholder_text: str, context: str, initial_query: str, crumbs: str) -> str:
69
+ prompt = (f"""
70
+ Generate a complete, self-contained HTML code snippet between <div> tags that includes inline CSS and JavaScript.
71
+ The code should display a simple but effective and elegant visualization based on the following requirements:
72
+ {placeholder_text}
73
+ It will be integrated in a broader report (in html) about:
74
+ {initial_query}
75
+
76
+ // Sources:
77
+ Keep in mind the:
78
+ - context:
79
+ {context}
80
+ - the knowledge inputs
81
+ {crumbs}
82
+
83
+ // Different types of visuals
84
  - There can be 2 types of visuals:
85
  o Simple management or ideation diagrams that can be generated in html directly (ex: ganntt chart or five forces, ... )
86
+ o Complex diagrams requiring a library (ex: )
87
+ Note: For advanced ones, use only the example below
88
+
89
+ // Requirements
90
+ - White background (#ffffff)
91
+ - overall dimension capped at 500px x 500px
92
  - Include explicit width/height attributes in SVG tags (500px)
93
  - Font size minimum 12px for readability in PDF
 
94
  - Use high-contrast colors (no dark backgrounds)
95
+ - use a title on the top <h3> aligned on the left
96
  - no introduction, conclusions, code fences -> Output the result directly
97
 
98
  // Important
 
100
  - It has to convey some relevant insights.
101
  - Take a deep breath, think step by step and think it well.
102
 
103
+ // Example of simple visual (This is a reference - use a similar structure for any other visual):
104
+ <div class="five-forces-container">
105
+ <svg width="500" height="500" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
106
+ <style>
107
+ .force-text {{ font: 12px Arial; fill: #333; }}
108
+ .force-value {{ font: bold 14px Arial; fill: #2c5282; }}
109
+ .connector {{ stroke: #666; stroke-width: 1; }}
110
+ .force-circle {{
111
+ fill: #ebf8ff;
112
+ stroke: #2c5282;
113
+ stroke-width: 2;
114
+ cursor: pointer;
115
+ transition: all 0.3s;
116
+ }}
117
+ .force-circle:hover {{ fill: #bee3f8; }}
118
+ </style>
119
+
120
+ <!-- Central Title -->
121
+ <text x="250" y="250" text-anchor="middle" font-size="16" font-weight="bold">Ice Cream Maker Industry</text>
122
+
123
+ <!-- Five Forces Elements -->
124
+ <g transform="rotate(0,250,250)">
125
+ <circle class="force-circle" cx="250" cy="100" r="40" data-force="rivalry"/>
126
+ <text class="force-text" x="250" y="90" text-anchor="middle">Competitive Rivalry</text>
127
+ <text class="force-value" x="250" y="110" text-anchor="middle">72%</text>
128
+ <line class="connector" x1="250" y1="140" x2="250" y2="200"/>
129
+ </g>
130
+
131
+ <g transform="rotate(72,250,250)">
132
+ <circle class="force-circle" cx="250" cy="100" r="40" data-force="suppliers"/>
133
+ <text class="force-text" x="250" y="90" text-anchor="middle">Supplier Power</text>
134
+ <text class="force-value" x="250" y="110" text-anchor="middle">65%</text>
135
+ <line class="connector" x1="250" y1="140" x2="250" y2="200"/>
136
+ </g>
137
+
138
+ <g transform="rotate(144,250,250)">
139
+ <circle class="force-circle" cx="250" cy="100" r="40" data-force="buyers"/>
140
+ <text class="force-text" x="250" y="90" text-anchor="middle">Buyer Power</text>
141
+ <text class="force-value" x="250" y="110" text-anchor="middle">68%</text>
142
+ <line class="connector" x1="250" y1="140" x2="250" y2="200"/>
143
+ </g>
144
+
145
+ <g transform="rotate(216,250,250)">
146
+ <circle class="force-circle" cx="250" cy="100" r="40" data-force="substitutes"/>
147
+ <text class="force-text" x="250" y="90" text-anchor="middle">Substitutes</text>
148
+ <text class="force-value" x="250" y="110" text-anchor="middle">85%</text>
149
+ <line class="connector" x1="250" y1="140" x2="250" y2="200"/>
150
+ </g>
151
+
152
+ <g transform="rotate(288,250,250)">
153
+ <circle class="force-circle" cx="250" cy="100" r="40" data-force="new_entrants"/>
154
+ <text class="force-text" x="250" y="90" text-anchor="middle">New Entrants</text>
155
+ <text class="force-value" x="250" y="110" text-anchor="middle">58%</text>
156
+ <line class="connector" x1="250" y1="140" x2="250" y2="200"/>
157
+ </g>
158
+
159
+ <!-- Interactive Tooltip -->
160
+ <foreignObject x="0" y="400" width="500" height="100">
161
+ <div xmlns="http://www.w3.org/1999/xhtml" class="tooltip"
162
+ style="text-align:center; font-size:12px; color:#666; padding:10px">
163
+ Hover over any force for details
164
+ </div>
165
+ </foreignObject>
166
+ </svg>
167
 
168
+ <script>
169
+ const forceData = {{
170
+ rivalry: "High competition from 10+ major brands. Price wars common (-15% margin impact)",
171
+ suppliers: "Limited dairy suppliers (3 major players control 80% market)",
172
+ buyers: "Consumers show high price sensitivity (68% would switch brands for 10% discount)",
173
+ substitutes: "Strong alternatives: frozen yogurt (+25% growth), plant-based options",
174
+ new_entrants: "Moderate barriers: $2M+ minimum investment, established distribution needed"
175
+ }};
176
+
177
+ document.querySelectorAll('.force-circle').forEach(circle => {{
178
+ circle.addEventListener('mouseover', function() {{
179
+ const force = this.dataset.force;
180
+ document.querySelector('.tooltip').innerHTML =
181
+ `<strong>${{this.parentNode.querySelector('text').textContent}}</strong><br>
182
+ ${{forceData[force]}}`;
183
+ }});
184
+ }});
185
+ </script>
186
  </div>
 
187
 
188
+ // Example of complex visual (This is a reference - use a similar structure for any other visual):
 
189
  <!-- Visual Placeholder 2 -->
190
  <div class=\"visual-placeholder\">
191
  <!-- Visual 2 Start -->
 
353
 
354
  // Critical Requirements
355
  - Use html with css to render it, keep it sober and focused on the content
356
+ - The output has to be boxed in a border of 1px Black and grey background (#ede8e8)
357
  - Font size minimum 12px for readability in PDF
358
  - You have freedom on the structure, but it has to cover all potential aspects on the topic in between 500 and 1000 words
359
 
 
631
  - Visual guidance:...
632
  ]]
633
  with n as the reference number
634
+ Important: after [[ put "Visual Placeholder n:" explicitly (with n as the ref number of the focus box created). This will be used in a regex
635
+
636
  - do not make reference in the report to "visual placeholders" just mention visuals.
637
  - in the placeholder, no need to add the references to the source, but make sure ALL of the data points required has a source from the learning and reference material hereafter
638
  - these placeholders text should contain:
 
645
 
646
  // Focus placeholders
647
  - To drill down on specific topic that would be deserve to be developped extensively separately, create special focus placeholders in [[...]] double backets
 
 
 
 
 
 
 
 
 
648
  Note: outside of the placeholder, do not make reference in the report to "focus placeholders" just mention the "Focus box n".
649
+ - in the placeholder, make a mention to the prescribed sources used
650
+ - do not make the placeholder as the exact same topic as the section or the sub-section where it is positioned, it has to be either:
651
+ o a special case that deserves attention
652
+ o a recent development / innovation
653
+ o a theoretical drill-down
654
+ o a contrarian point of view / objection
655
  - these placeholders text should contain:
656
  o the purpose of the focus box
657
  o the relevant data to generate it
 
660
  - there should be at least {round(pages/6,0)} of these focus placeholders within the report, not all in the same section except if this is relevant for illustration.
661
  Note: the placeholders will then be processed separtely by a llm to generate the specific code to display each of them so the instruction need to be clear enough.
662
 
663
+ // Format:
664
+ [[Focus Placeholder n:
665
+ - Topic of this focus:...
666
+ - Relevant data to generate it:...
667
+ - Writing style guidance:...
668
+ ]]
669
+
670
+ with n as the reference number
671
+ Important: after [[ put "Focus Placeholder n:" explicitly (with n as the ref number of the focus box created). This will be used in a regex
672
+
673
  // Structure of the overall report:
674
  - Abstract
675
  - Table of contents
 
991
  <meta charset="utf-8" />
992
  <style>
993
  body {{ font-family: Helvetica, sans-serif; margin: 40px; }}
994
+ h1 {{ font-size: 24pt; margin-bottom: 12px; text-align: left; }}
995
+ h2 {{ font-size: 20pt; margin-bottom: 10px; text-align: left; }}
996
+ h3 {{ font-size: 18pt; margin-bottom: 8px; text-align: left; }}
997
  p {{ font-size: 11pt; line-height: 1.5; margin-bottom: 10px; }}
998
  ol, ul {{ font-size: 11pt; margin-left: 20px; line-height: 1.5; }}
999
  hr {{ border: 1px solid #ccc; margin: 20px 0; }}