Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,55 +78,157 @@ def generate_storyboard(title, purpose, audience):
|
|
| 78 |
Format your response as a JSON list where each item is a slide with the above elements.
|
| 79 |
|
| 80 |
Start with an engaging title slide and end with a clear call-to-action or summary slide.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
"""
|
| 82 |
|
|
|
|
|
|
|
| 83 |
# Check which client type we have
|
| 84 |
if hasattr(client, "messages"):
|
| 85 |
# Newer API
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
else:
|
| 97 |
# Older API
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# Extract the JSON from the response
|
| 107 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
# Find JSON in the response
|
| 109 |
json_start = response_text.find("[")
|
| 110 |
json_end = response_text.rfind("]") + 1
|
|
|
|
| 111 |
if json_start >= 0 and json_end > 0:
|
| 112 |
json_str = response_text[json_start:json_end]
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
else:
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
-
st.error(f"Error parsing storyboard: {e}")
|
| 118 |
-
st.
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
def generate_slide_content(slide_info, template):
|
| 122 |
"""Generate detailed content for a slide using Claude"""
|
| 123 |
prompt = f"""
|
| 124 |
Create detailed content for a PowerPoint slide with the following information:
|
| 125 |
|
| 126 |
-
Slide Title: {slide_info
|
| 127 |
-
Slide Purpose: {slide_info
|
| 128 |
-
Key Points: {slide_info
|
| 129 |
-
Visual Elements: {slide_info
|
| 130 |
|
| 131 |
The presentation uses the {template} template style.
|
| 132 |
|
|
@@ -136,31 +238,59 @@ def generate_slide_content(slide_info, template):
|
|
| 136 |
3. Notes for the presenter
|
| 137 |
4. Specific visual instructions (charts, images, etc.)
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
Focus on clarity, impact, and alignment with the slide's purpose.
|
| 140 |
"""
|
| 141 |
|
| 142 |
# Check which client type we have
|
| 143 |
if hasattr(client, "messages"):
|
| 144 |
# Newer API
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
else:
|
| 156 |
# Older API
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# Extract the JSON from the response
|
| 166 |
try:
|
|
@@ -171,11 +301,23 @@ def generate_slide_content(slide_info, template):
|
|
| 171 |
json_str = response_text[json_start:json_end]
|
| 172 |
return json.loads(json_str)
|
| 173 |
else:
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
except Exception as e:
|
| 176 |
-
st.error(f"Error parsing slide content: {e}")
|
| 177 |
-
|
| 178 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
def create_ppt(slides_content, template):
|
| 181 |
"""Create a PowerPoint presentation from the slides content"""
|
|
|
|
| 78 |
Format your response as a JSON list where each item is a slide with the above elements.
|
| 79 |
|
| 80 |
Start with an engaging title slide and end with a clear call-to-action or summary slide.
|
| 81 |
+
|
| 82 |
+
The response should be a valid JSON array like this:
|
| 83 |
+
[
|
| 84 |
+
{{
|
| 85 |
+
"title": "Slide Title",
|
| 86 |
+
"purpose": "Purpose of this slide",
|
| 87 |
+
"key_points": ["Key point 1", "Key point 2"],
|
| 88 |
+
"visual_elements": ["Visual element 1", "Visual element 2"]
|
| 89 |
+
}},
|
| 90 |
+
...
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
Make sure your output is valid JSON that can be parsed.
|
| 94 |
"""
|
| 95 |
|
| 96 |
+
st.sidebar.write("Generating storyboard...")
|
| 97 |
+
|
| 98 |
# Check which client type we have
|
| 99 |
if hasattr(client, "messages"):
|
| 100 |
# Newer API
|
| 101 |
+
try:
|
| 102 |
+
response = client.messages.create(
|
| 103 |
+
model="claude-3-5-sonnet-20240620",
|
| 104 |
+
max_tokens=4000,
|
| 105 |
+
temperature=0.7,
|
| 106 |
+
system="You are an expert presentation designer specializing in creating logical, compelling PowerPoint storyboards. Always output valid JSON without explanations, markdown formatting, or extra text.",
|
| 107 |
+
messages=[
|
| 108 |
+
{"role": "user", "content": prompt}
|
| 109 |
+
]
|
| 110 |
+
)
|
| 111 |
+
response_text = response.content[0].text
|
| 112 |
+
st.sidebar.write("Got response from Claude (new API)")
|
| 113 |
+
except Exception as e:
|
| 114 |
+
st.sidebar.error(f"Error with Claude API (new): {str(e)}")
|
| 115 |
+
return []
|
| 116 |
else:
|
| 117 |
# Older API
|
| 118 |
+
try:
|
| 119 |
+
response = client.completion(
|
| 120 |
+
prompt=f"{anthropic.HUMAN_PROMPT} {prompt} {anthropic.AI_PROMPT}",
|
| 121 |
+
model="claude-2.0",
|
| 122 |
+
max_tokens_to_sample=4000,
|
| 123 |
+
temperature=0.7
|
| 124 |
+
)
|
| 125 |
+
response_text = response.completion
|
| 126 |
+
st.sidebar.write("Got response from Claude (old API)")
|
| 127 |
+
except Exception as e:
|
| 128 |
+
st.sidebar.error(f"Error with Claude API (old): {str(e)}")
|
| 129 |
+
return []
|
| 130 |
|
| 131 |
# Extract the JSON from the response
|
| 132 |
try:
|
| 133 |
+
# Display the raw response for debugging
|
| 134 |
+
st.sidebar.write("Parsing JSON...")
|
| 135 |
+
st.sidebar.markdown("**Raw response excerpt:**")
|
| 136 |
+
st.sidebar.markdown(f"```{response_text[:500]}...```")
|
| 137 |
+
|
| 138 |
# Find JSON in the response
|
| 139 |
json_start = response_text.find("[")
|
| 140 |
json_end = response_text.rfind("]") + 1
|
| 141 |
+
|
| 142 |
if json_start >= 0 and json_end > 0:
|
| 143 |
json_str = response_text[json_start:json_end]
|
| 144 |
+
parsed_json = json.loads(json_str)
|
| 145 |
+
st.sidebar.write(f"Found JSON with {len(parsed_json)} slides")
|
| 146 |
+
|
| 147 |
+
# Add placeholder slides if nothing was returned
|
| 148 |
+
if len(parsed_json) == 0:
|
| 149 |
+
st.sidebar.warning("No slides in JSON, adding placeholders")
|
| 150 |
+
parsed_json = [
|
| 151 |
+
{
|
| 152 |
+
"title": title,
|
| 153 |
+
"purpose": "Title slide",
|
| 154 |
+
"key_points": ["Introduction"],
|
| 155 |
+
"visual_elements": ["Company logo"]
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
"title": "Overview",
|
| 159 |
+
"purpose": "Outline key topics",
|
| 160 |
+
"key_points": ["Topic 1", "Topic 2", "Topic 3"],
|
| 161 |
+
"visual_elements": ["Simple bullet list"]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"title": "Thank You",
|
| 165 |
+
"purpose": "Conclusion slide",
|
| 166 |
+
"key_points": ["Contact information", "Next steps"],
|
| 167 |
+
"visual_elements": ["Company logo"]
|
| 168 |
+
}
|
| 169 |
+
]
|
| 170 |
+
|
| 171 |
+
return parsed_json
|
| 172 |
else:
|
| 173 |
+
st.sidebar.error(f"JSON not found in response. Start: {json_start}, End: {json_end}")
|
| 174 |
+
# Create a basic storyboard template instead
|
| 175 |
+
st.sidebar.warning("Creating default storyboard template")
|
| 176 |
+
return [
|
| 177 |
+
{
|
| 178 |
+
"title": title,
|
| 179 |
+
"purpose": "Title slide",
|
| 180 |
+
"key_points": ["Introduction"],
|
| 181 |
+
"visual_elements": ["Company logo"]
|
| 182 |
+
},
|
| 183 |
+
{
|
| 184 |
+
"title": "Overview",
|
| 185 |
+
"purpose": "Outline key topics",
|
| 186 |
+
"key_points": ["Topic 1", "Topic 2", "Topic 3"],
|
| 187 |
+
"visual_elements": ["Simple bullet list"]
|
| 188 |
+
},
|
| 189 |
+
{
|
| 190 |
+
"title": "Thank You",
|
| 191 |
+
"purpose": "Conclusion slide",
|
| 192 |
+
"key_points": ["Contact information", "Next steps"],
|
| 193 |
+
"visual_elements": ["Company logo"]
|
| 194 |
+
}
|
| 195 |
+
]
|
| 196 |
except Exception as e:
|
| 197 |
+
st.sidebar.error(f"Error parsing storyboard: {e}")
|
| 198 |
+
st.sidebar.markdown("**Response text:**")
|
| 199 |
+
st.sidebar.markdown(f"```{response_text[:1000]}...```")
|
| 200 |
+
# Provide a basic storyboard template as fallback
|
| 201 |
+
st.sidebar.warning("Creating default storyboard template")
|
| 202 |
+
return [
|
| 203 |
+
{
|
| 204 |
+
"title": title,
|
| 205 |
+
"purpose": "Title slide",
|
| 206 |
+
"key_points": ["Introduction to " + title],
|
| 207 |
+
"visual_elements": ["Company logo", "Background image"]
|
| 208 |
+
},
|
| 209 |
+
{
|
| 210 |
+
"title": "Overview",
|
| 211 |
+
"purpose": "Outline the presentation contents",
|
| 212 |
+
"key_points": ["Key topics", "Agenda", "Goals"],
|
| 213 |
+
"visual_elements": ["Bullet points", "Timeline graphic"]
|
| 214 |
+
},
|
| 215 |
+
{
|
| 216 |
+
"title": "Conclusion",
|
| 217 |
+
"purpose": "Summarize key points and next steps",
|
| 218 |
+
"key_points": ["Summary", "Call to action", "Contact information"],
|
| 219 |
+
"visual_elements": ["Company logo", "Contact details"]
|
| 220 |
+
}
|
| 221 |
+
]
|
| 222 |
|
| 223 |
def generate_slide_content(slide_info, template):
|
| 224 |
"""Generate detailed content for a slide using Claude"""
|
| 225 |
prompt = f"""
|
| 226 |
Create detailed content for a PowerPoint slide with the following information:
|
| 227 |
|
| 228 |
+
Slide Title: {slide_info.get('title', 'Untitled')}
|
| 229 |
+
Slide Purpose: {slide_info.get('purpose', 'No purpose provided')}
|
| 230 |
+
Key Points: {slide_info.get('key_points', [])}
|
| 231 |
+
Visual Elements: {slide_info.get('visual_elements', [])}
|
| 232 |
|
| 233 |
The presentation uses the {template} template style.
|
| 234 |
|
|
|
|
| 238 |
3. Notes for the presenter
|
| 239 |
4. Specific visual instructions (charts, images, etc.)
|
| 240 |
|
| 241 |
+
Your response must be a valid JSON object like this:
|
| 242 |
+
{{
|
| 243 |
+
"title": "Refined Title",
|
| 244 |
+
"content": ["Point 1", "Point 2", "Point 3"],
|
| 245 |
+
"notes": "Notes for the presenter",
|
| 246 |
+
"visual_instructions": "Details about visuals"
|
| 247 |
+
}}
|
| 248 |
+
|
| 249 |
Focus on clarity, impact, and alignment with the slide's purpose.
|
| 250 |
"""
|
| 251 |
|
| 252 |
# Check which client type we have
|
| 253 |
if hasattr(client, "messages"):
|
| 254 |
# Newer API
|
| 255 |
+
try:
|
| 256 |
+
response = client.messages.create(
|
| 257 |
+
model="claude-3-5-sonnet-20240620",
|
| 258 |
+
max_tokens=2000,
|
| 259 |
+
temperature=0.5,
|
| 260 |
+
system="You are an expert presentation content creator. Always output valid JSON without explanations or extra text.",
|
| 261 |
+
messages=[
|
| 262 |
+
{"role": "user", "content": prompt}
|
| 263 |
+
]
|
| 264 |
+
)
|
| 265 |
+
response_text = response.content[0].text
|
| 266 |
+
except Exception as e:
|
| 267 |
+
st.sidebar.error(f"Error with Claude API: {str(e)}")
|
| 268 |
+
# Return a default structure
|
| 269 |
+
return {
|
| 270 |
+
"title": slide_info.get('title', 'Untitled'),
|
| 271 |
+
"content": ["Content to be added"],
|
| 272 |
+
"notes": "Add presenter notes here",
|
| 273 |
+
"visual_instructions": "Add visual elements here"
|
| 274 |
+
}
|
| 275 |
else:
|
| 276 |
# Older API
|
| 277 |
+
try:
|
| 278 |
+
response = client.completion(
|
| 279 |
+
prompt=f"{anthropic.HUMAN_PROMPT} {prompt} {anthropic.AI_PROMPT}",
|
| 280 |
+
model="claude-2.0",
|
| 281 |
+
max_tokens_to_sample=2000,
|
| 282 |
+
temperature=0.5
|
| 283 |
+
)
|
| 284 |
+
response_text = response.completion
|
| 285 |
+
except Exception as e:
|
| 286 |
+
st.sidebar.error(f"Error with Claude API: {str(e)}")
|
| 287 |
+
# Return a default structure
|
| 288 |
+
return {
|
| 289 |
+
"title": slide_info.get('title', 'Untitled'),
|
| 290 |
+
"content": ["Content to be added"],
|
| 291 |
+
"notes": "Add presenter notes here",
|
| 292 |
+
"visual_instructions": "Add visual elements here"
|
| 293 |
+
}
|
| 294 |
|
| 295 |
# Extract the JSON from the response
|
| 296 |
try:
|
|
|
|
| 301 |
json_str = response_text[json_start:json_end]
|
| 302 |
return json.loads(json_str)
|
| 303 |
else:
|
| 304 |
+
st.sidebar.warning(f"JSON not found in slide content response for '{slide_info.get('title', 'Untitled')}'")
|
| 305 |
+
# Return a default structure based on slide_info
|
| 306 |
+
return {
|
| 307 |
+
"title": slide_info.get('title', 'Untitled'),
|
| 308 |
+
"content": slide_info.get('key_points', ["Content to be added"]),
|
| 309 |
+
"notes": f"This slide covers: {slide_info.get('purpose', 'No purpose specified')}",
|
| 310 |
+
"visual_instructions": str(slide_info.get('visual_elements', "No visual elements specified"))
|
| 311 |
+
}
|
| 312 |
except Exception as e:
|
| 313 |
+
st.sidebar.error(f"Error parsing slide content: {str(e)}")
|
| 314 |
+
# Return a default structure
|
| 315 |
+
return {
|
| 316 |
+
"title": slide_info.get('title', 'Untitled'),
|
| 317 |
+
"content": ["Content to be added"],
|
| 318 |
+
"notes": "Add presenter notes here",
|
| 319 |
+
"visual_instructions": "Add visual elements here"
|
| 320 |
+
}
|
| 321 |
|
| 322 |
def create_ppt(slides_content, template):
|
| 323 |
"""Create a PowerPoint presentation from the slides content"""
|