diff --git a/servers/fastapi/api/v1/ppt/endpoints/prompts.py b/servers/fastapi/api/v1/ppt/endpoints/prompts.py index 399d6b3a..2fd20740 100644 --- a/servers/fastapi/api/v1/ppt/endpoints/prompts.py +++ b/servers/fastapi/api/v1/ppt/endpoints/prompts.py @@ -47,7 +47,9 @@ Convert given static HTML and Tailwind slide to a TSX React component so that it -Layout Name examples: HeaderDescriptionBulletPointsLayout, HeaderDescriptionImageLayout -Layout Description examples: A slide with a header, description, and bullet points and A slide with a header, description, and image 12. Only give Code and nothing else. No other text or comments. -13. Do not parse the slideData inside dynamicSlideLayout, just use it as it is. +13. Do not parse the slideData inside dynamicSlideLayout, just use it as it is. Do not use statements like `Schema.parse() ` anywhere. Instead directly use the data without validating or parsing. +14. Always complete the reference, do not give "slideData .? .cards" instead give "slideData?.cards". +15. Do not add anything other than code. Do not add "use client", "json", "typescript", "javascript" and other prefix or suffix, just give out code exactly formatted like example. For example: Input:

Effects of Global Warming

global warming effects on earth

Global warming triggers a cascade of effects on our planet. These changes impact everything from our oceans to our ecosystems.

sea level rising icon

Rising Sea Levels

Rising sea levels threaten coastal communities and ecosystems due to melting glaciers and thermal expansion.

heatwave icon

Intense Heatwaves

Heatwaves are becoming more frequent and intense, posing significant risks to human health and agriculture.

precipitation changes icon

Changes in Precipitation

Altered precipitation patterns lead to increased droughts in some regions and severe flooding in others, affecting water resources.

