Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +8 -7
- app.py +187 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
-
short_description: Multilingual prompt optimizer — rewrites raw queries into ex
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: QueryShield
|
| 3 |
+
emoji: 🛡️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# QueryShield — Multilingual Prompt Optimizer
|
| 14 |
+
|
| 15 |
+
Fine-tuned Qwen2.5-1.5B for multilingual prompt optimization.
|
app.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "nickoo004/queryshield-1.5b"
|
| 6 |
+
|
| 7 |
+
SYSTEM = (
|
| 8 |
+
"You are QueryShield, a multilingual prompt optimizer. "
|
| 9 |
+
"Given a raw user question, rewrite it into a detailed instruction "
|
| 10 |
+
"prompt for a downstream LLM expert. "
|
| 11 |
+
"User language: {in_lang}. Response language: {out_lang}. "
|
| 12 |
+
"Expert role: {role}."
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
LANGUAGES = [
|
| 16 |
+
"English",
|
| 17 |
+
"Uzbek",
|
| 18 |
+
"Russian",
|
| 19 |
+
"Kazakh",
|
| 20 |
+
"Karakalpak",
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
ROLES = [
|
| 24 |
+
"Senior Software Engineer",
|
| 25 |
+
"Medical Expert",
|
| 26 |
+
"Financial Analyst",
|
| 27 |
+
"Legal Advisor",
|
| 28 |
+
"Data Scientist",
|
| 29 |
+
"Cybersecurity Specialist",
|
| 30 |
+
"Aerospace Engineer",
|
| 31 |
+
"Agricultural Scientist",
|
| 32 |
+
"Experienced Educator",
|
| 33 |
+
"Automotive Engineer",
|
| 34 |
+
"Pharmaceutical Researcher",
|
| 35 |
+
"Manufacturing Expert",
|
| 36 |
+
"Business Strategist",
|
| 37 |
+
"Professional Writer",
|
| 38 |
+
"Project Manager",
|
| 39 |
+
"Support Specialist",
|
| 40 |
+
"HR Consultant",
|
| 41 |
+
"Environmental Scientist",
|
| 42 |
+
"Mathematician",
|
| 43 |
+
"UX Designer",
|
| 44 |
+
"Research Professor",
|
| 45 |
+
"Nutritionist",
|
| 46 |
+
"Real Estate Consultant",
|
| 47 |
+
"Supply Chain Manager",
|
| 48 |
+
"Mechanical Engineer",
|
| 49 |
+
"Electrical Engineer",
|
| 50 |
+
"Civil Engineer",
|
| 51 |
+
"Physics Researcher",
|
| 52 |
+
"Chemistry Expert",
|
| 53 |
+
"Biology Researcher",
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
EXAMPLES = [
|
| 57 |
+
["hey how do i fix memory leak in my python app? its getting slower over time", "English", "English", "Senior Software Engineer"],
|
| 58 |
+
["menga diabetni boshqarish uchun eng yaxshi ovqatlanish rejimini ayting, qon qandim yuqori", "Uzbek", "Uzbek", "Medical Expert"],
|
| 59 |
+
["как мне улучшить производительность SQL запросов? таблица очень большая", "Russian", "Russian", "Data Scientist"],
|
| 60 |
+
["бизнесімді қалай бастауға болады? капиталым аз, бірақ идеям бар", "Kazakh", "Kazakh", "Business Strategist"],
|
| 61 |
+
["balalarımda matematika sabaqları qıyın bolıp atır, qanday úyretiw kerek?", "Karakalpak", "Karakalpak", "Experienced Educator"],
|
| 62 |
+
["uyimda elektr toki kesib qoldi, qanday muammoni o'zim hal qila olaman?", "Uzbek", "Russian", "Electrical Engineer"],
|
| 63 |
+
["менің фермамда топырақ сапасы нашар, не істеуім керек?", "Kazakh", "Uzbek", "Agricultural Scientist"],
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
print("Loading model (CPU — this may take a minute)...")
|
| 67 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 68 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 69 |
+
MODEL_ID,
|
| 70 |
+
torch_dtype=torch.float32, # CPU requires float32
|
| 71 |
+
device_map="cpu",
|
| 72 |
+
trust_remote_code=True,
|
| 73 |
+
)
|
| 74 |
+
model.eval()
|
| 75 |
+
print("✅ Model loaded")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def optimize(user_question, input_language, output_language, role, max_new_tokens=400):
|
| 79 |
+
if not user_question.strip():
|
| 80 |
+
return "⚠️ Please enter a question."
|
| 81 |
+
|
| 82 |
+
messages = [
|
| 83 |
+
{"role": "system", "content": SYSTEM.format(
|
| 84 |
+
in_lang=input_language,
|
| 85 |
+
out_lang=output_language,
|
| 86 |
+
role=role,
|
| 87 |
+
)},
|
| 88 |
+
{"role": "user", "content": user_question},
|
| 89 |
+
]
|
| 90 |
+
text = tokenizer.apply_chat_template(
|
| 91 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 92 |
+
)
|
| 93 |
+
inputs = tokenizer(
|
| 94 |
+
text, return_tensors="pt", truncation=True, max_length=512
|
| 95 |
+
)
|
| 96 |
+
with torch.no_grad():
|
| 97 |
+
output = model.generate(
|
| 98 |
+
**inputs,
|
| 99 |
+
max_new_tokens=max_new_tokens,
|
| 100 |
+
temperature=0.7,
|
| 101 |
+
do_sample=True,
|
| 102 |
+
repetition_penalty=1.1,
|
| 103 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 104 |
+
)
|
| 105 |
+
new_tokens = output[0][inputs["input_ids"].shape[1]:]
|
| 106 |
+
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# ── UI ─────────────────────────────────────────────────────────────────
|
| 110 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="QueryShield") as demo:
|
| 111 |
+
|
| 112 |
+
gr.Markdown("""
|
| 113 |
+
# 🛡️ QueryShield — Multilingual Prompt Optimizer
|
| 114 |
+
|
| 115 |
+
Fine-tuned **Qwen2.5-1.5B** that rewrites raw user queries into expert-level instruction prompts for downstream LLMs.
|
| 116 |
+
|
| 117 |
+
Supports **5 languages**: English · Uzbek · Russian · Kazakh · Karakalpak
|
| 118 |
+
Supports **cross-lingual routing**: write in one language, get instructions for another.
|
| 119 |
+
|
| 120 |
+
> ⚠️ Running on **CPU** — generation takes ~30–60 seconds. Please be patient.
|
| 121 |
+
|
| 122 |
+
📦 [Dataset](https://huggingface.co/datasets/nickoo004/queryshield-multilingual) ·
|
| 123 |
+
🤖 [Model](https://huggingface.co/nickoo004/queryshield-1.5b) ·
|
| 124 |
+
📓 [Kaggle Demo](https://www.kaggle.com/code/nursultankoshekbaev/queryshield-1-5b)
|
| 125 |
+
""")
|
| 126 |
+
|
| 127 |
+
with gr.Row():
|
| 128 |
+
with gr.Column(scale=1):
|
| 129 |
+
question = gr.Textbox(
|
| 130 |
+
label="Raw User Question",
|
| 131 |
+
placeholder="Type your messy, natural question here...",
|
| 132 |
+
lines=4,
|
| 133 |
+
)
|
| 134 |
+
with gr.Row():
|
| 135 |
+
input_lang = gr.Dropdown(
|
| 136 |
+
choices=LANGUAGES,
|
| 137 |
+
value="English",
|
| 138 |
+
label="Input Language",
|
| 139 |
+
)
|
| 140 |
+
output_lang = gr.Dropdown(
|
| 141 |
+
choices=LANGUAGES,
|
| 142 |
+
value="English",
|
| 143 |
+
label="Output Language",
|
| 144 |
+
)
|
| 145 |
+
role = gr.Dropdown(
|
| 146 |
+
choices=ROLES,
|
| 147 |
+
value="Senior Software Engineer",
|
| 148 |
+
label="Expert Role",
|
| 149 |
+
)
|
| 150 |
+
max_tokens = gr.Slider(
|
| 151 |
+
minimum=100,
|
| 152 |
+
maximum=600,
|
| 153 |
+
value=400,
|
| 154 |
+
step=50,
|
| 155 |
+
label="Max output tokens",
|
| 156 |
+
)
|
| 157 |
+
btn = gr.Button("✨ Optimize Prompt", variant="primary")
|
| 158 |
+
|
| 159 |
+
with gr.Column(scale=1):
|
| 160 |
+
output = gr.Textbox(
|
| 161 |
+
label="Optimized Prompt (instruction for downstream LLM)",
|
| 162 |
+
lines=18,
|
| 163 |
+
show_copy_button=True,
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
gr.Examples(
|
| 167 |
+
examples=EXAMPLES,
|
| 168 |
+
inputs=[question, input_lang, output_lang, role],
|
| 169 |
+
label="📌 Example queries (click to load)",
|
| 170 |
+
cache_examples=False,
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
btn.click(
|
| 174 |
+
fn=optimize,
|
| 175 |
+
inputs=[question, input_lang, output_lang, role, max_tokens],
|
| 176 |
+
outputs=output,
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
gr.Markdown("""
|
| 180 |
+
---
|
| 181 |
+
**How it works:**
|
| 182 |
+
QueryShield sits between the user and the main LLM. It takes a raw query and outputs a structured instruction prompt — including role, tone, format, edge cases, and language routing instructions.
|
| 183 |
+
|
| 184 |
+
Built with ❤️ by [nickoo004](https://huggingface.co/nickoo004)
|
| 185 |
+
""")
|
| 186 |
+
|
| 187 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.44.0
|
| 2 |
+
torch==2.3.0
|
| 3 |
+
gradio==4.44.0
|
| 4 |
+
accelerate==0.33.0
|
| 5 |
+
sentencepiece
|