update: updated prompt for template

This commit is contained in:
Suraj Jha 2025-08-07 20:33:16 +05:45
parent 9f31a46afd
commit 7429ba2387
2 changed files with 71 additions and 57 deletions

File diff suppressed because one or more lines are too long

View file

@ -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