return early if there are no images to replace

This commit is contained in:
Abi Raja 2023-11-16 15:11:54 -05:00
parent 640f41619c
commit 68dd33cf06
3 changed files with 76 additions and 4 deletions

View File

@ -82,6 +82,10 @@ async def generate_images(code, image_cache):
# Remove duplicates # Remove duplicates
prompts = list(set(alts)) prompts = list(set(alts))
# Return early if there are no images to replace
if len(prompts) == 0:
return code
# Generate images # Generate images
results = await process_tasks(prompts) results = await process_tasks(prompts)

View File

@ -11,7 +11,7 @@ from datetime import datetime
from fastapi import FastAPI, WebSocket from fastapi import FastAPI, WebSocket
from llm import stream_openai_response from llm import stream_openai_response
from mock import MOCK_HTML, mock_completion from mock import mock_completion
from image_generation import create_alt_url_mapping, generate_images from image_generation import create_alt_url_mapping, generate_images
from prompts import assemble_prompt from prompts import assemble_prompt
@ -41,6 +41,7 @@ async def stream_code_test(websocket: WebSocket):
params = await websocket.receive_json() params = await websocket.receive_json()
print("generating code...")
await websocket.send_json({"type": "status", "value": "Generating code..."}) await websocket.send_json({"type": "status", "value": "Generating code..."})
async def process_chunk(content): async def process_chunk(content):

View File

@ -2,7 +2,7 @@ import asyncio
async def mock_completion(process_chunk): async def mock_completion(process_chunk):
code_to_return = MOCK_HTML_2 code_to_return = NO_IMAGES_NYTIMES_MOCK_CODE
for i in range(0, len(code_to_return), 10): for i in range(0, len(code_to_return), 10):
await process_chunk(code_to_return[i : i + 10]) await process_chunk(code_to_return[i : i + 10])
@ -11,7 +11,7 @@ async def mock_completion(process_chunk):
return code_to_return return code_to_return
MOCK_HTML = """<html lang="en"> APPLE_MOCK_CODE = """<html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
@ -68,7 +68,7 @@ MOCK_HTML = """<html lang="en">
</body> </body>
</html>""" </html>"""
MOCK_HTML_2 = """ NYTIMES_MOCK_CODE = """
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
@ -138,3 +138,70 @@ MOCK_HTML_2 = """
</body> </body>
</html> </html>
""" """
NO_IMAGES_NYTIMES_MOCK_CODE = """
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The New York Times - News</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@300;400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<style>
body {
font-family: 'Libre Franklin', sans-serif;
}
</style>
</head>
<body class="bg-gray-100">
<div class="container mx-auto px-4">
<header class="border-b border-gray-300 py-4">
<div class="flex justify-between items-center">
<div class="flex items-center space-x-4">
<button class="text-gray-700"><i class="fas fa-bars"></i></button>
<button class="text-gray-700"><i class="fas fa-search"></i></button>
<div class="text-xs uppercase tracking-widest">Tuesday, November 14, 2023<br>Today's Paper</div>
</div>
<div class="flex items-center space-x-4">
<button class="bg-black text-white px-4 py-1 text-xs uppercase tracking-widest">Give the times</button>
<div class="text-xs">Account</div>
</div>
</div>
<nav class="flex justify-between items-center py-4">
<div class="flex space-x-4">
<a href="#" class="text-xs uppercase tracking-widest text-gray-700">U.S.</a>
<!-- Add other navigation links as needed -->
</div>
<div class="flex space-x-4">
<a href="#" class="text-xs uppercase tracking-widest text-gray-700">Cooking</a>
<!-- Add other navigation links as needed -->
</div>
</nav>
</header>
<main>
<section class="py-6">
<div class="grid grid-cols-3 gap-4">
<div class="col-span-2">
<article class="mb-4">
<h2 class="text-xl font-bold mb-2">Israeli Military Raids Gazas Largest Hospital</h2>
<p class="text-gray-700 mb-2">Israeli troops have entered the Al-Shifa Hospital complex, where conditions have grown dire and Israel says Hamas fighters are embedded.</p>
<a href="#" class="text-blue-600 text-sm">See more updates <i class="fas fa-external-link-alt"></i></a>
</article>
<!-- Repeat for each news item -->
</div>
<div class="col-span-1">
<article class="mb-4">
<h2 class="text-xl font-bold mb-2">From Elvis to Elopements, the Evolution of the Las Vegas Wedding</h2>
<p class="text-gray-700 mb-2">The glittering city that attracts thousands of couples seeking unconventional nuptials has grown beyond the drive-through wedding.</p>
<a href="#" class="text-blue-600 text-sm">8 MIN READ</a>
</article>
<!-- Repeat for each news item -->
</div>
</div>
</section>
</main>
</div>
</body>
</html>
"""