Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -152,64 +152,82 @@ def generation_code( | |
| 152 | 
             
            # UI LAYOUT & EVENT WIRING
         | 
| 153 | 
             
            # ==============================================================================
         | 
| 154 |  | 
| 155 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 156 | 
             
                # --- State Management ---
         | 
| 157 | 
             
                history_state = gr.State([])
         | 
| 158 | 
             
                initial_model = AVAILABLE_MODELS[0]
         | 
| 159 | 
             
                model_state = gr.State(initial_model)
         | 
| 160 |  | 
| 161 | 
             
                # --- UI Definition ---
         | 
| 162 | 
            -
                 | 
| 163 | 
            -
             | 
| 164 | 
            -
             | 
| 165 | 
            -
             | 
| 166 | 
            -
                     | 
| 167 | 
            -
             | 
| 168 | 
            -
             | 
| 169 | 
            -
                         | 
| 170 | 
            -
                         | 
| 171 | 
            -
             | 
| 172 | 
            -
             | 
| 173 | 
            -
             | 
|  | |
|  | |
| 174 |  | 
| 175 | 
            -
             | 
| 176 | 
            -
                         | 
| 177 | 
            -
             | 
| 178 | 
            -
             | 
| 179 | 
            -
             | 
| 180 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 181 | 
             
                        language_dd = gr.Dropdown(
         | 
| 182 | 
             
                            choices=["html", "python", "transformers.js", "sql", "javascript", "css"],
         | 
| 183 | 
             
                            value="html",
         | 
| 184 | 
            -
                            label=" | 
| 185 | 
             
                        )
         | 
| 186 | 
            -
                        search_chk = gr.Checkbox(label=" | 
| 187 | 
            -
             | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
             | 
| 191 | 
            -
             | 
| 192 | 
            -
             | 
| 193 | 
            -
                    with gr. | 
| 194 | 
            -
                        with gr. | 
| 195 | 
            -
                             | 
| 196 | 
            -
             | 
| 197 | 
            -
                             | 
| 198 | 
            -
             | 
| 199 | 
            -
                             | 
| 200 | 
            -
             | 
| 201 |  | 
| 202 | 
             
                # --- Event Wiring ---
         | 
| 203 | 
            -
                # Event listeners must be defined within the gr.Blocks context.
         | 
| 204 | 
            -
             | 
| 205 | 
             
                def on_model_change(model_name: str) -> Dict:
         | 
| 206 | 
             
                    """Updates the model_state when the user selects a new model."""
         | 
| 207 | 
             
                    model_details = get_model_details(model_name)
         | 
| 208 | 
             
                    return model_details or initial_model
         | 
| 209 | 
            -
                
         | 
| 210 | 
            -
                model_dd.change(fn=on_model_change, inputs=[model_dd], outputs=[model_state])
         | 
| 211 |  | 
| 212 | 
            -
                 | 
|  | |
| 213 |  | 
| 214 | 
             
                gen_btn.click(
         | 
| 215 | 
             
                    fn=generation_code,
         | 
| @@ -221,12 +239,20 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ShashaCode - AI Code Generator") a | |
| 221 | 
             
                )
         | 
| 222 |  | 
| 223 | 
             
                def clear_session():
         | 
| 224 | 
            -
                    """Resets  | 
| 225 | 
            -
                    return  | 
| 226 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 227 | 
             
                clr_btn.click(
         | 
| 228 | 
             
                    fn=clear_session,
         | 
| 229 | 
            -
                    outputs=[prompt_in,  | 
| 230 | 
             
                    queue=False
         | 
| 231 | 
             
                )
         | 
| 232 |  | 
|  | |
| 152 | 
             
            # UI LAYOUT & EVENT WIRING
         | 
| 153 | 
             
            # ==============================================================================
         | 
| 154 |  | 
| 155 | 
            +
            # Custom CSS for a more professional and modern look
         | 
| 156 | 
            +
            CUSTOM_CSS = """
         | 
| 157 | 
            +
            body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; }
         | 
| 158 | 
            +
            #main_title { text-align: center; font-size: 2.5rem; font-weight: 700; color: #1a202c; margin: 1.5rem 0 0.5rem 0; }
         | 
| 159 | 
            +
            #subtitle { text-align: center; color: #4a5568; margin-bottom: 2.5rem; font-size: 1.1rem; }
         | 
| 160 | 
            +
            .gradio-container { background-color: #f7fafc; }
         | 
| 161 | 
            +
            /* Custom styling for the generate button to make it stand out */
         | 
| 162 | 
            +
            #gen_btn { box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); }
         | 
| 163 | 
            +
            """
         | 
| 164 | 
            +
             | 
| 165 | 
            +
            with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), title="AnyCoder - AI Code Generator", css=CUSTOM_CSS) as demo:
         | 
| 166 | 
             
                # --- State Management ---
         | 
| 167 | 
             
                history_state = gr.State([])
         | 
| 168 | 
             
                initial_model = AVAILABLE_MODELS[0]
         | 
| 169 | 
             
                model_state = gr.State(initial_model)
         | 
| 170 |  | 
| 171 | 
             
                # --- UI Definition ---
         | 
| 172 | 
            +
                gr.Markdown("# π Shasha AI", elem_id="main_title")
         | 
| 173 | 
            +
                gr.Markdown("Your personal AI partner for generating, modifying, and understanding code.", elem_id="subtitle")
         | 
| 174 | 
            +
             | 
| 175 | 
            +
                with gr.Row(equal_height=False):
         | 
| 176 | 
            +
                    # Left column for controls and inputs
         | 
| 177 | 
            +
                    with gr.Column(scale=1):
         | 
| 178 | 
            +
                        gr.Markdown("### 1. Select Model")
         | 
| 179 | 
            +
                        model_choices = [model["name"] for model in AVAILABLE_MODELS]
         | 
| 180 | 
            +
                        model_dd = gr.Dropdown(
         | 
| 181 | 
            +
                            choices=model_choices,
         | 
| 182 | 
            +
                            value=initial_model["name"],
         | 
| 183 | 
            +
                            label="AI Model",
         | 
| 184 | 
            +
                            info="Different models have different strengths."
         | 
| 185 | 
            +
                        )
         | 
| 186 |  | 
| 187 | 
            +
                        gr.Markdown("### 2. Provide Context")
         | 
| 188 | 
            +
                        with gr.Tabs():
         | 
| 189 | 
            +
                            with gr.Tab("π Prompt"):
         | 
| 190 | 
            +
                                prompt_in = gr.Textbox(
         | 
| 191 | 
            +
                                    label="Your Request",
         | 
| 192 | 
            +
                                    lines=7,
         | 
| 193 | 
            +
                                    placeholder="e.g., 'Create a modern, responsive landing page for a SaaS product.'",
         | 
| 194 | 
            +
                                    show_label=False
         | 
| 195 | 
            +
                                )
         | 
| 196 | 
            +
                            with gr.Tab("π File"):
         | 
| 197 | 
            +
                                file_in = gr.File(label="Attach File (Optional)", type="filepath")
         | 
| 198 | 
            +
                            with gr.Tab("π Website"):
         | 
| 199 | 
            +
                                url_site = gr.Textbox(label="Scrape Website (Optional)", placeholder="https://example.com")
         | 
| 200 | 
            +
             | 
| 201 | 
            +
                        gr.Markdown("### 3. Configure Output")
         | 
| 202 | 
             
                        language_dd = gr.Dropdown(
         | 
| 203 | 
             
                            choices=["html", "python", "transformers.js", "sql", "javascript", "css"],
         | 
| 204 | 
             
                            value="html",
         | 
| 205 | 
            +
                            label="Target Language"
         | 
| 206 | 
             
                        )
         | 
| 207 | 
            +
                        search_chk = gr.Checkbox(label="Enable Web Search", info="Enhances AI with real-time info.")
         | 
| 208 | 
            +
             | 
| 209 | 
            +
                        with gr.Row():
         | 
| 210 | 
            +
                            clr_btn = gr.Button("Clear Session", variant="secondary")
         | 
| 211 | 
            +
                            gen_btn = gr.Button("Generate Code", variant="primary", elem_id="gen_btn")
         | 
| 212 | 
            +
             | 
| 213 | 
            +
                    # Right column for outputs
         | 
| 214 | 
            +
                    with gr.Column(scale=2):
         | 
| 215 | 
            +
                        with gr.Tabs() as main_tabs:
         | 
| 216 | 
            +
                            with gr.Tab("π» Code", id="code_tab"):
         | 
| 217 | 
            +
                                code_out = gr.Code(label="Generated Code", language="html", interactive=True)
         | 
| 218 | 
            +
                            with gr.Tab("ποΈ Live Preview", id="preview_tab"):
         | 
| 219 | 
            +
                                preview_out = gr.HTML(label="Live Preview")
         | 
| 220 | 
            +
                            with gr.Tab("π History", id="history_tab"):
         | 
| 221 | 
            +
                                chat_out = gr.Chatbot(label="Conversation History", type="messages")
         | 
| 222 |  | 
| 223 | 
             
                # --- Event Wiring ---
         | 
|  | |
|  | |
| 224 | 
             
                def on_model_change(model_name: str) -> Dict:
         | 
| 225 | 
             
                    """Updates the model_state when the user selects a new model."""
         | 
| 226 | 
             
                    model_details = get_model_details(model_name)
         | 
| 227 | 
             
                    return model_details or initial_model
         | 
|  | |
|  | |
| 228 |  | 
| 229 | 
            +
                model_dd.change(fn=on_model_change, inputs=[model_dd], outputs=[model_state])
         | 
| 230 | 
            +
                language_dd.change(fn=lambda lang: gr.update(language=lang), inputs=[language_dd], outputs=[code_out])
         | 
| 231 |  | 
| 232 | 
             
                gen_btn.click(
         | 
| 233 | 
             
                    fn=generation_code,
         | 
|  | |
| 239 | 
             
                )
         | 
| 240 |  | 
| 241 | 
             
                def clear_session():
         | 
| 242 | 
            +
                    """Resets all UI components and state to their initial values."""
         | 
| 243 | 
            +
                    return (
         | 
| 244 | 
            +
                        "",      # prompt_in
         | 
| 245 | 
            +
                        None,    # file_in
         | 
| 246 | 
            +
                        "",      # url_site
         | 
| 247 | 
            +
                        [],      # history_state
         | 
| 248 | 
            +
                        "",      # code_out
         | 
| 249 | 
            +
                        "",      # preview_out
         | 
| 250 | 
            +
                        []       # chat_out
         | 
| 251 | 
            +
                    )
         | 
| 252 | 
            +
             | 
| 253 | 
             
                clr_btn.click(
         | 
| 254 | 
             
                    fn=clear_session,
         | 
| 255 | 
            +
                    outputs=[prompt_in, file_in, url_site, history_state, code_out, preview_out, chat_out],
         | 
| 256 | 
             
                    queue=False
         | 
| 257 | 
             
                )
         | 
| 258 |  | 
