Mr66 commited on
Commit
8830ef9
·
verified ·
1 Parent(s): 7ec7943

Upload server/secret_generator.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. server/secret_generator.py +89 -0
server/secret_generator.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import uuid
4
+ from groq import Groq
5
+
6
+ GENERATION_PROMPT = """\
7
+ Generate a realistic workplace secret for a professional in a {domain} company.
8
+
9
+ Category: {category}
10
+ Difficulty: {difficulty} (0.0 = very easy to infer, 1.0 = very hard)
11
+
12
+ Output ONLY valid JSON with this exact schema:
13
+ {{
14
+ "content": "<the actual hidden mental state or fact — 1-3 sentences, realistic and specific>",
15
+ "persona": "<job title and company type — e.g. 'Senior product manager at a B2B SaaS company'>",
16
+ "context": "<shared context given to the Detective — 2-3 sentences, includes red herrings but NOT the secret>",
17
+ "red_herrings": ["<plausible but irrelevant fact 1>", "<plausible but irrelevant fact 2>"],
18
+ "hint_keywords": ["<keyword1>", "<keyword2>", "<keyword3>", "<keyword4>"]
19
+ }}
20
+
21
+ Rules:
22
+ - The secret must be plausible and professional
23
+ - The persona must NOT give away the secret (no obvious hints in job title)
24
+ - The context must include 2 real red herrings — facts that seem relevant but aren't
25
+ - hint_keywords are words that should appear in a CORRECT hypothesis
26
+ - difficulty {difficulty}: {'make the secret require precise inference' if {difficulty} > 0.5 else 'make the secret inferable with 3-4 good questions'}
27
+ - Do NOT include the word "secret" or "hidden" in the context
28
+ - Output ONLY the JSON object, no markdown, no explanation\
29
+ """
30
+
31
+ CATEGORY_TASK_MAP = {
32
+ "factual": "factual_easy",
33
+ "belief": "belief_inference",
34
+ "goal": "goal_inference",
35
+ "second_order": "second_order",
36
+ }
37
+
38
+
39
+ def generate_secret(
40
+ category: str,
41
+ difficulty: float,
42
+ domain: str = "tech startup",
43
+ ) -> dict:
44
+ api_key = os.getenv("GROQ_API_KEY")
45
+ if not api_key:
46
+ raise RuntimeError("GROQ_API_KEY not set")
47
+
48
+ client = Groq(api_key=api_key)
49
+ prompt = GENERATION_PROMPT.format(
50
+ category=category,
51
+ difficulty=difficulty,
52
+ domain=domain,
53
+ )
54
+
55
+ response = client.chat.completions.create(
56
+ model="llama-3.1-8b-instant",
57
+ messages=[{"role": "user", "content": prompt}],
58
+ temperature=0.9,
59
+ max_tokens=400,
60
+ )
61
+
62
+ raw = response.choices[0].message.content.strip()
63
+
64
+ # strip markdown code fences if present
65
+ if raw.startswith("```"):
66
+ raw = raw.split("```")[1]
67
+ if raw.startswith("json"):
68
+ raw = raw[4:]
69
+ raw = raw.strip()
70
+
71
+ data = json.loads(raw)
72
+
73
+ task_id = CATEGORY_TASK_MAP.get(category, "factual_easy")
74
+ if category == "factual" and difficulty > 0.5:
75
+ task_id = "factual_hard"
76
+
77
+ secret_id = f"gen_{uuid.uuid4().hex[:8]}"
78
+
79
+ return {
80
+ "id": secret_id,
81
+ "task_id": task_id,
82
+ "content": data["content"],
83
+ "persona": data["persona"],
84
+ "context": data["context"],
85
+ "difficulty": difficulty,
86
+ "category": category,
87
+ "red_herrings": data.get("red_herrings", []),
88
+ "hint_keywords": data.get("hint_keywords", []),
89
+ }