akhaliq HF Staff commited on
Commit
9fb4088
·
1 Parent(s): 0e5d991

ad deploy app button

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py CHANGED
@@ -16,6 +16,8 @@ from bs4 import BeautifulSoup
16
  import html2text
17
  import json
18
  import time
 
 
19
 
20
  import gradio as gr
21
  from huggingface_hub import InferenceClient
@@ -1079,6 +1081,51 @@ This will help me create a better design for you."""
1079
  history_output: history_to_chatbot_messages(_history),
1080
  }
1081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1082
  # Main application
1083
  with gr.Blocks(
1084
  theme=gr.themes.Base(
@@ -1194,6 +1241,8 @@ with gr.Blocks(
1194
  interactive=False,
1195
  label="Generated code"
1196
  )
 
 
1197
  with gr.Tab("Preview"):
1198
  sandbox = gr.HTML(label="Live preview")
1199
  with gr.Tab("History"):
@@ -1221,5 +1270,13 @@ with gr.Blocks(
1221
  language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
1222
  clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
1223
 
 
 
 
 
 
 
 
 
1224
  if __name__ == "__main__":
1225
  demo.queue(api_open=False, default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=False, show_api=False)
 
16
  import html2text
17
  import json
18
  import time
19
+ import webbrowser
20
+ import urllib.parse
21
 
22
  import gradio as gr
23
  from huggingface_hub import InferenceClient
 
1081
  history_output: history_to_chatbot_messages(_history),
1082
  }
1083
 
1084
+ # Deploy to Spaces logic
1085
+
1086
+ def wrap_html_in_gradio_app(html_code):
1087
+ # Minimal Gradio app that serves the HTML code
1088
+ return f'''import gradio as gr\n\ndef show_html():\n return """{html_code.replace('"', '\\"').replace("'", "\\'")}"""\n\ndemo = gr.Interface(fn=show_html, inputs=None, outputs=gr.HTML())\n\nif __name__ == "__main__":\n demo.launch()\n'''
1089
+
1090
+ def deploy_to_spaces(code):
1091
+ if not code or not code.strip():
1092
+ return # Do nothing if code is empty
1093
+ # Wrap the HTML code in a Gradio app
1094
+ app_py = wrap_html_in_gradio_app(code.strip())
1095
+ base_url = "https://huggingface.co/new-space"
1096
+ params = urllib.parse.urlencode({
1097
+ "name": "new-space",
1098
+ "sdk": "gradio"
1099
+ })
1100
+ # Use urlencode for file params
1101
+ files_params = urllib.parse.urlencode({
1102
+ "files[0][path]": "app.py",
1103
+ "files[0][content]": app_py
1104
+ })
1105
+ full_url = f"{base_url}?{params}&{files_params}"
1106
+ webbrowser.open_new_tab(full_url)
1107
+
1108
+ def wrap_html_in_static_app(html_code):
1109
+ # For static Spaces, just use the HTML code as-is
1110
+ return html_code
1111
+
1112
+ def deploy_to_spaces_static(code):
1113
+ if not code or not code.strip():
1114
+ return # Do nothing if code is empty
1115
+ # Use the HTML code directly for static Spaces
1116
+ app_html = wrap_html_in_static_app(code.strip())
1117
+ base_url = "https://huggingface.co/new-space"
1118
+ params = urllib.parse.urlencode({
1119
+ "name": "new-space",
1120
+ "sdk": "static"
1121
+ })
1122
+ files_params = urllib.parse.urlencode({
1123
+ "files[0][path]": "index.html",
1124
+ "files[0][content]": app_html
1125
+ })
1126
+ full_url = f"{base_url}?{params}&{files_params}"
1127
+ webbrowser.open_new_tab(full_url)
1128
+
1129
  # Main application
1130
  with gr.Blocks(
1131
  theme=gr.themes.Base(
 
1241
  interactive=False,
1242
  label="Generated code"
1243
  )
1244
+ # Rename button to '🚀 Deploy App'
1245
+ deploy_btn = gr.Button("🚀 Deploy App", variant="primary", size="sm", visible=True)
1246
  with gr.Tab("Preview"):
1247
  sandbox = gr.HTML(label="Live preview")
1248
  with gr.Tab("History"):
 
1270
  language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
1271
  clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
1272
 
1273
+ # Deploy to Spaces logic
1274
+
1275
+ deploy_btn.click(
1276
+ fn=lambda code: deploy_to_spaces_static(code),
1277
+ inputs=code_output,
1278
+ outputs=None
1279
+ )
1280
+
1281
  if __name__ == "__main__":
1282
  demo.queue(api_open=False, default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=False, show_api=False)