File size: 16,120 Bytes
0bb3006
a217992
ad2df9a
2e9f353
8100125
 
09cb397
 
4e9c5bd
 
 
 
09cb397
ad2df9a
09cb397
 
 
 
ad2df9a
 
09cb397
4e9c5bd
 
 
 
 
 
 
 
ad2df9a
 
 
 
 
 
 
 
09cb397
4e9c5bd
 
 
 
ad2df9a
4e9c5bd
ad2df9a
 
 
4e9c5bd
09cb397
 
ad2df9a
09cb397
 
 
 
 
ad2df9a
09cb397
 
ad2df9a
 
4e9c5bd
09cb397
 
 
 
 
4e9c5bd
 
 
 
ad2df9a
 
4e9c5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad2df9a
09cb397
 
ad2df9a
 
4e9c5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09cb397
 
 
 
 
4e9c5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09cb397
ad2df9a
09cb397
 
 
4e9c5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad2df9a
4e9c5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09cb397
8100125
ad2df9a
 
 
 
 
 
 
 
 
 
 
 
09cb397
 
ad2df9a
 
 
4e9c5bd
ad2df9a
 
 
 
 
 
 
 
 
 
 
09cb397
ad2df9a
 
09cb397
4e9c5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad2df9a
4e9c5bd
 
 
b3f3cb1
 
 
 
 
4e9c5bd
 
 
 
 
 
 
ad2df9a
09cb397
4e9c5bd
 
09cb397
 
ad2df9a
09cb397
b68c9a6
 
 
ad2df9a
 
 
09cb397
 
ad2df9a
09cb397
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import gradio as gr
from prompt_refiner import PromptRefiner
from variables import models, explanation_markdown, metaprompt_list, examples
from custom_css import custom_css

