mgbam commited on
Commit
a73275a
Β·
verified Β·
1 Parent(s): e244f95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -7,7 +7,7 @@ This file defines the user interface, handles user interactions, and orchestrate
7
  the calls to the backend logic in other modules.
8
  """
9
  import gradio as gr
10
- import os # <--- FIX: Import the os module
11
 
12
  from config import AVAILABLE_MODELS, MULTIMODAL_MODELS
13
  from core import generate_code
@@ -54,7 +54,6 @@ def on_model_change(model_name: str):
54
  def on_language_change(language: str):
55
  return gr.update(language=get_gradio_language(language))
56
 
57
- # <--- FIX: Use gr.Request to get the auth token
58
  def on_deploy_click(code: str, space_name: str, sdk_name: str, request: gr.Request):
59
  """Handler for the deploy button, now correctly using gr.Request."""
60
  hf_token = request.token
@@ -67,6 +66,7 @@ def on_deploy_click(code: str, space_name: str, sdk_name: str, request: gr.Reque
67
 
68
  # --- Gradio UI Layout ---
69
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as demo:
 
70
  history_state = gr.State([])
71
 
72
  gr.Markdown("# πŸ€– AnyCoder - AI Code Generator")
@@ -88,21 +88,15 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as
88
  clear_btn = gr.Button("Clear")
89
  generate_btn = gr.Button("Generate", variant="primary", scale=2)
90
 
91
- # <--- FIX: Conditionally render the deployment UI only when on a HF Space
 
92
  if os.getenv("SPACE_ID"):
93
  with gr.Accordion("πŸš€ Deploy to Hugging Face", open=False):
94
- login_button = gr.LoginButton()
95
  space_name_input = gr.Textbox(label="New App Name", placeholder="my-cool-app")
96
  sdk_dropdown = gr.Dropdown(choices=["Static (HTML)", "Gradio (Python)"], value="Static (HTML)", label="App Type")
97
  deploy_btn = gr.Button("Deploy")
98
  deploy_status = gr.Markdown(visible=False)
99
-
100
- # <--- FIX: Correct event handler wiring
101
- deploy_btn.click(
102
- on_deploy_click,
103
- inputs=[code_output, space_name_input, sdk_dropdown],
104
- outputs=deploy_status
105
- )
106
 
107
  with gr.Column(scale=2):
108
  with gr.Tabs():
@@ -116,19 +110,33 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as
116
  type="messages",
117
  height=500
118
  )
 
 
 
119
 
120
- # --- Event Wiring ---
121
  generate_btn.click(
122
  fn=on_generate_click,
123
  inputs=[input_prompt, image_input, file_input, website_url_input, history_state, model_dropdown, search_toggle, language_dropdown],
124
  outputs=[code_output, sandbox, history_state, history_output]
125
  )
 
 
126
  clear_btn.click(lambda: ([], [], None, None, None, "", ""),
127
  outputs=[history_state, history_output, input_prompt, image_input, file_input, website_url_input, code_output])
128
 
 
129
  model_dropdown.change(on_model_change, inputs=model_dropdown, outputs=image_input)
130
  language_dropdown.change(on_language_change, inputs=language_dropdown, outputs=code_output)
131
  language_dropdown.change(lambda code, lang: send_to_sandbox(code, lang), inputs=[code_output, language_dropdown], outputs=sandbox)
132
 
 
 
 
 
 
 
 
 
133
  if __name__ == "__main__":
134
  demo.queue().launch()
 
7
  the calls to the backend logic in other modules.
8
  """
9
  import gradio as gr
10
+ import os
11
 
12
  from config import AVAILABLE_MODELS, MULTIMODAL_MODELS
13
  from core import generate_code
 
54
  def on_language_change(language: str):
55
  return gr.update(language=get_gradio_language(language))
56
 
 
57
  def on_deploy_click(code: str, space_name: str, sdk_name: str, request: gr.Request):
58
  """Handler for the deploy button, now correctly using gr.Request."""
59
  hf_token = request.token
 
66
 
67
  # --- Gradio UI Layout ---
68
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as demo:
69
+ # 1. DEFINE ALL COMPONENTS FIRST
70
  history_state = gr.State([])
71
 
72
  gr.Markdown("# πŸ€– AnyCoder - AI Code Generator")
 
88
  clear_btn = gr.Button("Clear")
89
  generate_btn = gr.Button("Generate", variant="primary", scale=2)
90
 
91
+ # Conditionally define the deployment components
92
+ # We must define them here so they exist for the event wiring later.
93
  if os.getenv("SPACE_ID"):
94
  with gr.Accordion("πŸš€ Deploy to Hugging Face", open=False):
95
+ login_button = gr.LoginButton() # This is just a placeholder in this context
96
  space_name_input = gr.Textbox(label="New App Name", placeholder="my-cool-app")
97
  sdk_dropdown = gr.Dropdown(choices=["Static (HTML)", "Gradio (Python)"], value="Static (HTML)", label="App Type")
98
  deploy_btn = gr.Button("Deploy")
99
  deploy_status = gr.Markdown(visible=False)
 
 
 
 
 
 
 
100
 
101
  with gr.Column(scale=2):
102
  with gr.Tabs():
 
110
  type="messages",
111
  height=500
112
  )
113
+
114
+ # 2. WIRE UP ALL EVENTS AT THE END
115
+ # This ensures all component variables (like 'code_output') are defined before being used.
116
 
117
+ # Main generation event
118
  generate_btn.click(
119
  fn=on_generate_click,
120
  inputs=[input_prompt, image_input, file_input, website_url_input, history_state, model_dropdown, search_toggle, language_dropdown],
121
  outputs=[code_output, sandbox, history_state, history_output]
122
  )
123
+
124
+ # Clear button event
125
  clear_btn.click(lambda: ([], [], None, None, None, "", ""),
126
  outputs=[history_state, history_output, input_prompt, image_input, file_input, website_url_input, code_output])
127
 
128
+ # UI change events
129
  model_dropdown.change(on_model_change, inputs=model_dropdown, outputs=image_input)
130
  language_dropdown.change(on_language_change, inputs=language_dropdown, outputs=code_output)
131
  language_dropdown.change(lambda code, lang: send_to_sandbox(code, lang), inputs=[code_output, language_dropdown], outputs=sandbox)
132
 
133
+ # Conditionally wire up the deployment event
134
+ if os.getenv("SPACE_ID"):
135
+ deploy_btn.click(
136
+ on_deploy_click,
137
+ inputs=[code_output, space_name_input, sdk_dropdown],
138
+ outputs=deploy_status
139
+ )
140
+
141
  if __name__ == "__main__":
142
  demo.queue().launch()