aika42 commited on
Commit
8d7a9f0
·
verified ·
1 Parent(s): ab8d2ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -40,25 +40,37 @@ Respond ONLY in this JSON format:
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")
@@ -67,7 +79,7 @@ st.title("👮 PromptPolice: Prompt Evaluator")
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):")
 
40
  """
41
 
42
  def evaluate_prompt(user_prompt):
43
+ # Format the input prompt
44
+ formatted_prompt = PROMPT_TEMPLATE.format(user_prompt=user_prompt)
45
+
46
  payload = {
47
+ "messages": [
48
+ {
49
+ "role": "user",
50
+ "content": formatted_prompt
51
+ }
52
+ ],
53
+ "model": "deepseek/deepseek-v3-0324"
54
  }
55
 
56
+ try:
57
+ # Send request to DeepSeek API
58
+ response = requests.post(API_URL, headers=HEADERS, json=payload)
59
+ response.raise_for_status()
60
+
61
+ result = response.json()
62
+
63
+ # Extract JSON formatted evaluation
64
+ if isinstance(result, dict) and "choices" in result:
65
+ raw_text = result["choices"][0]["message"]["content"]
66
+ try:
67
+ json_part = raw_text[raw_text.index("{"):]
68
+ return json.loads(json_part)
69
+ except (ValueError, json.JSONDecodeError) as e:
70
+ return {"error": f"Failed to parse model output: {str(e)}"}
71
  return result
72
+ except requests.exceptions.RequestException as e:
73
+ return {"error": f"API request failed: {str(e)}"}
74
 
75
  # --- Streamlit UI ---
76
  st.set_page_config(page_title="👮 PromptPolice", layout="centered")
 
79
  user_prompt = st.text_area("Paste your AI prompt here:", height=200)
80
 
81
  if st.button("Evaluate Prompt") and user_prompt:
82
+ with st.spinner("Evaluating your prompt with DeepSeek..."):
83
  evaluation_result = evaluate_prompt(user_prompt)
84
 
85
  st.subheader("Evaluation Result (JSON):")