File size: 7,628 Bytes
1857a25
05e3f60
9592ee8
e1cd8a1
3070d9e
e89a843
9592ee8
e1cd8a1
 
 
05e3f60
e1cd8a1
1857a25
e1cd8a1
1857a25
9592ee8
 
e1cd8a1
1857a25
e1cd8a1
 
 
 
2d35fe8
e1cd8a1
 
eb21a74
1857a25
3070d9e
 
 
 
 
 
 
 
 
 
 
e1cd8a1
c9c5035
a37e8ba
 
 
 
 
 
 
 
 
75b99f9
e143b97
1857a25
 
 
0673272
1857a25
 
 
 
 
e1cd8a1
 
 
 
 
eb21a74
c14cd0b
 
 
eb21a74
 
c14cd0b
 
c8109c5
8c50225
eb21a74
57301b6
6d29ea4
 
 
 
1857a25
c9c5035
eb21a74
f86d510
003aa82
c9c5035
 
f86d510
c9c5035
eb7f0d2
1857a25
c9f21d4
 
 
 
 
 
 
c8a96f9
c9f21d4
163a5ed
0463035
 
 
c8109c5
0463035
3070d9e
5df282b
 
 
 
 
 
 
 
57301b6
2453f3e
 
 
c9f21d4
c9c5035
 
c9f21d4
c9c5035
 
1857a25
c9f21d4
c9c5035
 
21bcfa3
c9c5035
5df282b
6d29ea4
1316d52
eb21a74
c9f21d4
c9c5035
21bcfa3
1857a25
c9c5035
3f24fde
 
75b99f9
2453f3e
 
 
 
 
 
c9f21d4
 
 
 
 
 
2453f3e
 
 
 
21ffe15
75b99f9
 
a2e6071
 
 
 
 
7178e8d
bb1e5fe
 
7178e8d
 
a2e6071
5479092
c8a96f9
bb1e5fe
7178e8d
5479092
 
7178e8d
 
 
c9f21d4
72b0d5f
21ffe15
ac02b8b
21ffe15
 
bbab5dc
a37e8ba
76f4a65
c8a96f9
c9c5035
e89a843
 
 
c9f21d4
 
 
21ffe15
f86d510
 
e079bd5
f86d510
 
e079bd5
 
003aa82
e079bd5
003aa82
f86d510
 
 
 
 
 
 
 
21ffe15
ddd06fc
 
1857a25
e1cd8a1
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import gradio as gr
from huggingface_hub import hf_hub_download
import importlib.util
import base64

# --- Private Repository Information ---
PRIVATE_DATASET_ID = os.getenv("PRIVATE_DATASET_ID")
HF_TOKEN = os.getenv("HF_TOKEN")
INDEX_SUBDIR = os.getenv("INDEX_SUBDIR", ".")

# --- Core Logic Download and Import ---
try:
    AGENT_CODE_PATH = hf_hub_download(
        repo_id=PRIVATE_DATASET_ID,
        filename="deepv_core.py",
        repo_type="dataset",
        token=HF_TOKEN
    )
    spec = importlib.util.spec_from_file_location("deepv_core_module", AGENT_CODE_PATH)
    agent_module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(agent_module)
    run_generation = agent_module.run_generation
    
except Exception as e:
    def show_error(*args):
        return f"// ERROR: Failed to load core agent code. Details: {e}", "", ""

# --- Base64 Encoding for Logo ---
def get_logo_base64():
    try:
        with open("DeepV_logo.png", "rb") as image_file:
            return base64.b64encode(image_file.read()).decode("utf-8")
    except FileNotFoundError:
        print("Logo file not found. Ensure DeepV_logo.png is in the correct directory.")
        return ""

logo_base64 = get_logo_base64()

