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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
+
# π§ Prompt Optimization Tool using Generative AI
|
| 12 |
+
|
| 13 |
+
A production-ready **GenAI application** that automatically improves user prompts, evaluates multiple prompt variants, and selects the best-performing one using an intelligent scoring framework.
|
| 14 |
+
|
| 15 |
+
This project demonstrates **modern prompt engineering, LLM integration, and automated evaluation** in a real-world deployed system.
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## π Features
|
| 20 |
+
|
| 21 |
+
- β¨ Generates multiple **optimized prompt variants**
|
| 22 |
+
- π€ Uses a **Large Language Model (LLM)** to produce responses
|
| 23 |
+
- π Automatically **evaluates outputs** using:
|
| 24 |
+
- Semantic similarity
|
| 25 |
+
- Conciseness
|
| 26 |
+
- LLM-based judgment
|
| 27 |
+
- π Selects the **best prompt** automatically
|
| 28 |
+
- π Interactive **Gradio web interface**
|
| 29 |
+
- π Secure token handling using **environment variables**
|
| 30 |
+
- βοΈ Deployable on **Hugging Face Spaces**
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## ποΈ System Architecture
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
User Input
|
| 38 |
+
β
|
| 39 |
+
Prompt Variant Generator
|
| 40 |
+
β
|
| 41 |
+
LLM (Mistral-7B-Instruct)
|
| 42 |
+
β
|
| 43 |
+
Automatic Evaluator
|
| 44 |
+
β
|
| 45 |
+
Scoring Engine
|
| 46 |
+
β
|
| 47 |
+
Best Prompt Selection
|
| 48 |
+
β
|
| 49 |
+
Gradio Web Interface
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
---
|
| 53 |
+
|
| 54 |
+
## π οΈ Tech Stack
|
| 55 |
+
|
| 56 |
+
| Layer | Technology |
|
| 57 |
+
|------|------------|
|
| 58 |
+
Frontend | Gradio
|
| 59 |
+
Backend | Python
|
| 60 |
+
LLM | Mistral-7B-Instruct (Hugging Face Inference API)
|
| 61 |
+
Evaluation | Sentence Transformers, Scikit-learn
|
| 62 |
+
Deployment | Hugging Face Spaces
|
| 63 |
+
Security | Environment variables & HF Secrets
|
| 64 |
+
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
## π Project Structure
|
| 68 |
+
|
| 69 |
+
```
|
| 70 |
+
.
|
| 71 |
+
βββ app.py # Gradio UI
|
| 72 |
+
βββ backend.py # LLM connection + prompt variant generator
|
| 73 |
+
βββ evaluator.py # Automatic scoring logic
|
| 74 |
+
βββ optimizer.py # Core optimization engine
|
| 75 |
+
βββ requirements.txt # Project dependencies
|
| 76 |
+
βββ README.md # Project documentation
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
## βοΈ Installation (Local / Colab)
|
| 82 |
+
|
| 83 |
+
### 1οΈβ£ Clone the repository
|
| 84 |
+
```bash
|
| 85 |
+
git clone <your-repo-url>
|
| 86 |
+
cd prompt-optimization-tool
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
### 2οΈβ£ Install dependencies
|
| 90 |
+
```bash
|
| 91 |
+
pip install -r requirements.txt
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
### 3οΈβ£ Set Hugging Face token
|
| 95 |
+
|
| 96 |
+
Create a token at:
|
| 97 |
+
π https://huggingface.co/settings/tokens (Role: **Read**)
|
| 98 |
+
|
| 99 |
+
Then set it in your environment:
|
| 100 |
+
|
| 101 |
+
**Linux / Mac**
|
| 102 |
+
```bash
|
| 103 |
+
export HF_TOKEN=hf_your_token_here
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
**Windows (PowerShell)**
|
| 107 |
+
```powershell
|
| 108 |
+
setx HF_TOKEN "hf_your_token_here"
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
**Google Colab**
|
| 112 |
+
```python
|
| 113 |
+
import os
|
| 114 |
+
os.environ["HF_TOKEN"] = "hf_your_token_here"
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## βΆοΈ Running the App
|
| 120 |
+
|
| 121 |
+
```bash
|
| 122 |
+
python app.py
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
Open the link shown in the terminal to use the web interface.
|
| 126 |
+
|
| 127 |
+
---
|
| 128 |
+
|
| 129 |
+
## π Deployment on Hugging Face Spaces
|
| 130 |
+
|
| 131 |
+
1. Go to: https://huggingface.co/spaces
|
| 132 |
+
2. Create a new Space β **SDK: Gradio**
|
| 133 |
+
3. Upload these files:
|
| 134 |
+
- `app.py`
|
| 135 |
+
- `backend.py`
|
| 136 |
+
- `evaluator.py`
|
| 137 |
+
- `optimizer.py`
|
| 138 |
+
- `requirements.txt`
|
| 139 |
+
4. Add your token:
|
| 140 |
+
- Space β **Settings β Repository secrets**
|
| 141 |
+
```
|
| 142 |
+
HF_TOKEN = hf_your_token_here
|
| 143 |
+
```
|
| 144 |
+
5. Restart the Space
|
| 145 |
+
6. Your app is now live π
|
| 146 |
+
|
| 147 |
+
---
|
| 148 |
+
|
| 149 |
+
## π§ͺ Example Test Cases
|
| 150 |
+
|
| 151 |
+
### Prompt
|
| 152 |
+
```
|
| 153 |
+
Explain gradient descent
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
### Reference (optional)
|
| 157 |
+
```
|
| 158 |
+
Gradient descent is an optimization algorithm that minimizes a loss function by iteratively updating parameters in the direction of the negative gradient.
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
### Output
|
| 162 |
+
- Best optimized prompt
|
| 163 |
+
- Ranked prompt variants
|
| 164 |
+
- Model-generated answers
|
| 165 |
+
- Scores for each variant
|
| 166 |
+
|
| 167 |
+
---
|
| 168 |
+
|
| 169 |
+
## π Security Notes
|
| 170 |
+
|
| 171 |
+
- β Never hard-code API keys or tokens
|
| 172 |
+
- β
Always use **environment variables** or **Hugging Face secrets**
|
| 173 |
+
- π Rotate tokens if exposed
|
| 174 |
+
|
| 175 |
+
---
|
| 176 |
+
|
| 177 |
+
## π Why this project is unique
|
| 178 |
+
|
| 179 |
+
Instead of only generating text, this system:
|
| 180 |
+
|
| 181 |
+
- **Evaluates prompt quality automatically**
|
| 182 |
+
- Uses **LLM-as-a-judge**
|
| 183 |
+
- Applies **semantic similarity scoring**
|
| 184 |
+
- Implements **real optimization logic**
|
| 185 |
+
- Is fully **deployable in production**
|
| 186 |
+
|
| 187 |
+
This makes it a strong example of **applied Generative AI engineering**.
|
| 188 |
+
|
| 189 |
+
---
|
| 190 |
+
|
| 191 |
+
## π Use Cases
|
| 192 |
+
|
| 193 |
+
- Prompt engineering research
|
| 194 |
+
- GenAI tooling
|
| 195 |
+
- AI assistant improvement
|
| 196 |
+
- Educational demonstrations
|
| 197 |
+
- Hackathons & academic projects
|
| 198 |
+
|
| 199 |
+
---
|
| 200 |
+
|
| 201 |
+
## π License
|
| 202 |
+
|
| 203 |
+
This project is for **educational and research purposes**.
|
| 204 |
+
You are free to modify and extend it.
|
| 205 |
+
|
| 206 |
+
---
|
| 207 |
+
|
| 208 |
+
## π Acknowledgements
|
| 209 |
+
|
| 210 |
+
- Hugging Face for the Inference API
|
| 211 |
+
- Mistral AI for the open-source model
|
| 212 |
+
- Gradio for the UI framework
|
| 213 |
+
|
| 214 |
+
---
|
| 215 |
+
|
| 216 |
+
## π¨βπ» Author
|
| 217 |
+
|
| 218 |
+
**Sharv Mehta**
|
| 219 |
+
Prompt Optimization Tool using Generative AI
|
| 220 |
|
| 221 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|