Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
|
| 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 |
+
|
| 23 |
+
Evaluate based on the following criteria:
|
| 24 |
+
- Clarity (1-5)
|
| 25 |
+
- Context (1-5)
|
| 26 |
+
- Specificity (1-5)
|
| 27 |
+
- Intent (1-5)
|
| 28 |
+
Also include a suggestion for improving the prompt.
|
| 29 |
+
|
| 30 |
+
Respond ONLY in this JSON format:
|
| 31 |
+
{
|
| 32 |
+
"prompt": "...",
|
| 33 |
+
"evaluation": {
|
| 34 |
+
"Clarity": ...,
|
| 35 |
+
"Context": ...,
|
| 36 |
+
"Specificity": ...,
|
| 37 |
+
"Intent": ...,
|
| 38 |
+
"suggestion": "..."
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
def evaluate_prompt(user_prompt):
|
| 44 |
+
payload = {
|
| 45 |
+
"inputs": PROMPT_TEMPLATE.format(user_prompt=user_prompt),
|
| 46 |
+
"parameters": {"max_new_tokens": 512, "temperature": 0.7}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
response = requests.post(HF_API_URL, headers=HEADERS, json=payload)
|
| 50 |
+
result = response.json()
|
| 51 |
+
|
| 52 |
+
# Handle streaming/text output
|
| 53 |
+
if isinstance(result, list) and "generated_text" in result[0]:
|
| 54 |
+
raw_text = result[0]["generated_text"]
|
| 55 |
+
try:
|
| 56 |
+
# Try to parse the JSON segment only
|
| 57 |
+
json_part = raw_text[raw_text.index("{"):]
|
| 58 |
+
return json.loads(json_part)
|
| 59 |
+
except:
|
| 60 |
+
return {"error": "Failed to parse model output."}
|
| 61 |
+
else:
|
| 62 |
+
return result
|
| 63 |
+
|
| 64 |
+
# --- Streamlit UI ---
|
| 65 |
+
st.set_page_config(page_title="👮 PromptPolice", layout="centered")
|
| 66 |
+
st.title("👮 PromptPolice: Prompt Evaluator")
|
| 67 |
+
|
| 68 |
+
user_prompt = st.text_area("Paste your AI prompt here:", height=200)
|
| 69 |
+
|
| 70 |
+
if st.button("Evaluate Prompt") and user_prompt:
|
| 71 |
+
with st.spinner("Evaluating your prompt with Mistral..."):
|
| 72 |
+
evaluation_result = evaluate_prompt(user_prompt)
|
| 73 |
+
|
| 74 |
+
st.subheader("Evaluation Result (JSON):")
|
| 75 |
+
st.code(json.dumps(evaluation_result, indent=2), language='json')
|
| 76 |
+
|
| 77 |
+
# Optional: Append to local file for crowd-sourced fine-tuning later
|
| 78 |
+
if st.button("💾 Save to Dataset"):
|
| 79 |
+
with open("crowdsourced_prompts.jsonl", "a", encoding="utf-8") as f:
|
| 80 |
+
f.write(json.dumps(evaluation_result) + "\n")
|
| 81 |
+
st.success("Prompt evaluation saved!")
|