# --- Gradio UI setup below ---
with gr.Blocks(title="DeepV for RTL (Model-Agnostic)", theme=gr.themes.Soft()) as demo:
    # The logo is now at the top middle, outside of any column structure
    if logo_base64:
        gr.HTML(f"""
            <div id="deepv-logo-container">
                <img id="deepv-logo" src="data:image/png;base64,{logo_base64}" alt="DeepV Logo">
            </div>
        """)
    else:
        gr.Markdown("Logo not found.")

    with gr.Row():
        with gr.Column(scale=2):
            with gr.Row():
                model_choice = gr.Dropdown(
                    choices=["gpt-4o", "gpt-4.1", "gpt-5-chat-latest"],
                    value="gpt-4o",
                    label="Model"
                )
                api_key = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...")

            gr.Markdown(
                """
                **Note:** Your API key is used for the current session only and is not saved or stored.
                """
            )

            spec = gr.Textbox(
                label="Design Specification (natural language or I/O contract)",
                placeholder="e.g., 8-bit UART transmitter with baud rate generator ...",
                lines=10,
                elem_id="spec-input"
            )
            with gr.Row():
                use_rag = gr.Checkbox(value=True, label="Use RAG")
                top_k = gr.Slider(1, 5, value=3, step=1, label="Top-K retrieved examples")

            # --- Generation settings as text boxes ---
            with gr.Row():
                temperature_tb = gr.Textbox(label="Temperature", value="0.2", scale=1)
                top_p_tb = gr.Textbox(label="Top-p", value="0.9", scale=1)
                max_new_tokens_tb = gr.Textbox(label="Max tokens", value="768", scale=1)

            # --- Loading State Components ---
            with gr.Row():
                run_btn = gr.Button("Generate Verilog", variant="primary", elem_id="generate-button")
                loading_state = gr.Markdown(
                    value="Generating...",
                    visible=False,
                    elem_id="loading-state"
                )
            
        with gr.Column(scale=3):
            out_code = gr.Textbox(
                label="Generated Verilog",
                lines=28,
                interactive=False,
                placeholder="// Your Verilog code will appear here",
                elem_id="verilog-output"
            )

            copy_button = gr.Button("📋", variant="secondary", elem_id="copy-button")
            
            with gr.Row():
                clear_btn = gr.ClearButton(
                    value="Clear All",
                    components=[spec, out_code]
                )

    # --- Wrapper function to handle output from the generation agent ---
    def generate_only(
        spec, use_rag, top_k, model_choice, api_key, temperature, top_p, max_new_tokens
    ):
        verilog_code, _, _ = run_generation(
            spec, use_rag, top_k, model_choice, api_key, temperature, top_p, max_new_tokens
        )
        return verilog_code
        
    def copy_to_clipboard_fn(text):
        return text

    def show_loading_no_anim():
        return [gr.update(visible=False), gr.update(visible=True)]

    def hide_loading_no_anim():
        return [gr.update(visible=True), gr.update(visible=False)]

    run_btn.click(
        fn=show_loading_no_anim,
        inputs=[],
        outputs=[run_btn, loading_state],
        show_progress=False,
    ).then(
        fn=generate_only,
        inputs=[spec, use_rag, top_k, model_choice, api_key, temperature_tb, top_p_tb, max_new_tokens_tb],
        outputs=[out_code],
    ).then(
        fn=hide_loading_no_anim,
        inputs=[],
        outputs=[run_btn, loading_state],
    )

    clear_btn.click(fn=lambda: "Ready", outputs=[])
    spec.submit(fn=lambda: "Ready", outputs=[])

    copy_button.click(
        fn=copy_to_clipboard_fn,
        inputs=[out_code],
        outputs=[],
        js="""
            (text) => {
                const el = document.createElement('textarea');
                el.value = text;
                document.body.appendChild(el);
                el.select();
                document.execCommand('copy');
                document.body.removeChild(el);
            }
        """
    )

    demo.css = """
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap');

        /* New CSS to make the layout more compact */
        .gradio-container {
            margin-top: 0 !important;
        }

        #deepv-logo-container {
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 0;
            margin-bottom: 0px; 
            margin-top: -35px; /* Pull the logo up */
        }

        #deepv-logo {
            width: 450px; 
            height: 125px; 
            object-fit: contain;
        }
        
        #verilog-output {
            position: relative;
        }

        #copy-button {
            position: absolute;
            top: 50px; 
            right: 20px;
            z-index: 1000;
            background-color: #F0F0F0;
            color: #333;
            border-radius: 5px;
            border: none;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            width: 30px;
            height: 30px;
            padding: 0;
        }

        #loading-state {
            padding: 14px;
            text-align: center;
            font-size: 1.2em;
            font-weight: 800; 
            color: #1E3A8A;
            background-color: #e6f2ff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            animation: pulse 1s infinite;
        }

        @keyframes pulse {
            0% { background-color: #e6f2ff; }
            50% { background-color: #d2e4f7; }
            100% { background-color: #e6f2ff; }
        }
    """


if __name__ == "__main__":
    if 'agent_module' in locals():
        demo.launch()
    else:
        with gr.Blocks() as error_demo:
            gr.Markdown("# Initialization Error")
            gr.Markdown(f"An error occurred while loading the application code. Please check your configuration.")
            gr.Textbox(label="Error Details", value=str(e), lines=5)
        error_demo.launch()