Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,80 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 5 |
import json
|
| 6 |
-
from huggingface_hub import login
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
PROMPT_TEMPLATE = """You are a prompt evaluation assistant. Evaluate the following user prompt in JSON format using the structure provided below.
|
| 15 |
Prompt:
|
|
|
|
| 16 |
{user_prompt}
|
|
|
|
| 17 |
Evaluate based on the following criteria:
|
| 18 |
- Clarity (1-5)
|
| 19 |
- Context (1-5)
|
| 20 |
- Specificity (1-5)
|
| 21 |
- Intent (1-5)
|
| 22 |
Also include a suggestion for improving the prompt.
|
|
|
|
| 23 |
Respond ONLY in this JSON format:
|
| 24 |
-
{
|
| 25 |
"prompt": "...",
|
| 26 |
-
"evaluation": {
|
| 27 |
"Clarity": ...,
|
| 28 |
"Context": ...,
|
| 29 |
"Specificity": ...,
|
| 30 |
"Intent": ...,
|
| 31 |
"suggestion": "..."
|
| 32 |
-
}
|
| 33 |
-
}
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 50 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 51 |
-
model_id,
|
| 52 |
-
device_map="cpu",
|
| 53 |
-
torch_dtype=torch.float32 # Use float32 if float16 fails
|
| 54 |
-
)
|
| 55 |
-
return tokenizer, model
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
# --- Prompt Input ---
|
| 60 |
user_prompt = st.text_area("Paste your AI prompt here:", height=200)
|
| 61 |
|
| 62 |
-
def evaluate_prompt_local(prompt_text):
|
| 63 |
-
full_prompt = PROMPT_TEMPLATE.format(user_prompt=prompt_text)
|
| 64 |
-
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 65 |
-
outputs = model.generate(
|
| 66 |
-
**inputs,
|
| 67 |
-
max_new_tokens=512,
|
| 68 |
-
temperature=0.7,
|
| 69 |
-
repetition_penalty=1.1,
|
| 70 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 71 |
-
)
|
| 72 |
-
decoded = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
|
| 73 |
-
|
| 74 |
-
# Try extracting only the JSON response from the output
|
| 75 |
-
try:
|
| 76 |
-
json_part = decoded[decoded.index("{"):decoded.rindex("}")+1]
|
| 77 |
-
return json.loads(json_part)
|
| 78 |
-
except Exception as e:
|
| 79 |
-
return {"error": f"Failed to parse model output: {str(e)}", "raw": decoded}
|
| 80 |
-
|
| 81 |
-
# --- Run Evaluation ---
|
| 82 |
if st.button("Evaluate Prompt") and user_prompt:
|
| 83 |
-
with st.spinner("Evaluating your prompt
|
| 84 |
-
evaluation_result =
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
st.text_area("Raw Output:", value=evaluation_result.get("raw", ""), height=250)
|
| 89 |
-
else:
|
| 90 |
-
st.subheader("Evaluation Result (JSON):")
|
| 91 |
-
st.code(json.dumps(evaluation_result, indent=2), language='json')
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
| 1 |
+
# 👮 PromptPolice MVP - Streamlit App with Mistral Backend (JSON Output)
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
+
import requests
|
|
|
|
| 5 |
import json
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
HF_API_URL = "https://router.huggingface.co/novita/v3/openai/chat/completions"
|
| 8 |
+
HF_TOKEN = "HF_PROJECT_TOKEN"
|
| 9 |
|
| 10 |
+
HEADERS = {
|
| 11 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 12 |
+
"Content-Type": "application/json"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
PROMPT_TEMPLATE = """
|
| 16 |
+
You are a prompt evaluation assistant. Evaluate the following user prompt in JSON format using the structure provided below.
|
| 17 |
|
|
|
|
| 18 |
Prompt:
|
| 19 |
+
|
| 20 |
{user_prompt}
|
| 21 |
+
|
| 22 |
Evaluate based on the following criteria:
|
| 23 |
- Clarity (1-5)
|
| 24 |
- Context (1-5)
|
| 25 |
- Specificity (1-5)
|
| 26 |
- Intent (1-5)
|
| 27 |
Also include a suggestion for improving the prompt.
|
| 28 |
+
|
| 29 |
Respond ONLY in this JSON format:
|
| 30 |
+
{
|
| 31 |
"prompt": "...",
|
| 32 |
+
"evaluation": {
|
| 33 |
"Clarity": ...,
|
| 34 |
"Context": ...,
|
| 35 |
"Specificity": ...,
|
| 36 |
"Intent": ...,
|
| 37 |
"suggestion": "..."
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
"""
|
| 41 |
|
| 42 |
+
def evaluate_prompt(user_prompt):
|
| 43 |
+
payload = {
|
| 44 |
+
"inputs": PROMPT_TEMPLATE.format(user_prompt=user_prompt),
|
| 45 |
+
"parameters": {"max_new_tokens": 512, "temperature": 0.7}
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
response = requests.post(HF_API_URL, headers=HEADERS, json=payload)
|
| 49 |
+
result = response.json()
|
| 50 |
|
| 51 |
+
# Handle streaming/text output
|
| 52 |
+
if isinstance(result, list) and "generated_text" in result[0]:
|
| 53 |
+
raw_text = result[0]["generated_text"]
|
| 54 |
+
try:
|
| 55 |
+
# Try to parse the JSON segment only
|
| 56 |
+
json_part = raw_text[raw_text.index("{"):]
|
| 57 |
+
return json.loads(json_part)
|
| 58 |
+
except:
|
| 59 |
+
return {"error": "Failed to parse model output."}
|
| 60 |
+
else:
|
| 61 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# --- Streamlit UI ---
|
| 64 |
+
st.set_page_config(page_title="👮 PromptPolice", layout="centered")
|
| 65 |
+
st.title("👮 PromptPolice: Prompt Evaluator")
|
| 66 |
|
|
|
|
| 67 |
user_prompt = st.text_area("Paste your AI prompt here:", height=200)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if st.button("Evaluate Prompt") and user_prompt:
|
| 70 |
+
with st.spinner("Evaluating your prompt with Mistral..."):
|
| 71 |
+
evaluation_result = evaluate_prompt(user_prompt)
|
| 72 |
|
| 73 |
+
st.subheader("Evaluation Result (JSON):")
|
| 74 |
+
st.code(json.dumps(evaluation_result, indent=2), language='json')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
# Optional: Append to local file for crowd-sourced fine-tuning later
|
| 77 |
+
if st.button("💾 Save to Dataset"):
|
| 78 |
+
with open("crowdsourced_prompts.jsonl", "a", encoding="utf-8") as f:
|
| 79 |
+
f.write(json.dumps(evaluation_result) + "\n")
|
| 80 |
+
st.success("Prompt evaluation saved!")
|