Update api_clients.py
Browse files- api_clients.py +17 -29
api_clients.py
CHANGED
|
@@ -129,39 +129,27 @@ This will help me create a better design for you."""
|
|
| 129 |
clean_code = remove_code_block(content)
|
| 130 |
if has_existing_html:
|
| 131 |
# Fallback: If the model returns a full HTML file, use it directly
|
| 132 |
-
if clean_code.strip().startswith("<!DOCTYPE html>") or clean_code.strip().startswith("<html"):
|
| 133 |
-
yield {
|
| 134 |
-
"code_output": gr.update(value=clean_code, language=get_gradio_language(language)),
|
| 135 |
-
"history_output": history_to_chatbot_messages(_history),
|
| 136 |
-
"sandbox": send_to_sandbox(clean_code) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",
|
| 137 |
-
}
|
| 138 |
-
else:
|
| 139 |
last_html = _history[-1][1] if _history else ""
|
| 140 |
modified_html = apply_search_replace_changes(last_html, clean_code)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
"code_output": gr.update(value=clean_code, language=get_gradio_language(language)),
|
| 150 |
-
"history_output": history_to_chatbot_messages(_history),
|
| 151 |
-
"sandbox": send_to_sandbox(clean_code) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>",
|
| 152 |
-
}
|
| 153 |
# Final update
|
| 154 |
_history = messages_to_history(messages + [{'role': 'assistant', 'content': content}])
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
error_message = f"Error: {str(e)}"
|
| 164 |
-
yield
|
| 165 |
-
"code_output": error_message,
|
| 166 |
-
"history_output": history_to_chatbot_messages(_history),
|
| 167 |
-
}
|
|
|
|
| 129 |
clean_code = remove_code_block(content)
|
| 130 |
if has_existing_html:
|
| 131 |
# Fallback: If the model returns a full HTML file, use it directly
|
| 132 |
+
if not (clean_code.strip().startswith("<!DOCTYPE html>") or clean_code.strip().startswith("<html")):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
last_html = _history[-1][1] if _history else ""
|
| 134 |
modified_html = apply_search_replace_changes(last_html, clean_code)
|
| 135 |
+
clean_code = remove_code_block(modified_html)
|
| 136 |
+
|
| 137 |
+
yield (
|
| 138 |
+
gr.update(value=clean_code, language=get_gradio_language(language)),
|
| 139 |
+
_history,
|
| 140 |
+
send_to_sandbox(clean_code) if language == "html" else "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML.</div>",
|
| 141 |
+
history_to_chatbot_messages(_history)
|
| 142 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
# Final update
|
| 144 |
_history = messages_to_history(messages + [{'role': 'assistant', 'content': content}])
|
| 145 |
+
final_code = remove_code_block(content)
|
| 146 |
+
yield (
|
| 147 |
+
final_code,
|
| 148 |
+
_history,
|
| 149 |
+
send_to_sandbox(final_code),
|
| 150 |
+
history_to_chatbot_messages(_history),
|
| 151 |
+
)
|
| 152 |
|
| 153 |
except Exception as e:
|
| 154 |
error_message = f"Error: {str(e)}"
|
| 155 |
+
yield (error_message, _history, None, history_to_chatbot_messages(_history))
|
|
|
|
|
|
|
|
|