bluenevus commited on
Commit
18f95e3
·
verified ·
1 Parent(s): 94c0750

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -6,10 +6,15 @@ import io
6
  import re
7
  import os
8
  import requests
 
9
  from pptx import Presentation
10
  from pptx.util import Inches, Pt
11
  from concurrent.futures import ThreadPoolExecutor
12
 
 
 
 
 
13
  # Stability AI API key
14
  STABILITY_API_KEY = os.getenv('STABILITY_API_KEY')
15
 
@@ -102,14 +107,15 @@ def generate_image(prompt, style_preset):
102
  ]
103
  }
104
 
105
- response = requests.post(url, json=body, headers=headers)
106
-
107
- if response.status_code == 200:
108
  data = response.json()
109
  image_data = base64.b64decode(data['artifacts'][0]['base64'])
 
110
  return image_data
111
- else:
112
- print(f"Error: {response.status_code}, {response.text}")
113
  return None
114
 
115
  def enhance_prompt(text):
@@ -168,7 +174,7 @@ def markdown_to_pptx(md_text):
168
  title_box.text_frame.paragraphs[0].font.size = Pt(32)
169
  title_box.text_frame.paragraphs[0].font.bold = True
170
 
171
- # Add content
172
  content_box = current_slide.shapes.add_textbox(Inches(0.5), Inches(1.5), Inches(4.5), Inches(6))
173
  text_frame = content_box.text_frame
174
  for line in lines[1:]:
@@ -183,11 +189,16 @@ def markdown_to_pptx(md_text):
183
  p.font.size = Pt(18)
184
  p.level = 1
185
 
186
- # Add image
187
  image_data = future.result()
188
  if image_data:
189
  image_stream = io.BytesIO(image_data)
190
  current_slide.shapes.add_picture(image_stream, Inches(5.5), Inches(1.5), width=Inches(4), height=Inches(6))
 
 
 
 
 
191
 
192
  output = io.BytesIO()
193
  prs.save(output)
@@ -231,6 +242,7 @@ def convert_markdown(n_clicks, file_contents, markdown_text):
231
  className="mt-3"
232
  )
233
  except Exception as e:
 
234
  return dbc.Alert(f"An error occurred: {str(e)}", color="danger")
235
 
236
  if __name__ == '__main__':
 
6
  import re
7
  import os
8
  import requests
9
+ import logging
10
  from pptx import Presentation
11
  from pptx.util import Inches, Pt
12
  from concurrent.futures import ThreadPoolExecutor
13
 
14
+ # Set up logging
15
+ logging.basicConfig(level=logging.INFO)
16
+ logger = logging.getLogger(__name__)
17
+
18
  # Stability AI API key
19
  STABILITY_API_KEY = os.getenv('STABILITY_API_KEY')
20
 
 
107
  ]
108
  }
109
 
110
+ try:
111
+ response = requests.post(url, json=body, headers=headers)
112
+ response.raise_for_status()
113
  data = response.json()
114
  image_data = base64.b64decode(data['artifacts'][0]['base64'])
115
+ logger.info(f"Image generated successfully for prompt: {prompt[:30]}...")
116
  return image_data
117
+ except requests.exceptions.RequestException as e:
118
+ logger.error(f"Error generating image: {str(e)}")
119
  return None
120
 
121
  def enhance_prompt(text):
 
174
  title_box.text_frame.paragraphs[0].font.size = Pt(32)
175
  title_box.text_frame.paragraphs[0].font.bold = True
176
 
177
+ # Add content (left side)
178
  content_box = current_slide.shapes.add_textbox(Inches(0.5), Inches(1.5), Inches(4.5), Inches(6))
179
  text_frame = content_box.text_frame
180
  for line in lines[1:]:
 
189
  p.font.size = Pt(18)
190
  p.level = 1
191
 
192
+ # Add image (right side)
193
  image_data = future.result()
194
  if image_data:
195
  image_stream = io.BytesIO(image_data)
196
  current_slide.shapes.add_picture(image_stream, Inches(5.5), Inches(1.5), width=Inches(4), height=Inches(6))
197
+ else:
198
+ logger.warning(f"Failed to generate image for slide: {title}")
199
+ # Add a placeholder textbox for the missing image
200
+ placeholder = current_slide.shapes.add_textbox(Inches(5.5), Inches(1.5), Inches(4), Inches(6))
201
+ placeholder.text_frame.text = "Image generation failed"
202
 
203
  output = io.BytesIO()
204
  prs.save(output)
 
242
  className="mt-3"
243
  )
244
  except Exception as e:
245
+ logger.exception("An error occurred during conversion")
246
  return dbc.Alert(f"An error occurred: {str(e)}", color="danger")
247
 
248
  if __name__ == '__main__':