yxmnjxzx commited on
Commit
951a6c9
·
verified ·
1 Parent(s): 9447f3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +252 -68
app.py CHANGED
@@ -113,95 +113,270 @@ class PromptRefiner:
113
  class GradioInterface:
114
  def __init__(self, prompt_refiner: PromptRefiner):
115
  self.prompt_refiner = prompt_refiner
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
- with gr.Blocks() as self.interface:
118
- gr.Markdown("# PROMPT++")
119
- gr.Markdown("### Automating Prompt Engineering by Refining your Prompts")
120
- gr.Markdown("Learn how to generate an improved version of your prompts. Enter a main idea for a prompt, choose a meta prompt, and the model will attempt to generate an improved version.")
121
-
122
- gr.Markdown("## Refine Prompt")
123
- with gr.Row():
124
- prompt_text = gr.Textbox(label="Type the prompt (or let it empty to see metaprompt)")
125
- with gr.Accordion("Meta Prompt explanation", open=False):
126
- gr.Markdown(explanation_markdown)
127
- with gr.Row():
128
- meta_prompt_choice = gr.Radio(["superstar", "star", "done", "physics", "morphosis", "verse", "phor","bolism","math","math_meta"], label="Choose Meta Prompt", value="morphosis")
129
- refine_button = gr.Button("Refine Prompt")
130
- with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  gr.Markdown("### Initial prompt analysis")
132
- with gr.Column():
133
- analysis_evaluation = gr.Markdown(label="Analysis and Evaluation")
134
  gr.Markdown("### Refined Prompt")
135
- refined_prompt = gr.Textbox(label="Refined Prompt")
 
 
 
 
 
 
136
  gr.Markdown("### Explanation of Refinements")
137
- explanation_of_refinements = gr.Markdown(label="Explanation of Refinements")
138
 
139
- with gr.Accordion("Full Response JSON", open=False,visible=False):
140
- full_response_json = gr.JSON()
141
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
 
 
 
 
 
 
 
 
 
 
143
  refine_button.click(
144
  fn=self.refine_prompt,
145
  inputs=[prompt_text, meta_prompt_choice],
146
  outputs=[analysis_evaluation, refined_prompt, explanation_of_refinements, full_response_json]
147
  )
148
- gr.Markdown("## See MetaPrompt Impact")
149
- with gr.Row():
150
- apply_model = gr.Dropdown(
151
- [
152
- "llama-3.1-70b-versatile",
153
- "llama3-groq-70b-8192-tool-use-preview",
154
- "llama-3.2-90b-text-preview",
155
- "llama-3.2-90b-vision-preview",
156
- "llama3-groq-70b-8192-tool-use-preview",
157
- "mixtral-8x7b-32768"
158
- ],
159
- value="llama-3.1-70b-versatile",
160
- label="Choose the Model to apply prompts"
161
- )
162
- # apply_model=gr.Dropdown(["gpt-4o",'gpt-4-turbo'], value="gpt-4o", label="Model"),
163
- apply_button = gr.Button("Apply MetaPrompt")
164
-
165
- with gr.Tab("Original Prompt Output"):
166
- # gr.Markdown("### Original Prompt Output")
167
- original_output = gr.Markdown(label="Original Prompt Output")
168
- with gr.Tab("Refined Prompt Output"):
169
- #gr.Markdown("### Refined Prompt Output")
170
- refined_output = gr.Markdown(label="Refined Prompt Output")
171
 
172
  apply_button.click(
173
  fn=self.apply_prompts,
174
  inputs=[prompt_text, refined_prompt, apply_model],
175
- outputs=[original_output, refined_output]
176
  )
177
 
178
- with gr.Accordion("Examples", open=True):
179
- gr.Examples(
180
- examples=[
181
- ["Write a story on the end of prompt engineering replaced by an Ai specialized in refining prompts.", "star"],
182
- ["Tell me about that guy who invented the light bulb", "physics"],
183
- ["Explain the universe.", "star"],
184
- ["What's the population of New York City and how tall is the Empire State Building and who was the first mayor?", "morphosis"],
185
- ["List American presidents.", "verse"],
186
- ["Explain why the experiment failed.", "morphosis"],
187
- ["Is nuclear energy good?", "verse"],
188
- ["How does a computer work?", "phor"],
189
- ["How to make money fast?", "done"],
190
- ["how can you prove IT0's lemma in stochastic calculus ?", "superstar"],
191
- ],
192
- inputs=[prompt_text, meta_prompt_choice]
193
- )
194
-
195
-
196
  def refine_prompt(self, prompt: str, meta_prompt_choice: str) -> tuple:
197
  input_data = PromptInput(text=prompt, meta_prompt_choice=meta_prompt_choice)
