Commit
·
1aca019
1
Parent(s):
31622fc
commit
Browse files
app.py
CHANGED
@@ -44,7 +44,7 @@ Requirements:
|
|
44 |
|
45 |
Output format: Return the complete prompt with frontmatter and system prompt as plain text with no markdown formatting or code fences."""
|
46 |
|
47 |
-
def reformat_prompt(api_key: str, rough_draft: str, prompt_type: str = "standard") -> str:
|
48 |
"""
|
49 |
Reformat a rough draft into a polished system prompt using OpenAI API.
|
50 |
|
@@ -52,22 +52,26 @@ def reformat_prompt(api_key: str, rough_draft: str, prompt_type: str = "standard
|
|
52 |
api_key: OpenAI API key
|
53 |
rough_draft: The rough draft text to be reformatted
|
54 |
prompt_type: Type of prompt to generate ("standard" or "claude_code")
|
|
|
55 |
|
56 |
Returns:
|
57 |
-
|
58 |
"""
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
|
62 |
if not rough_draft.strip():
|
63 |
-
return "Error: Please provide a rough draft to reformat."
|
64 |
|
65 |
# Select the appropriate system prompt based on type
|
66 |
selected_system_prompt = CLAUDE_CODE_SYSTEM_PROMPT if prompt_type == "claude_code" else SYSTEM_PROMPT
|
67 |
|
68 |
try:
|
69 |
-
# Initialize OpenAI client with the
|
70 |
-
client = openai.OpenAI(api_key=
|
71 |
|
72 |
# Make API call to reformat the prompt
|
73 |
response = client.chat.completions.create(
|
@@ -81,39 +85,20 @@ def reformat_prompt(api_key: str, rough_draft: str, prompt_type: str = "standard
|
|
81 |
)
|
82 |
|
83 |
reformatted_text = response.choices[0].message.content
|
84 |
-
return reformatted_text
|
85 |
|
86 |
except openai.AuthenticationError:
|
87 |
-
return "Error: Invalid API key. Please check your OpenAI API key."
|
88 |
except openai.RateLimitError:
|
89 |
-
return "Error: Rate limit exceeded. Please try again later."
|
90 |
except openai.APIError as e:
|
91 |
-
return f"Error: OpenAI API error - {str(e)}"
|
92 |
except Exception as e:
|
93 |
-
return f"Error: {str(e)}"
|
94 |
|
95 |
def create_interface():
|
96 |
"""Create and configure the Gradio interface."""
|
97 |
|
98 |
-
# JavaScript for localStorage API key persistence
|
99 |
-
js_code = """
|
100 |
-
function saveApiKey(api_key) {
|
101 |
-
if (api_key && api_key.trim()) {
|
102 |
-
localStorage.setItem('openai_api_key', api_key);
|
103 |
-
}
|
104 |
-
return api_key;
|
105 |
-
}
|
106 |
-
|
107 |
-
function loadApiKey() {
|
108 |
-
return localStorage.getItem('openai_api_key') || '';
|
109 |
-
}
|
110 |
-
|
111 |
-
function clearApiKey() {
|
112 |
-
localStorage.removeItem('openai_api_key');
|
113 |
-
return '';
|
114 |
-
}
|
115 |
-
"""
|
116 |
-
|
117 |
with gr.Blocks(
|
118 |
title="Code Gen Agent Prompt Assistant",
|
119 |
theme=gr.themes.Soft(),
|
@@ -148,8 +133,7 @@ def create_interface():
|
|
148 |
font-size: 0.9em;
|
149 |
color: #2d5a2d;
|
150 |
}
|
151 |
-
"""
|
152 |
-
js=js_code
|
153 |
) as interface:
|
154 |
|
155 |
gr.HTML("""
|
@@ -163,23 +147,16 @@ def create_interface():
|
|
163 |
with gr.TabItem("Prompt Reformatter"):
|
164 |
with gr.Row():
|
165 |
with gr.Column():
|
|
|
|
|
|
|
166 |
api_key_input = gr.Textbox(
|
167 |
label="OpenAI API Key",
|
168 |
placeholder="sk-...",
|
169 |
type="password",
|
170 |
-
info="Your API key
|
171 |
)
|
172 |
|
173 |
-
with gr.Row():
|
174 |
-
load_key_btn = gr.Button("Load Saved Key", variant="secondary", size="sm")
|
175 |
-
clear_key_btn = gr.Button("Clear Saved Key", variant="secondary", size="sm")
|
176 |
-
|
177 |
-
gr.HTML("""
|
178 |
-
<div class="persistence-info">
|
179 |
-
<strong>🔒 Privacy:</strong> Your API key is stored only in your browser's local storage and never sent to our servers.
|
180 |
-
</div>
|
181 |
-
""")
|
182 |
-
|
183 |
prompt_type_radio = gr.Radio(
|
184 |
choices=[
|
185 |
("Standard Multi-Agent Prompt", "standard"),
|
@@ -268,41 +245,14 @@ def create_interface():
|
|
268 |
# Event handlers
|
269 |
submit_btn.click(
|
270 |
fn=reformat_prompt,
|
271 |
-
inputs=[api_key_input, rough_draft_input, prompt_type_radio],
|
272 |
-
outputs=output,
|
273 |
show_progress=True
|
274 |
-
).then(
|
275 |
-
fn=None,
|
276 |
-
inputs=[api_key_input],
|
277 |
-
outputs=None,
|
278 |
-
js="(api_key) => saveApiKey(api_key)"
|
279 |
-
)
|
280 |
-
|
281 |
-
load_key_btn.click(
|
282 |
-
fn=None,
|
283 |
-
inputs=None,
|
284 |
-
outputs=[api_key_input],
|
285 |
-
js="() => loadApiKey()"
|
286 |
-
)
|
287 |
-
|
288 |
-
clear_key_btn.click(
|
289 |
-
fn=None,
|
290 |
-
inputs=None,
|
291 |
-
outputs=[api_key_input],
|
292 |
-
js="() => clearApiKey()"
|
293 |
)
|
294 |
|
295 |
clear_btn.click(
|
296 |
-
fn=lambda: ("", "standard", "", ""),
|
297 |
-
outputs=[api_key_input, prompt_type_radio, rough_draft_input, output]
|
298 |
-
)
|
299 |
-
|
300 |
-
# Auto-load API key on interface load
|
301 |
-
interface.load(
|
302 |
-
fn=None,
|
303 |
-
inputs=None,
|
304 |
-
outputs=[api_key_input],
|
305 |
-
js="() => loadApiKey()"
|
306 |
)
|
307 |
|
308 |
|
|
|
44 |
|
45 |
Output format: Return the complete prompt with frontmatter and system prompt as plain text with no markdown formatting or code fences."""
|
46 |
|
47 |
+
def reformat_prompt(api_key: str, rough_draft: str, prompt_type: str = "standard", api_key_state: str = "") -> tuple[str, str]:
|
48 |
"""
|
49 |
Reformat a rough draft into a polished system prompt using OpenAI API.
|
50 |
|
|
|
52 |
api_key: OpenAI API key
|
53 |
rough_draft: The rough draft text to be reformatted
|
54 |
prompt_type: Type of prompt to generate ("standard" or "claude_code")
|
55 |
+
api_key_state: State variable for API key persistence
|
56 |
|
57 |
Returns:
|
58 |
+
Tuple of (reformatted system prompt or error message, updated api_key_state)
|
59 |
"""
|
60 |
+
# Use provided API key or fall back to state
|
61 |
+
effective_api_key = api_key.strip() or api_key_state.strip()
|
62 |
+
|
63 |
+
if not effective_api_key:
|
64 |
+
return "Error: Please provide your OpenAI API key.", api_key_state
|
65 |
|
66 |
if not rough_draft.strip():
|
67 |
+
return "Error: Please provide a rough draft to reformat.", effective_api_key
|
68 |
|
69 |
# Select the appropriate system prompt based on type
|
70 |
selected_system_prompt = CLAUDE_CODE_SYSTEM_PROMPT if prompt_type == "claude_code" else SYSTEM_PROMPT
|
71 |
|
72 |
try:
|
73 |
+
# Initialize OpenAI client with the effective API key
|
74 |
+
client = openai.OpenAI(api_key=effective_api_key)
|
75 |
|
76 |
# Make API call to reformat the prompt
|
77 |
response = client.chat.completions.create(
|
|
|
85 |
)
|
86 |
|
87 |
reformatted_text = response.choices[0].message.content
|
88 |
+
return reformatted_text, effective_api_key
|
89 |
|
90 |
except openai.AuthenticationError:
|
91 |
+
return "Error: Invalid API key. Please check your OpenAI API key.", api_key_state
|
92 |
except openai.RateLimitError:
|
93 |
+
return "Error: Rate limit exceeded. Please try again later.", effective_api_key
|
94 |
except openai.APIError as e:
|
95 |
+
return f"Error: OpenAI API error - {str(e)}", effective_api_key
|
96 |
except Exception as e:
|
97 |
+
return f"Error: {str(e)}", effective_api_key
|
98 |
|
99 |
def create_interface():
|
100 |
"""Create and configure the Gradio interface."""
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
with gr.Blocks(
|
103 |
title="Code Gen Agent Prompt Assistant",
|
104 |
theme=gr.themes.Soft(),
|
|
|
133 |
font-size: 0.9em;
|
134 |
color: #2d5a2d;
|
135 |
}
|
136 |
+
"""
|
|
|
137 |
) as interface:
|
138 |
|
139 |
gr.HTML("""
|
|
|
147 |
with gr.TabItem("Prompt Reformatter"):
|
148 |
with gr.Row():
|
149 |
with gr.Column():
|
150 |
+
# Create a state variable for API key persistence
|
151 |
+
api_key_state = gr.State("")
|
152 |
+
|
153 |
api_key_input = gr.Textbox(
|
154 |
label="OpenAI API Key",
|
155 |
placeholder="sk-...",
|
156 |
type="password",
|
157 |
+
info="Your API key will be remembered during this session"
|
158 |
)
|
159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
prompt_type_radio = gr.Radio(
|
161 |
choices=[
|
162 |
("Standard Multi-Agent Prompt", "standard"),
|
|
|
245 |
# Event handlers
|
246 |
submit_btn.click(
|
247 |
fn=reformat_prompt,
|
248 |
+
inputs=[api_key_input, rough_draft_input, prompt_type_radio, api_key_state],
|
249 |
+
outputs=[output, api_key_state],
|
250 |
show_progress=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
)
|
252 |
|
253 |
clear_btn.click(
|
254 |
+
fn=lambda: ("", "standard", "", "", ""),
|
255 |
+
outputs=[api_key_input, prompt_type_radio, rough_draft_input, output, api_key_state]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
)
|
257 |
|
258 |
|