yilunzhao commited on
Commit
82f8f5f
·
verified ·
1 Parent(s): 31ddcfe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.\nBegin the design with a clear statement of the research objective, followed by a detailed description of the experiment setup.\nDo not include the discussion of results or conclusions in the response, as the focus is solely on the experimental design.\nThe response should be within 300 words. Present the response in **Markdown** format (use headings, bold text, and bullet or numbered lists where appropriate).
8
+ """.strip()
9
+
10
+ # ----------------------- HELPERS ------------------------- #
11
+
12
+ def prepare_user_prompt(
13
+ research_background: str,
14
+ method: str,
15
+ experiment_setup: str,
16
+ experiment_results: str,
17
+ module_name: str,
18
+ ) -> str:
19
+ """Craft the ‘user’ portion of the OpenAI chat based on form inputs."""
20
+ research_background_block = f"### Research Background\n{research_background}\n"
21
+ method_block = f"### Method Section\n{method}\n"
22
+ experiment_block = (
23
+ "### Main Experiment Setup\n"
24
+ f"{experiment_setup}\n\n"
25
+ "### Main Experiment Results\n"
26
+ f"{experiment_results}\n"
27
+ )
28
+
29
+ return (
30
+ "## Research Context\n"
31
+ f"{research_background_block}{method_block}{experiment_block}\n\n"
32
+ f"Design an **ablation study** about **{module_name}** based on the research context above."
33
+ )
34
+
35
+
36
+ def generate_ablation_design(
37
+ research_background,
38
+ method,
39
+ experiment_setup,
40
+ experiment_results,
41
+ module_name,
42
+ api_key,
43
+ ):
44
+ """Combine inputs ➜ call OpenAI ➜ return the ablation-study design text (Markdown)."""
45
+ # 1 ) validate the API key
46
+ if not api_key or not api_key.startswith("sk-"):
47
+ return "❌ **Please enter a valid OpenAI API key in the textbox above.**"
48
+
49
+ # 2 ) build the chat conversation
50
+ messages = [
51
+ {"role": "system", "content": SYSTEM_PROMPT},
52
+ {
53
+ "role": "user",
54
+ "content": prepare_user_prompt(
55
+ research_background,
56
+ method,
57
+ experiment_setup,
58
+ experiment_results,
59
+ module_name,
60
+ ),
61
+ },
62
+ ]
63
+
64
+ # 3 ) call the model
65
+ client = OpenAI(api_key=api_key)
66
+ try:
67
+ response = client.chat.completions.create(
68
+ model="gpt-4o-mini",
69
+ messages=messages,
70
+ max_tokens=2048,
71
+ temperature=1,
72
+ )
73
+ return response.choices[0].message.content.strip()
74
+ except Exception as e:
75
+ return f"⚠️ **OpenAI error:** {e}"
76
+
77
+ # ----------------------- UI LAYOUT ----------------------- #
78
+ with gr.Blocks(title="Ablation Study Designer") as demo:
79
+ gr.Markdown(
80
+ """
81
+ # 🧪 Ablation Study Designer \n
82
+ Supply your study details below, then click **Generate** to receive a tailored ablation-study design rendered in Markdown.
83
+ """
84
+ )
85
+
86
+ # API-key field (required)
87
+ api_key = gr.Textbox(
88
+ label="🔑 OpenAI API Key (required)",
89
+ type="password",
90
+ placeholder="sk-...",
91
+ )
92
+
93
+ research_background = gr.Textbox(
94
+ label="Research Background", lines=6, placeholder="Describe the broader research context…"
95
+ )
96
+ method = gr.Textbox(
97
+ label="Method Description", lines=6, placeholder="Summarize the method section…"
98
+ )
99
+ experiment_setup = gr.Textbox(
100
+ label="Main Experiment – Setup", lines=6, placeholder="Datasets, hyper-parameters, etc."
101
+ )
102
+ experiment_results = gr.Textbox(
103
+ label="Main Experiment – Results", lines=6, placeholder="Key quantitative or qualitative findings…"
104
+ )
105
+ module_name = gr.Textbox(
106
+ label="Module / Process for Ablation", placeholder="e.g., Attention mechanism"
107
+ )
108
+
109
+ generate_btn = gr.Button("Generate Ablation Study Design")
110
+
111
+ # Markdown output area — dynamic, renders Markdown
112
+ output_md = gr.Markdown(value="", label="Ablation Study Design")
113
+
114
+ generate_btn.click(
115
+ fn=generate_ablation_design,
116
+ inputs=[
117
+ research_background,
118
+ method,
119
+ experiment_setup,
120
+ experiment_results,
121
+ module_name,
122
+ api_key,
123
+ ],
124
+ outputs=output_md,
125
+ )
126
+
127
+ # ----------------------- LAUNCH -------------------------- #
128
+ if __name__ == "__main__":
129
+ demo.launch()