Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -132,7 +132,24 @@ async def mainFunction(html_content:str):
|
|
| 132 |
# Optimize the image to be less than 200KB
|
| 133 |
await optimize_image(captured_image_path, output_image_file)
|
| 134 |
|
|
|
|
|
|
|
| 135 |
# Run the asynchronous main function
|
| 136 |
@app.post("/convert-html-to-image/")
|
| 137 |
async def convert_html_to_image_endpoint(html_content:HtmlInput):
|
| 138 |
-
await mainFunction(html_content.message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
# Optimize the image to be less than 200KB
|
| 133 |
await optimize_image(captured_image_path, output_image_file)
|
| 134 |
|
| 135 |
+
return output_image_file
|
| 136 |
+
|
| 137 |
# Run the asynchronous main function
|
| 138 |
@app.post("/convert-html-to-image/")
|
| 139 |
async def convert_html_to_image_endpoint(html_content:HtmlInput):
|
| 140 |
+
image_path = await mainFunction(html_content.message)
|
| 141 |
+
|
| 142 |
+
# Check if image was generated
|
| 143 |
+
if not image_path or not os.path.exists(image_path):
|
| 144 |
+
raise HTTPException(status_code=500, detail="Image generation failed.")
|
| 145 |
+
|
| 146 |
+
# Open the image file for streaming
|
| 147 |
+
with open(image_path, "rb") as img_file:
|
| 148 |
+
buffer = io.BytesIO(img_file.read())
|
| 149 |
+
|
| 150 |
+
buffer.seek(0)
|
| 151 |
+
|
| 152 |
+
# Return the image as a downloadable file
|
| 153 |
+
return StreamingResponse(buffer, media_type="image/jpeg", headers={
|
| 154 |
+
"Content-Disposition": f"attachment; filename={os.path.basename(image_path)}"
|
| 155 |
+
})
|