Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# Environment Setup
|
| 8 |
+
HF_API_URL = "https://router.huggingface.co/novita/v3/openai/chat/completions"
|
| 9 |
+
HF_TOKEN = os.environ.get("HF_PROJECT_TOKEN")
|
| 10 |
+
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 11 |
+
|
| 12 |
+
# Prompt Template
|
| 13 |
+
PROMPT_TEMPLATE = """
|
| 14 |
+
You are a prompt evaluation assistant called \"PromptPolice\". Evaluate the following user prompt based on the criteria below. For each, rate from 1 (poor) to 5 (excellent), explain why, and suggest specific improvements.
|
| 15 |
+
|
| 16 |
+
Prompt:
|
| 17 |
+
\"\"\"
|
| 18 |
+
{user_prompt}
|
| 19 |
+
\"\"\"
|
| 20 |
+
|
| 21 |
+
Evaluation Criteria (1β5):
|
| 22 |
+
- π Clarity: Is the prompt easy to understand?
|
| 23 |
+
- π§ Context: Does the prompt provide enough background or situational framing?
|
| 24 |
+
- π― Specificity: Are the goals or constraints clearly defined?
|
| 25 |
+
- ποΈ Intent Alignment: Is it clear what task or behavior the prompt is meant to elicit?
|
| 26 |
+
|
| 27 |
+
Verdict Logic:
|
| 28 |
+
- β
**Pass**: Overall Score is 16 or above AND no criterion rated below 3.
|
| 29 |
+
- β οΈ **Warning**: Score between 11β15 OR one rating below 3.
|
| 30 |
+
- π« **Ticket**: Score 10 or below OR two or more ratings below 3.
|
| 31 |
+
|
| 32 |
+
Output Format:
|
| 33 |
+
|
| 34 |
+
Evaluation:
|
| 35 |
+
- π Clarity: X/5 β [Explanation]
|
| 36 |
+
- π§ Context: X/5 β [Explanation]
|
| 37 |
+
- π― Specificity: X/5 β [Explanation]
|
| 38 |
+
- ποΈ Intent Alignment: X/5 β [Explanation]
|
| 39 |
+
|
| 40 |
+
Overall Score: X/20
|
| 41 |
+
|
| 42 |
+
Flaw Summary: [One-line summary of the weakest point]
|
| 43 |
+
|
| 44 |
+
π‘οΈ Verdict: [β
Pass / β οΈ Warning / π« Ticket] β [Justification based on score and flaw]
|
| 45 |
+
|
| 46 |
+
Prompt Type(s): [e.g., Instruction, Summarization, Query, Roleplay, Classification, Creative Writing, Other]
|
| 47 |
+
|
| 48 |
+
Suggestions:
|
| 49 |
+
- [Actionable suggestion 1]
|
| 50 |
+
- [Actionable suggestion 2]
|
| 51 |
+
|
| 52 |
+
Improved Prompt:
|
| 53 |
+
"[Rewritten version of the user prompt]"
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
# Function to query DeepSeek
|
| 57 |
+
@st.cache_data(show_spinner=False)
|
| 58 |
+
def evaluate_prompt(user_prompt):
|
| 59 |
+
payload = {
|
| 60 |
+
"messages": [
|
| 61 |
+
{"role": "user", "content": PROMPT_TEMPLATE.format(user_prompt=user_prompt)}
|
| 62 |
+
],
|
| 63 |
+
"model": "deepseek/deepseek-r1-turbo"
|
| 64 |
+
}
|
| 65 |
+
response = requests.post(HF_API_URL, headers=HEADERS, json=payload)
|
| 66 |
+
if response.status_code == 200:
|
| 67 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 68 |
+
else:
|
| 69 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 70 |
+
|
| 71 |
+
# App UI
|
| 72 |
+
st.set_page_config("PromptPolice", page_icon="π", layout="wide")
|
| 73 |
+
st.title(":oncoming_police_car: PromptPolice: Evaluate Your Prompts Like a Pro")
|
| 74 |
+
|
| 75 |
+
# Sidebar
|
| 76 |
+
with st.sidebar:
|
| 77 |
+
st.header(":gear: Prompt Options")
|
| 78 |
+
use_example = st.checkbox("Load Example Prompt")
|
| 79 |
+
st.markdown("---")
|
| 80 |
+
st.write("Created with β€οΈ using DeepSeek + Streamlit")
|
| 81 |
+
|
| 82 |
+
# Main Input
|
| 83 |
+
if use_example:
|
| 84 |
+
user_input = st.text_area("Paste your prompt here:",
|
| 85 |
+
"Generate a short story about a robot in a post-apocalyptic world.",
|
| 86 |
+
height=200)
|
| 87 |
+
else:
|
| 88 |
+
user_input = st.text_area("Paste your prompt here:", height=200)
|
| 89 |
+
|
| 90 |
+
# Evaluate Button
|
| 91 |
+
if st.button(":mag_right: Evaluate Prompt"):
|
| 92 |
+
if not HF_TOKEN:
|
| 93 |
+
st.error("Missing Hugging Face token. Please set HF_PROJECT_TOKEN as environment variable.")
|
| 94 |
+
elif user_input.strip() == "":
|
| 95 |
+
st.warning("Please enter a prompt to evaluate.")
|
| 96 |
+
else:
|
| 97 |
+
with st.spinner("Evaluating prompt with PromptPolice..."):
|
| 98 |
+
result = evaluate_prompt(user_input)
|
| 99 |
+
st.markdown("---")
|
| 100 |
+
st.subheader(":clipboard: Evaluation Result")
|
| 101 |
+
st.markdown(f"""
|
| 102 |
+
<div style='background-color:#f5f5f5; padding:20px; border-radius:10px;'>
|
| 103 |
+
<pre style='white-space:pre-wrap'>{result}</pre>
|
| 104 |
+
</div>
|
| 105 |
+
""", unsafe_allow_html=True)
|
| 106 |
+
|
| 107 |
+
# Option to Copy
|
| 108 |
+
st.download_button("Download Evaluation", result, file_name="evaluation.txt")
|
| 109 |
+
|
| 110 |
+
# Footer
|
| 111 |
+
st.markdown("""
|
| 112 |
+
---
|
| 113 |
+
Made with π by [Penguins]. Powered by DeepSeek R1 Turbo (free-tier). Feedback welcome!
|
| 114 |
+
""")
|