Update app.py
Browse files
app.py
CHANGED
@@ -86,24 +86,43 @@ with gr.Blocks() as demo:
|
|
86 |
gr.Markdown("""
|
87 |
# π§ Optical Thin-Film Stack AI Agent with Multiple Tool Calls
|
88 |
|
89 |
-
This interactive demo showcases an **AI agent that coordinates multiple tool calls** to solve an inverse optics problem.
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
""")
|
92 |
-
|
93 |
-
gr.Markdown("### π Transmission Spectra of Layer Stack Designs")
|
94 |
gr.Image(value="121_resized.png", interactive=False)
|
95 |
-
|
96 |
gr.Markdown("""
|
97 |
-
|
|
|
98 |
|
99 |
| Tool | Description | Arguments |
|
100 |
|--------------------|--------------------------------------------|-----------------------------------------|
|
101 |
| `simulate_spectrum`| Simulates optical spectrum | `layer_order`: List of materials |
|
102 |
| `cosine_similarity`| Compares predicted vs target spectra | `vec1`, `vec2`: Lists of floats |
|
103 |
| `final_answer` | Emits final result with justification | `answer`: any |
|
|
|
|
|
104 |
""")
|
105 |
|
106 |
-
|
|
|
107 |
run_btn = gr.Button("π Run Agent on Random Stack")
|
108 |
true_order = gr.Textbox(label="True Material Order")
|
109 |
prompt_box = gr.Textbox(label="Agent Prompt")
|
@@ -120,8 +139,8 @@ with gr.Blocks() as demo:
|
|
120 |
return
|
121 |
|
122 |
prompt = f"""You are an AI agent that uses tools to simulate optical spectra and compare them to a target spectrum.
|
123 |
-
|
124 |
-
|
125 |
Stop when similarity > 0.999. Report number of permutations tried.
|
126 |
Target spectrum: {target_val}
|
127 |
"""
|
|
|
86 |
gr.Markdown("""
|
87 |
# π§ Optical Thin-Film Stack AI Agent with Multiple Tool Calls
|
88 |
|
89 |
+
This interactive demo showcases an **AI agent that coordinates multiple tool calls** to solve an inverse optics problem.
|
90 |
+
Rather than relying on a single inference, the agent repeatedly invokes a **physics-based simulator (RCWA)** and a **spectrum comparison function** to iteratively search for the correct material ordering in a thin-film stack.
|
91 |
+
It dynamically explores options, evaluates feedback, and stops only when a verifiably accurate match is found.
|
92 |
+
|
93 |
+
### π€ Objective: Reverse-engineer a 4-layer thin-film stack
|
94 |
+
Given only a target transmission spectrum, the agent must find the **correct material order** that would reproduce it.
|
95 |
+
**Constraints:**
|
96 |
+
- Materials: `Si`, `SiβNβ`, `SiOβ`, `AlN` (used once each)
|
97 |
+
- Terminate when `cosine_similarity > 0.999`
|
98 |
+
|
99 |
+
## π What's Happening Under the Hood
|
100 |
+
|
101 |
+
1. A **random 4-layer stack** is generated from a predefined material set (`Si`, `SiβNβ`, `SiOβ`, `AlN`), with each layer set to 100nm.
|
102 |
+
2. Its **optical transmission spectrum** is computed using **RCWA (Rigorous Coupled-Wave Analysis)** β a high-fidelity physics simulator.
|
103 |
+
3. This target spectrum is handed to an AI **CodeAgent**, powered by GPT-4.1-mini, along with access to callable tools.
|
104 |
+
4. The agent dynamically explores candidate layer permutations by invoking `simulate_spectrum(...)` to generate spectra, and `cosine_similarity(...)` to compare them to the target.
|
105 |
+
5. It loops over permutations, choosing what to simulate next based on feedback β and stops automatically when similarity exceeds a threshold (e.g., `> 0.999`).
|
106 |
+
6. The final output includes the matched material order, a reasoning trace, and optionally a spectrum comparison plot.
|
107 |
""")
|
108 |
+
|
109 |
+
gr.Markdown("### π Transmission Spectra of Layer Stack Designs for all 24 permutations of material order")
|
110 |
gr.Image(value="121_resized.png", interactive=False)
|
|
|
111 |
gr.Markdown("""
|
112 |
+
|
113 |
+
### π οΈ Tools available to the Agent:
|
114 |
|
115 |
| Tool | Description | Arguments |
|
116 |
|--------------------|--------------------------------------------|-----------------------------------------|
|
117 |
| `simulate_spectrum`| Simulates optical spectrum | `layer_order`: List of materials |
|
118 |
| `cosine_similarity`| Compares predicted vs target spectra | `vec1`, `vec2`: Lists of floats |
|
119 |
| `final_answer` | Emits final result with justification | `answer`: any |
|
120 |
+
|
121 |
+
> π§ͺ This is not a single tool call to the Physics Solver (RCWA) β the agent uses simulation as feedback to drive tool selection and termination.
|
122 |
""")
|
123 |
|
124 |
+
|
125 |
+
gr.Markdown("# π§ͺ Optical Spectrum Matching Agent (Streaming UI Style)")
|
126 |
run_btn = gr.Button("π Run Agent on Random Stack")
|
127 |
true_order = gr.Textbox(label="True Material Order")
|
128 |
prompt_box = gr.Textbox(label="Agent Prompt")
|
|
|
139 |
return
|
140 |
|
141 |
prompt = f"""You are an AI agent that uses tools to simulate optical spectra and compare them to a target spectrum.
|
142 |
+
You must find a 4-layer permutation from this list: [Si, Si3N4, SiO2, AlN]. Use each material only once.
|
143 |
+
Use simulate_spectrum(order, thicknesses) to simulate and cosine_similarity(predicted, target) to compare.
|
144 |
Stop when similarity > 0.999. Report number of permutations tried.
|
145 |
Target spectrum: {target_val}
|
146 |
"""
|