diff --git a/servers/fastapi/api/v1/ppt/endpoints/slide_to_html.py b/servers/fastapi/api/v1/ppt/endpoints/slide_to_html.py index f56a0394..a75d967b 100644 --- a/servers/fastapi/api/v1/ppt/endpoints/slide_to_html.py +++ b/servers/fastapi/api/v1/ppt/endpoints/slide_to_html.py @@ -110,6 +110,7 @@ async def generate_html_from_slide(base64_image: str, media_type: str, xml_conte Raises: HTTPException: If API call fails or no content is generated """ + print(f"Generating HTML from slide image and XML using Google Gen AI API...") try: # Initialize Google Gen AI client client = genai.Client(api_key=api_key) @@ -164,7 +165,7 @@ async def generate_html_from_slide(base64_image: str, media_type: str, xml_conte print(f"Google API Error: {e}") raise HTTPException( status_code=500, - detail=f"Google API error during HTML generation: {e}" + detail=f"Google API error during HTML generation: {str(e)}" ) except Exception as e: # Handle various API errors @@ -188,7 +189,7 @@ async def generate_html_from_slide(base64_image: str, media_type: str, xml_conte ) -async def generate_react_component_from_html(html_content: str, api_key: str) -> str: +async def generate_react_component_from_html(html_content: str, api_key: str, retry_times: int = 1) -> str: """ Convert HTML content to TSX React component using Google Gen AI API. @@ -202,7 +203,13 @@ async def generate_react_component_from_html(html_content: str, api_key: str) -> Raises: HTTPException: If API call fails or no content is generated """ - try: + if retry_times > 2: + raise HTTPException( + status_code=500, + detail="Google Gen AI API error during React generation" + ) + + if True: # Initialize Google Gen AI client client = genai.Client(api_key=api_key) @@ -236,12 +243,11 @@ async def generate_react_component_from_html(html_content: str, api_key: str) -> print(f"Received React content length: {len(react_content)}") if not react_content: - raise HTTPException( - status_code=500, - detail="No React component generated by Google Gen AI API" - ) + return await generate_react_component_from_html(html_content, api_key, retry_times + 1) - react_content = react_content.replace("```tsx", "").replace("```", "").replace("typescript", "") + react_content = react_content.replace("```tsx", "")\ + .replace("```", "").replace("typescript", "")\ + .replace("javascript", "") # Filter out lines that start with import or export @@ -255,32 +261,32 @@ async def generate_react_component_from_html(html_content: str, api_key: str) -> print(f"Filtered React content length: {len(filtered_react_content)}") return filtered_react_content - except errors.APIError as e: - print(f"Google API Error: {e}") - raise HTTPException( - status_code=500, - detail=f"Google API error during React generation: {e}" - ) - except Exception as e: - # Handle various API errors - error_msg = str(e) - print(f"Exception occurred: {error_msg}") - print(f"Exception type: {type(e)}") - if "timeout" in error_msg.lower(): - raise HTTPException( - status_code=408, - detail=f"Google Gen AI API timeout during React generation: {error_msg}" - ) - elif "connection" in error_msg.lower(): - raise HTTPException( - status_code=503, - detail=f"Google Gen AI API connection error during React generation: {error_msg}" - ) - else: - raise HTTPException( - status_code=500, - detail=f"Google Gen AI API error during React generation: {error_msg}" - ) + # except errors.APIError as e: + # print(f"Google API Error: {e}") + # raise HTTPException( + # status_code=500, + # detail=f"Google API error during React generation: {str(e)}" + # ) + # except Exception as e: + # # Handle various API errors + # error_msg = str(e) + # print(f"Exception occurred: {error_msg}") + # print(f"Exception type: {type(e)}") + # if "timeout" in error_msg.lower(): + # raise HTTPException( + # status_code=408, + # detail=f"Google Gen AI API timeout during React generation: {error_msg}" + # ) + # elif "connection" in error_msg.lower(): + # raise HTTPException( + # status_code=503, + # detail=f"Google Gen AI API connection error during React generation: {error_msg}" + # ) + # else: + # raise HTTPException( + # status_code=500, + # detail=f"Google Gen AI API error during React generation: {error_msg}" + # ) async def edit_html_with_images(current_ui_base64: str, sketch_base64: Optional[str], media_type: str, html_content: str, prompt: str, api_key: str) -> str: @@ -305,9 +311,7 @@ async def edit_html_with_images(current_ui_base64: str, sketch_base64: Optional[ # Initialize Google Gen AI client client = genai.Client(api_key=api_key) - print("Starting streaming request to Google Gen AI for HTML editing...") - - edited_html = "" + print("Starting non-streaming request to Google Gen AI for HTML editing...") # Convert base64 images to bytes current_ui_bytes = base64.b64decode(current_ui_base64) @@ -341,19 +345,19 @@ async def edit_html_with_images(current_ui_base64: str, sketch_base64: Optional[ ), ) - print("Streaming started, collecting edited HTML response...") + print("Making non-streaming request for HTML editing...") - # Stream the response - for chunk in client.models.generate_content_stream( + # Generate content in non-streaming mode + response = client.models.generate_content( model="gemini-2.5-pro", contents=contents, config=generate_content_config, - ): - if chunk.text: - edited_html += chunk.text - print(f"[HTML EDIT] {chunk.text}", end="", flush=True) + ) - print(f"\nCollected edited HTML content length: {len(edited_html)}") + # Extract the response text + edited_html = response.text if response.text else "" + + print(f"Received edited HTML content length: {len(edited_html)}") if not edited_html: raise HTTPException( @@ -363,9 +367,17 @@ async def edit_html_with_images(current_ui_base64: str, sketch_base64: Optional[ return edited_html + except errors.APIError as e: + print(f"Google API Error: {e}") + raise HTTPException( + status_code=500, + detail=f"Google API error during HTML editing: {str(e)}" + ) except Exception as e: # Handle various API errors error_msg = str(e) + print(f"Exception occurred: {error_msg}") + print(f"Exception type: {type(e)}") if "timeout" in error_msg.lower(): raise HTTPException( status_code=408, @@ -485,7 +497,7 @@ async def convert_html_to_react(request: HtmlToReactRequest): Returns: HtmlToReactResponse with generated React component """ - try: + if True: # Get Google Gen AI API key from environment api_key = os.getenv("GOOGLE_API_KEY") if not api_key: @@ -515,16 +527,16 @@ async def convert_html_to_react(request: HtmlToReactRequest): message="React component generated successfully" ) - except HTTPException: - # Re-raise HTTP exceptions as-is - raise - except Exception as e: - # Log the full error for debugging - print(f"Unexpected error during HTML to React processing: {str(e)}") - raise HTTPException( - status_code=500, - detail=f"Error processing HTML to React: {str(e)}" - ) + # except HTTPException: + # # Re-raise HTTP exceptions as-is + # raise + # except Exception as e: + # # Log the full error for debugging + # print(f"Unexpected error during HTML to React processing: {str(e)}") + # raise HTTPException( + # status_code=500, + # detail=f"Error processing HTML to React: {str(e)}" + # ) # ENDPOINT 3: HTML editing with images