Spaces:
Running
Running
alan
commited on
Commit
·
2c42a14
1
Parent(s):
5a6f12c
init
Browse files- app.py +140 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
from openai import OpenAI
|
4 |
+
|
5 |
+
# ----------------------- CONSTANTS ----------------------- #
|
6 |
+
SYSTEM_PROMPT = """
|
7 |
+
Given the research context, design an ablation study for the specified module or process.
|
8 |
+
Begin the design with a clear statement of the research objective, followed by a detailed description of the experiment setup.
|
9 |
+
Do not include the discussion of results or conclusions in the response, as the focus is solely on the experimental design.
|
10 |
+
The response should be within 300 words. Present the response in **Markdown** format (use headings, bold text, and bullet or numbered lists where appropriate).
|
11 |
+
""".strip()
|
12 |
+
|
13 |
+
# ----------------------- HELPERS ------------------------- #
|
14 |
+
|
15 |
+
def prepare_user_prompt(
|
16 |
+
research_background: str,
|
17 |
+
method: str,
|
18 |
+
experiment_setup: str,
|
19 |
+
experiment_results: str,
|
20 |
+
module_name: str,
|
21 |
+
) -> str:
|
22 |
+
"""Craft the ‘user’ portion of the OpenAI chat based on form inputs."""
|
23 |
+
research_background_block = f"### Research Background\n{research_background}\n"
|
24 |
+
method_block = f"### Method Section\n{method}\n"
|
25 |
+
experiment_block = (
|
26 |
+
"### Main Experiment Setup\n"
|
27 |
+
f"{experiment_setup}\n\n"
|
28 |
+
"### Main Experiment Results\n"
|
29 |
+
f"{experiment_results}\n"
|
30 |
+
)
|
31 |
+
|
32 |
+
return (
|
33 |
+
"## Research Context\n"
|
34 |
+
f"{research_background_block}{method_block}{experiment_block}\n\n"
|
35 |
+
f"Design an **ablation study** about **{module_name}** based on the research context above."
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
def generate_ablation_design(
|
40 |
+
research_background,
|
41 |
+
method,
|
42 |
+
experiment_setup,
|
43 |
+
experiment_results,
|
44 |
+
module_name,
|
45 |
+
api_key,
|
46 |
+
):
|
47 |
+
"""Combine inputs ➜ call OpenAI ➜ return the ablation‑study design text (Markdown)."""
|
48 |
+
# 1) Validate the API key format.
|
49 |
+
if not api_key or not api_key.startswith("sk-"):
|
50 |
+
return "❌ **Please enter a valid OpenAI API key in the textbox above.**"
|
51 |
+
|
52 |
+
# 2) Build the chat conversation payload.
|
53 |
+
messages = [
|
54 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
55 |
+
{
|
56 |
+
"role": "user",
|
57 |
+
"content": prepare_user_prompt(
|
58 |
+
research_background,
|
59 |
+
method,
|
60 |
+
experiment_setup,
|
61 |
+
experiment_results,
|
62 |
+
module_name,
|
63 |
+
),
|
64 |
+
},
|
65 |
+
]
|
66 |
+
|
67 |
+
# 3) Call the model and return the assistant response.
|
68 |
+
client = OpenAI(api_key=api_key)
|
69 |
+
try:
|
70 |
+
response = client.chat.completions.create(
|
71 |
+
model="gpt-4.1",
|
72 |
+
messages=messages,
|
73 |
+
max_tokens=2048,
|
74 |
+
temperature=1,
|
75 |
+
)
|
76 |
+
return response.choices[0].message.content.strip()
|
77 |
+
except Exception as e:
|
78 |
+
return f"⚠️ **OpenAI error:** {e}"
|
79 |
+
|
80 |
+
# ----------------------- UI LAYOUT ----------------------- #
|
81 |
+
with gr.Blocks(title="Ablation Study Designer") as demo:
|
82 |
+
# Main two‑column layout.
|
83 |
+
with gr.Row():
|
84 |
+
# ---------- LEFT COLUMN: INPUTS ----------
|
85 |
+
with gr.Column(scale=1):
|
86 |
+
gr.Markdown(
|
87 |
+
"""
|
88 |
+
# 🧪 Ablation Study Designer
|
89 |
+
Fill in the study details below, then click **Generate** to receive a tailored ablation‑study design rendered in Markdown.
|
90 |
+
""",
|
91 |
+
elem_id="header",
|
92 |
+
)
|
93 |
+
|
94 |
+
# API‑key field (required)
|
95 |
+
api_key = gr.Textbox(
|
96 |
+
label="🔑 OpenAI API Key (required)",
|
97 |
+
type="password",
|
98 |
+
placeholder="sk-...",
|
99 |
+
)
|
100 |
+
|
101 |
+
research_background = gr.Textbox(
|
102 |
+
label="Research Background", lines=6, placeholder="Describe the broader research context…"
|
103 |
+
)
|
104 |
+
method = gr.Textbox(
|
105 |
+
label="Method Description", lines=6, placeholder="Summarize the method section…"
|
106 |
+
)
|
107 |
+
experiment_setup = gr.Textbox(
|
108 |
+
label="Main Experiment – Setup", lines=6, placeholder="Datasets, hyper‑parameters, etc."
|
109 |
+
)
|
110 |
+
experiment_results = gr.Textbox(
|
111 |
+
label="Main Experiment – Results", lines=6, placeholder="Key quantitative or qualitative findings…"
|
112 |
+
)
|
113 |
+
module_name = gr.Textbox(
|
114 |
+
label="Module / Process for Ablation", placeholder="e.g., Attention mechanism"
|
115 |
+
)
|
116 |
+
|
117 |
+
generate_btn = gr.Button("Generate Ablation Study Design")
|
118 |
+
|
119 |
+
# ---------- RIGHT COLUMN: OUTPUT ----------
|
120 |
+
with gr.Column(scale=1):
|
121 |
+
output_md = gr.Markdown(value="", label="Ablation Study Design")
|
122 |
+
|
123 |
+
# Button click: trigger generation with a loading indicator.
|
124 |
+
generate_btn.click(
|
125 |
+
fn=generate_ablation_design,
|
126 |
+
inputs=[
|
127 |
+
research_background,
|
128 |
+
method,
|
129 |
+
experiment_setup,
|
130 |
+
experiment_results,
|
131 |
+
module_name,
|
132 |
+
api_key,
|
133 |
+
],
|
134 |
+
outputs=output_md,
|
135 |
+
show_progress="full", # Display a full‑screen progress bar.
|
136 |
+
)
|
137 |
+
|
138 |
+
# ----------------------- LAUNCH -------------------------- #
|
139 |
+
if __name__ == "__main__":
|
140 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
openai
|