class GradioInterface:
  def __init__(self, prompt_refiner: PromptRefiner, custom_css):
      self.prompt_refiner = prompt_refiner
      # Set default model to second-to-last in the list
      default_model = models[-1] if len(models) >= 1 else models[0] if models else None
      #meta_prompt_choice=metaprompt_list[0]
      
      with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as self.interface:
          # CONTAINER 1
          with gr.Column(elem_classes=["container", "title-container"]):
              gr.Markdown("# PROMPT++")
              gr.Markdown("### Automating Prompt Engineering by Refining your Prompts")
              gr.Markdown("Learn how to generate an improved version of your prompts.")
              
          # CONTAINER 2
          with gr.Column(elem_classes=["container", "input-container"]):
              prompt_text = gr.Textbox(label="Type your prompt (or leave empty to see metaprompt)",lines=5)
              with gr.Accordion("Prompt Examples", open=False, visible=True):     
                  gr.Examples(examples=examples,inputs=[prompt_text]) 
              automatic_metaprompt_button = gr.Button(
                  "Automatic Choice for Refinement Method",
                  elem_classes=["button-highlight"]
              )
              MetaPrompt_analysis = gr.Markdown()
              
          # CONTAINER 3    
          with gr.Column(elem_classes=["container","meta-container"]):
              meta_prompt_choice = gr.Radio(
                  choices=metaprompt_list,
                  label="Choose Meta Prompt",
                  value=metaprompt_list[0],
                  elem_classes=["no-background", "radio-group"]
              )
              refine_button = gr.Button(
                  "Refine Prompt",
                  elem_classes=["button-waiting"]
              )
              with gr.Accordion("Metaprompt Explanation", open=False, visible=True): 
                  gr.Markdown(explanation_markdown)              
              
          with gr.Column(elem_classes=["container", "analysis-container"]):           
              gr.Markdown(" ")
              prompt_evaluation = gr.Markdown()
              gr.Markdown("### Refined Prompt")
              refined_prompt = gr.Textbox(
                  label=" ",
                  interactive=True,
                  show_label=True,
                  show_copy_button=True,
              )
              explanation_of_refinements = gr.Markdown()

          with gr.Column(elem_classes=["container", "model-container"]):
              with gr.Row():
                  apply_model = gr.Dropdown(
                      choices=models,
                      value=default_model,
                      label="Choose the Model",
                      container=False,
                      scale=1,
                      min_width=300
                  )
                  apply_button = gr.Button(
                      "Apply Prompts",
                      elem_classes=["button-waiting"]
                  )
              
              gr.Markdown("### Prompts on Chosen Model")
              with gr.Tabs(elem_classes=["tabs"]):
                with gr.TabItem("Prompts Output Comparison", elem_classes=["tabitem"]):                     
                    with gr.Row(elem_classes=["output-row"]):
                        with gr.Column(scale=1, elem_classes=["comparison-column"]):
                              gr.Markdown("### Original Prompt Output")
                              original_output1 = gr.Markdown(
                           #       value="Output will appear here",
                                  elem_classes=["output-content"],
                                  visible=True
                              )
                        with gr.Column(scale=1, elem_classes=["comparison-column"]):
                              gr.Markdown("### Refined Prompt Output")
                              refined_output1 = gr.Markdown(
                             #     value="Output will appear here",
                                  elem_classes=["output-content"],
                                  visible=True
                              )
                with gr.TabItem("Original Prompt Output", elem_classes=["tabitem"]):
                      with gr.Row(elem_classes=["output-row"]):
                          with gr.Column(scale=1, elem_classes=["comparison-column"]):
                              gr.Markdown("### Original Prompt Output")
                              original_output = gr.Markdown(
                               #   value="Output will appear here",
                                  elem_classes=[ "output-content"],
                                  visible=True
                              )
                with gr.TabItem("Refined Prompt Output", elem_classes=["tabitem"]):
                      with gr.Row(elem_classes=["output-row"]):
                          with gr.Column(scale=1, elem_classes=["comparison-column"]):
                              gr.Markdown("### Refined Prompt Output")
                              refined_output = gr.Markdown(
                              #    value="Output will appear here",
                                  elem_classes=["output-content"],
                                  visible=True
                              )
              
              with gr.Accordion("Full Response JSON", open=False, visible=True):
                  full_response_json = gr.JSON()

          # Button click handlers
          automatic_metaprompt_button.click(
              fn=self.automatic_metaprompt,
              inputs=[prompt_text],
              outputs=[MetaPrompt_analysis, meta_prompt_choice]
          ).then(
              fn=lambda: None,
              inputs=None,
              outputs=None,
              js="""
                  () => {
                      // Clear subsequent outputs
                      document.querySelectorAll('.analysis-container textarea, .analysis-container .markdown-text, .model-container .markdown-text, .comparison-output').forEach(el => {
                          if (el.value !== undefined) {
                              el.value = '';
                          } else {
                              el.textContent = '';
                          }
                      });
                      
                      // Update button states
                      const allButtons = Array.from(document.querySelectorAll('button')).filter(btn => 
                          btn.textContent.includes('Automatic Choice') || 
                          btn.textContent.includes('Refine Prompt') || 
                          btn.textContent.includes('Apply Prompts')
                      );
                      allButtons.forEach(btn => btn.classList.remove('button-highlight'));
                      allButtons[1].classList.add('button-highlight'); // Highlight refine button
                      allButtons[0].classList.add('button-completed'); // Complete current button
                      allButtons[2].classList.add('button-waiting'); // Set apply button to waiting
                  }
              """
          )
          
          refine_button.click(
              fn=self.refine_prompt,
              inputs=[prompt_text, meta_prompt_choice],
              outputs=[prompt_evaluation, refined_prompt, explanation_of_refinements, full_response_json]
          ).then(
              fn=lambda: None,
              inputs=None,
              outputs=None,
              js="""
                  () => {
                      // Clear model outputs
                      document.querySelectorAll('.model-container .markdown-text, .comparison-output').forEach(el => {
                          if (el.value !== undefined) {
                              el.value = '';
                          } else {
                              el.textContent = '';
                          }
                      });
                      
                      // Update button states
                      const allButtons = Array.from(document.querySelectorAll('button')).filter(btn => 
                          btn.textContent.includes('Automatic Choice') || 
                          btn.textContent.includes('Refine Prompt') || 
                          btn.textContent.includes('Apply Prompts')
                      );
                      allButtons.forEach(btn => btn.classList.remove('button-highlight'));
                      allButtons[2].classList.add('button-highlight'); // Highlight apply button
                      allButtons[1].classList.add('button-completed'); // Complete current button
                      allButtons[2].classList.remove('button-waiting'); // Remove waiting from apply button
                  }
              """
          )
          
          apply_button.click(
              fn=self.apply_prompts,
              inputs=[prompt_text, refined_prompt, apply_model],
              outputs=[original_output, refined_output, original_output1, refined_output1],
              show_progress=True  # Add this line
            ).then(
              fn=lambda: None,
              inputs=None,
              outputs=None,
              js="""
                  () => {
                      // Update button states
                      const allButtons = Array.from(document.querySelectorAll('button')).filter(btn => 
                          btn.textContent.includes('Automatic Choice') || 
                          btn.textContent.includes('Refine Prompt') || 
                          btn.textContent.includes('Apply Prompts')
                      );
                      allButtons.forEach(btn => btn.classList.remove('button-highlight', 'button-waiting'));
                      allButtons[2].classList.add('button-completed'); // Complete apply button
                      
                      // Force refresh of output containers
                      document.querySelectorAll('.comparison-output').forEach(el => {
                          if (el.parentElement) {
                              el.parentElement.style.display = 'none';
                              setTimeout(() => {
                                  el.parentElement.style.display = 'block';
                              }, 100);
                          }
                      });
                  }
              """
            )

          # Reset when input changes
          prompt_text.change(
              fn=lambda: None,
              inputs=None,
              outputs=None,
              js="""
                  () => {
                      // Clear all outputs
                      document.querySelectorAll('.analysis-container textarea, .analysis-container .markdown-text, .model-container .markdown-text, .comparison-output').forEach(el => {
                          if (el.value !== undefined) {
                              el.value = '';
                          } else {
                              el.textContent = '';
                          }
                      });
                      
                      // Reset all button states
                      const allButtons = Array.from(document.querySelectorAll('button')).filter(btn => 
                          btn.textContent.includes('Automatic Choice') || 
                          btn.textContent.includes('Refine Prompt') || 
                          btn.textContent.includes('Apply Prompts')
                      );
                      allButtons.forEach(btn => {
                          btn.classList.remove('button-completed', 'button-highlight', 'button-waiting');
                      });
                      allButtons[0].classList.add('button-highlight'); // Highlight first button
                      allButtons.slice(1).forEach(btn => btn.classList.add('button-waiting')); // Set subsequent buttons to waiting
                  }
              """
          )

  def automatic_metaprompt(self, prompt: str) -> tuple:
      """Handle automatic metaprompt selection"""
      try:
          if not prompt.strip():
              return "Please enter a prompt to analyze.", None

          metaprompt_analysis, recommended_key = self.prompt_refiner.automatic_metaprompt(prompt)
          return metaprompt_analysis, recommended_key

      except Exception as e:
          error_message = f"Error in automatic metaprompt: {str(e)}"
          return error_message, None

  def refine_prompt(self, prompt: str, meta_prompt_choice: str) -> tuple:
      """Handle manual prompt refinement"""
      try:
          if not prompt.strip():
              return ("No prompt provided.", "", "", {})
    
          result = self.prompt_refiner.refine_prompt(prompt, meta_prompt_choice)
          return (
              result[0],  # initial_prompt_evaluation
              result[1],  # refined_prompt
              result[2],  # explanation_of_refinements
              result[3]   # full_response
          )
      except Exception as e:
          error_message = f"Error in refine_prompt: {str(e)}"
          return error_message, "", "", {}

  def apply_prompts(self, original_prompt: str, refined_prompt: str, model: str) -> tuple:
      """Apply both original and refined prompts to the selected model"""
      try:
          if not original_prompt or not refined_prompt:
              return ("Please provide both original and refined prompts.", 
                      "Please provide both original and refined prompts.",
                      "Please provide both original and refined prompts.",
                      "Please provide both original and refined prompts.")

          if not model:
              return ("Please select a model.", 
                      "Please select a model.",
                      "Please select a model.",
                      "Please select a model.")

          # Apply prompts and get outputs
          try:
            #  print(original_prompt)
             # print(refined_prompt)
              #print(model)
              
              original_output = self.prompt_refiner.apply_prompt(original_prompt, model)
              #print(original_output)
              refined_output = self.prompt_refiner.apply_prompt(refined_prompt, model)
          except Exception as e:
              return (f"Error applying prompts: {str(e)}", 
                      f"Error applying prompts: {str(e)}",
                      f"Error applying prompts: {str(e)}",
                      f"Error applying prompts: {str(e)}")

          # Ensure we have string outputs
          original_output = str(original_output) if original_output is not None else "No output generated"
          refined_output = str(refined_output) if refined_output is not None else "No output generated"
          #print('-'*100)
          #print(original_output)
          #print('-'*100)
          #print(refined_output)
          #print('-'*100)
          
          return (
              original_output,  # For Original Prompt Output tab
              refined_output,   # For Refined Prompt Output tab
              original_output,  # For Comparison tab - original
              refined_output    # For Comparison tab - refined
          )
          
      except Exception as e:
          error_message = f"Error in apply_prompts: {str(e)}"
          return (error_message, error_message, error_message, error_message)

  def launch(self, share=False):
      """Launch the Gradio interface"""
      self.interface.launch(share=share)


if __name__ == '__main__':
  from variables import api_token, meta_prompts, metaprompt_explanations
  
  # Initialize the prompt refiner
  prompt_refiner = PromptRefiner(api_token, meta_prompts, metaprompt_explanations)
  
  # Create and launch the Gradio interface
  gradio_interface = GradioInterface(prompt_refiner, custom_css)
  gradio_interface.launch(share=True)