Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from optimizer import optimize_prompt
|
| 3 |
+
|
| 4 |
+
def run_optimizer(prompt, reference):
|
| 5 |
+
if not prompt.strip():
|
| 6 |
+
return "Please enter a prompt.", ""
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
results = optimize_prompt(prompt, reference)
|
| 10 |
+
best = results[0]
|
| 11 |
+
|
| 12 |
+
best_prompt = best["prompt"]
|
| 13 |
+
details = ""
|
| 14 |
+
|
| 15 |
+
for i, r in enumerate(results, 1):
|
| 16 |
+
details += f"\n=== Variant {i} | Score: {r['score']} ===\n"
|
| 17 |
+
details += f"PROMPT:\n{r['prompt']}\n\n"
|
| 18 |
+
details += f"OUTPUT:\n{r['output']}\n\n"
|
| 19 |
+
|
| 20 |
+
return best_prompt, details
|
| 21 |
+
|
| 22 |
+
except Exception as e:
|
| 23 |
+
# user-friendly message
|
| 24 |
+
msg = "⚠️ Something went wrong while generating results.\n"
|
| 25 |
+
msg += "Please try again in a moment."
|
| 26 |
+
return msg, msg
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("# 🧠 Prompt Optimization Tool")
|
| 31 |
+
gr.Markdown("Powered by **Mistral-7B-Instruct** (free, HF-native LLM)")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
prompt_in = gr.Textbox(label="Enter your prompt", lines=4)
|
| 35 |
+
ref_in = gr.Textbox(label="Optional: Ideal answer", lines=4)
|
| 36 |
+
|
| 37 |
+
optimize_btn = gr.Button("🚀 Optimize Prompt")
|
| 38 |
+
|
| 39 |
+
best_out = gr.Textbox(label="🏆 Best Optimized Prompt", lines=6)
|
| 40 |
+
details_out = gr.Textbox(label="📊 All Variants", lines=20)
|
| 41 |
+
|
| 42 |
+
optimize_btn.click(
|
| 43 |
+
fn=run_optimizer,
|
| 44 |
+
inputs=[prompt_in, ref_in],
|
| 45 |
+
outputs=[best_out, details_out]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
demo.launch()
|