198
- result = self.prompt_refiner.refine_prompt(input_data)
199
- analysis_evaluation = f"\n\n{result.initial_prompt_evaluation}"
 
 
200
  return (
201
  analysis_evaluation,
202
- result.refined_prompt,
203
- result.explanation_of_refinements,
204
- result.dict()
205
  )
206
 
207
  def apply_prompts(self, original_prompt: str, refined_prompt: str, model: str):
@@ -211,7 +386,7 @@ class GradioInterface:
211
 
212
  def launch(self, share=False):
213
  self.interface.launch(share=share)
214
-
215
  metaprompt_explanations = {
216
  "star": "Use ECHO when you need a comprehensive, multi-stage approach for complex prompts. It's ideal for tasks requiring in-depth analysis, exploration of multiple alternatives, and synthesis of ideas. Choose this over others when you have time for a thorough refinement process and need to consider various aspects of the prompt.",
217
  "superstar": "Use advanced ECHO when you need a comprehensive, multi-stage approach for complex prompts. It's ideal for tasks requiring in-depth analysis, exploration of multiple alternatives, and synthesis of ideas. Choose this over others when you have time for a thorough refinement process and need to consider various aspects of the prompt.",
@@ -224,6 +399,15 @@ metaprompt_explanations = {
224
  "math": "Apply advanced mathematical and logical reasoning techniques to refine the given prompt, ensuring clarity, specificity, and effectiveness. It's best for prompts that deal with math."
225
  }
226
 
 
 
 
 
 
 
 
 
 
227
  explanation_markdown = "".join([f"- **{key}**: {value}\n" for key, value in metaprompt_explanations.items()])
228
 
229
  # Main code to run the application
 
113
  class GradioInterface:
114
  def __init__(self, prompt_refiner: PromptRefiner):
115
  self.prompt_refiner = prompt_refiner
116
+ custom_css = """
117
+ .container {
118
+ border: 2px solid #2196F3;
119
+ border-radius: 10px;
120
+ padding: 12px;
121
+ margin: 6px;
122
+ background: white;
123
+ position: relative;
124
+ width: 100% !important;
125
+ max-width: 1200px !important;
126
+ margin: 0 auto 20px auto !important;
127
+ }
128
+ .container::before {
129
+ position: absolute;
130
+ top: -10px;
131
+ left: 20px;
132
+ background: white;
133
+ padding: 0 10px;
134
+ color: #2196F3;
135
+ font-weight: bold;
136
+ font-size: 1.2em;
137
+ }
138
+ .title-container {
139
+ width: fit-content !important;
140
+ margin: 0 auto !important;
141
+ padding: 2px 40px !important;
142
+ border: 1px solid #0066cc !important;
143
+ border-radius: 10px !important;
144
+ background-color: rgba(0, 102, 204, 0.05) !important;
145
+ }
146
+ .title-container * {
147
+ text-align: center;
148
+ margin: 0 !important;
149
+ line-height: 1.2 !important;
150
+ }
151
+ .title-container h1 {
152
+ font-size: 28px !important;
153
+ margin-bottom: 1px !important;
154
+ }
155
+ .title-container h3 {
156
+ font-size: 18px !important;
157
+ margin-bottom: 1px !important;
158
+ }
159
+ .title-container p {
160
+ font-size: 14px !important;
161
+ margin-bottom: 1px !important;
162
+ }
163
+ .input-container::before {
164
+ content: 'PROMPT REFINEMENT';
165
+ }
166
+ .analysis-container::before {
167
+ content: 'ANALYSIS';
168
+ }
169
+ .model-container::before {
170
+ content: 'MODEL APPLICATION';
171
+ }
172
+ .examples-container::before {
173
+ content: 'EXAMPLES';
174
+ }
175
+ /* Resizable textbox */
176
+ .input-container textarea {
177
+ resize: vertical !important;
178
+ min-height: 100px !important;
179
+ max-height: 500px !important;
180
+ width: 100% !important;
181
+ border: 1px solid #ddd !important;
182
+ border-radius: 4px !important;
183
+ padding: 8px !important;
184
+ transition: all 0.3s ease !important;
185
+ }
186
+ .input-container textarea:focus {
187
+ border-color: #2196F3 !important;
188
+ box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.1) !important;
189
+ }
190
+ /* Radio group styling */
191
+ .radio-group {
192
+ background-color: rgba(0, 102, 204, 0.05) !important;
193
+ padding: 10px !important;
194
+ border-radius: 8px !important;
195
+ border: 1px solid rgba(0, 102, 204, 0.1) !important;
196
+ display: flex !important;
197
+ justify-content: center !important;
198
+ flex-wrap: wrap !important;
199
+ gap: 8px !important;
200
+ width: 100% !important;
201
+ }
202
+ .gradio-radio {
203
+ display: flex !important;
204
+ justify-content: center !important;
205
+ flex-wrap: wrap !important;
206
+ gap: 8px !important;
207
+ }
208
+ .gradio-radio label {
209
+ display: flex !important;
210
+ align-items: center !important;
211
+ padding: 6px 12px !important;
212
+ border: 1px solid #ddd !important;
213
+ border-radius: 4px !important;
214
+ cursor: pointer !important;
215
+ background: white !important;
216
+ margin: 4px !important;
217
+ }
218
+ .gradio-radio input[type="radio"]:checked + label {
219
+ background: rgba(0, 102, 204, 0.1) !important;
220
+ border-color: #0066cc !important;
221
+ color: #0066cc !important;
222
+ font-weight: bold !important;
223
+ }
224
+ /* Button styling */
225
+ .gradio-button {
226
+ background-color: white !important;
227
+ color: #2196F3 !important;
228
+ border: 2px solid #2196F3 !important;
229
+ border-radius: 4px !important;
230
+ padding: 8px 16px !important;
231
+ margin: 10px 0 !important;
232
+ font-weight: bold !important;
233
+ transition: all 0.3s ease !important;
234
+ }
235
+ .gradio-button:hover {
236
+ background-color: #2196F3 !important;
237
+ color: white !important;
238
+ box-shadow: 0 2px 5px rgba(33, 150, 243, 0.3) !important;
239
+ }
240
+ /* Accordion styling */
241
+ .gradio-accordion {
242
+ margin: 10px 0 !important;
243
+ border: none !important;
244
+ }
245
+ /* Container alignment */
246
+ .gradio-container {
247
+ display: flex !important;
248
+ flex-direction: column !important;
249
+ align-items: center !important;
250
+ width: 100% !important;
251
+ max-width: 1200px !important;
252
+ margin: 0 auto !important;
253
+ }
254
+ /* Dropdown styling */
255
+ .gradio-dropdown {
256
+ width: 100% !important;
257
+ max-width: 300px !important;
258
+ }
259
+ /* JSON container */
260
+ .full-response-json {
261
+ margin-top: 20px !important;
262
+ padding: 10px !important;
263
+ background-color: rgba(0, 102, 204, 0.05) !important;
264
+ border-radius: 8px !important;
265
+ }
266
+ """
267
 
268
+ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as self.interface:
269
+ with gr.Column(elem_classes=["container", "title-container"]):
270
+ gr.Markdown("# PROMPT++")
271
+ gr.Markdown("### Automating Prompt Engineering by Refining your Prompts")
272
+ gr.Markdown("Learn how to generate an improved version of your prompts.")
273
+
274
+ with gr.Column(elem_classes=["container", "input-container"]):
275
+ prompt_text = gr.Textbox(
276
+ label="Type your prompt (or let it empty to see metaprompt)",
277
+ # elem_classes="no-background",
278
+ #elem_classes="container2",
279
+ lines=5
280
+ )
281
+ meta_prompt_choice = gr.Radio(
282
+ ["superstar", "star", "done", "physics", "morphosis", "verse", "phor","bolism","math","math_meta"],
283
+ label="Choose Meta Prompt",
284
+ value="star",
285
+ elem_classes=["no-background", "radio-group"]
286
+ # elem_classes=[ "radio-group"]
287
+ )
288
+ refine_button = gr.Button("Refine Prompt")
289
+
290
+ # Option 1: Put Examples here (before Meta Prompt explanation)
291
+ with gr.Row(elem_classes=["container2"]):
292
+ with gr.Accordion("Examples", open=False):
293
+ gr.Examples(
294
+ examples=[
295
+ ["Write a story on the end of prompt engineering replaced by an Ai specialized in refining prompts.", "superstar"],
296
+ ["Tell me about that guy who invented the light bulb", "physics"],
297
+ ["Explain the universe.", "star"],
298
+ ["What's the population of New York City and how tall is the Empire State Building and who was the first mayor?", "morphosis"],
299
+ ["List American presidents.", "verse"],
300
+ ["Explain why the experiment failed.", "morphosis"],
301
+ ["Is nuclear energy good?", "verse"],
302
+ ["How does a computer work?", "phor"],
303
+ ["How to make money fast?", "done"],
304
+ ["how can you prove IT0's lemma in stochastic calculus ?", "math_meta"],
305
+ ],
306
+ inputs=[prompt_text, meta_prompt_choice]
307
+ )
308
+
309
+ with gr.Accordion("Meta Prompt explanation", open=False):
310
+ gr.Markdown(explanation_markdown)
311
+
312
+
313
+
314
+ # Option 2: Or put Examples here (after the button)
315
+ # with gr.Accordion("Examples", open=False):
316
+ # gr.Examples(...)
317
+
318
+ with gr.Column(elem_classes=["container", "analysis-container"]):
319
+ gr.Markdown(' ')
320
  gr.Markdown("### Initial prompt analysis")
321
+ analysis_evaluation = gr.Markdown()
 
322
  gr.Markdown("### Refined Prompt")
323
+ refined_prompt = gr.Textbox(
324
+ label="Refined Prompt",
325
+ interactive=True,
326
+ show_label=True, # Must be True for copy button to show
327
+ show_copy_button=True, # Adds the copy button
328
+ # elem_classes="no-background"
329
+ )
330
  gr.Markdown("### Explanation of Refinements")
331
+ explanation_of_refinements = gr.Markdown()
332
 
 
 
333
 
334
+ with gr.Column(elem_classes=["container", "model-container"]):
335
+ # gr.Markdown("## See MetaPrompt Impact")
336
+ with gr.Row():
337
+ apply_model = gr.Dropdown(models,
338
+ value="llama-3.1-70b-versatile",
339
+ label="Choose the Model",
340
+ container=False, # This removes the container around the dropdown
341
+ scale=1, # Controls the width relative to other components
342
+ min_width=300 # Sets minimum width in pixels
343
+ # elem_classes="no-background"
344
+ )
345
+ apply_button = gr.Button("Apply MetaPrompt")
346
 
347
+ # with gr.Column(elem_classes=["container", "results-container"]):
348
+ gr.Markdown("### Prompts on choosen model")
349
+ with gr.Tabs():
350
+ with gr.TabItem("Original Prompt Output"):
351
+ original_output = gr.Markdown()
352
+ with gr.TabItem("Refined Prompt Output"):
353
+ refined_output = gr.Markdown()
354
+ with gr.Accordion("Full Response JSON", open=False, visible=True):
355
+ full_response_json = gr.JSON()
356
+
357
  refine_button.click(
358
  fn=self.refine_prompt,
359
  inputs=[prompt_text, meta_prompt_choice],
360
  outputs=[analysis_evaluation, refined_prompt, explanation_of_refinements, full_response_json]
361
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
 
363
  apply_button.click(
364
  fn=self.apply_prompts,
365
  inputs=[prompt_text, refined_prompt, apply_model],
366
+ outputs=[original_output, refined_output]
367
  )
368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  def refine_prompt(self, prompt: str, meta_prompt_choice: str) -> tuple:
370
  input_data = PromptInput(text=prompt, meta_prompt_choice=meta_prompt_choice)
371
+ # Since result is a tuple with 4 elements based on the return value of prompt_refiner.refine_prompt
372
+ initial_prompt_evaluation, refined_prompt, explanation_refinements, full_response = self.prompt_refiner.refine_prompt(input_data)
373
+
374
+ analysis_evaluation = f"\n\n{initial_prompt_evaluation}"
375
  return (
376
  analysis_evaluation,
377
+ refined_prompt,
378
+ explanation_refinements,
379
+ full_response
380
  )
381
 
382
  def apply_prompts(self, original_prompt: str, refined_prompt: str, model: str):
 
386
 
387
  def launch(self, share=False):
388
  self.interface.launch(share=share)
389
+
390
  metaprompt_explanations = {
391
  "star": "Use ECHO when you need a comprehensive, multi-stage approach for complex prompts. It's ideal for tasks requiring in-depth analysis, exploration of multiple alternatives, and synthesis of ideas. Choose this over others when you have time for a thorough refinement process and need to consider various aspects of the prompt.",
392
  "superstar": "Use advanced ECHO when you need a comprehensive, multi-stage approach for complex prompts. It's ideal for tasks requiring in-depth analysis, exploration of multiple alternatives, and synthesis of ideas. Choose this over others when you have time for a thorough refinement process and need to consider various aspects of the prompt.",
 
399
  "math": "Apply advanced mathematical and logical reasoning techniques to refine the given prompt, ensuring clarity, specificity, and effectiveness. It's best for prompts that deal with math."
400
  }
401
 
402
+ models = [
403
+ "llama-3.1-70b-versatile",
404
+ "llama3-groq-70b-8192-tool-use-preview",
405
+ "llama-3.2-90b-text-preview",
406
+ "llama-3.2-90b-vision-preview",
407
+ "llama3-groq-70b-8192-tool-use-preview",
408
+ "mixtral-8x7b-32768"
409
+ ]
410
+
411
  explanation_markdown = "".join([f"- **{key}**: {value}\n" for key, value in metaprompt_explanations.items()])
412
 
413
  # Main code to run the application