Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from dotenv import load_dotenv
|
3 |
import os
|
4 |
-
import
|
5 |
-
|
6 |
from proctor import (
|
7 |
CompositeTechnique,
|
8 |
RolePrompting,
|
@@ -10,60 +8,51 @@ from proctor import (
|
|
10 |
ChainOfVerification,
|
11 |
SelfAsk,
|
12 |
EmotionPrompting,
|
13 |
-
ZeroShotCoT,
|
14 |
list_techniques,
|
15 |
)
|
16 |
|
17 |
-
#
|
18 |
-
logging.basicConfig(level=logging.INFO)
|
19 |
-
logger = logging.getLogger(__name__)
|
20 |
-
|
21 |
-
# Load environment variables
|
22 |
load_dotenv()
|
23 |
-
|
24 |
-
# Check for OpenRouter API key
|
25 |
openrouter_key = os.environ.get("OPENROUTER_API_KEY")
|
26 |
-
if not openrouter_key:
|
27 |
-
raise ValueError("OPENROUTER_API_KEY not set. Please set it in your .env file.")
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
"Llama 4 Scout": "openrouter/meta-llama/llama-4-scout",
|
35 |
-
"Mistral Small 3.1 24B": "openrouter/mistralai/mistral-small-3.1-24b-instruct",
|
36 |
-
}
|
37 |
-
|
38 |
-
TECHNIQUES = list_techniques()
|
39 |
|
40 |
-
# Model
|
41 |
MODEL_CONFIGS = {
|
42 |
-
"
|
|
|
43 |
"api_base": "https://openrouter.ai/api/v1",
|
44 |
"api_key": openrouter_key,
|
45 |
"temperature": 0.3,
|
46 |
-
"max_tokens":
|
47 |
},
|
48 |
-
"
|
|
|
49 |
"api_base": "https://openrouter.ai/api/v1",
|
50 |
"api_key": openrouter_key,
|
51 |
"temperature": 0.7,
|
52 |
-
"max_tokens":
|
53 |
},
|
54 |
-
"
|
|
|
55 |
"api_base": "https://openrouter.ai/api/v1",
|
56 |
"api_key": openrouter_key,
|
57 |
"temperature": 0.6,
|
58 |
-
"max_tokens":
|
59 |
},
|
60 |
-
"
|
|
|
61 |
"api_base": "https://openrouter.ai/api/v1",
|
62 |
"api_key": openrouter_key,
|
63 |
"temperature": 0.6,
|
64 |
-
"max_tokens":
|
65 |
},
|
66 |
-
"
|
|
|
67 |
"api_base": "https://openrouter.ai/api/v1",
|
68 |
"api_key": openrouter_key,
|
69 |
"temperature": 0.8,
|
@@ -71,251 +60,183 @@ MODEL_CONFIGS = {
|
|
71 |
},
|
72 |
}
|
73 |
|
74 |
-
#
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
name="Expert Chain-of-Thought",
|
78 |
identifier="custom-expert-cot",
|
79 |
-
techniques=[
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
92 |
|
93 |
-
def
|
94 |
"""
|
95 |
-
|
96 |
-
|
97 |
-
Args:
|
98 |
-
response: The raw response text to format
|
99 |
-
|
100 |
-
Returns:
|
101 |
-
Formatted markdown string
|
102 |
"""
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
for line in lines:
|
111 |
-
line = line.strip()
|
112 |
-
if not line:
|
113 |
-
in_list = False
|
114 |
-
formatted_lines.append("")
|
115 |
-
continue
|
116 |
-
|
117 |
-
# Check for headings (e.g., "Target Market:")
|
118 |
-
if line.endswith(":") and not line.startswith("-") and len(line) < 100:
|
119 |
-
formatted_lines.append(f"### {line}")
|
120 |
-
continue
|
121 |
-
|
122 |
-
# Check for list items (e.g., "- Item" or "1. Item")
|
123 |
-
if line.startswith("-") or (line and line[0].isdigit() and len(line) > 2 and line[1:3] in [". ", ".("]):
|
124 |
-
in_list = True
|
125 |
-
formatted_lines.append(line)
|
126 |
-
continue
|
127 |
-
|
128 |
-
# If not a heading or list item, treat as a paragraph
|
129 |
-
if in_list:
|
130 |
-
in_list = False
|
131 |
-
formatted_lines.append("")
|
132 |
-
formatted_lines.append(line)
|
133 |
-
|
134 |
-
return "\n".join(formatted_lines)
|
135 |
|
136 |
-
def
|
137 |
"""
|
138 |
-
|
139 |
-
|
140 |
-
Args:
|
141 |
-
problem: The problem statement
|
142 |
-
technique_name: Selected technique name
|
143 |
-
model_name: Selected model name
|
144 |
-
|
145 |
-
Returns:
|
146 |
-
Error message if validation fails, None otherwise
|
147 |
"""
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
-
def
|
160 |
-
problem: str,
|
161 |
-
technique_name: str,
|
162 |
-
model_name: str,
|
163 |
-
role: str = "",
|
164 |
-
emotion: str = ""
|
165 |
-
) -> str:
|
166 |
"""
|
167 |
-
|
168 |
-
|
169 |
-
Args:
|
170 |
-
problem: The problem statement to solve
|
171 |
-
technique_name: Name of the prompting technique to use
|
172 |
-
model_name: Name of the model to use
|
173 |
-
role: Role for role prompting (optional)
|
174 |
-
emotion: Emotion for emotion prompting (optional)
|
175 |
-
|
176 |
-
Returns:
|
177 |
-
Formatted response or error message
|
178 |
"""
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
kwargs = {"llm_config": llm_config}
|
191 |
-
|
192 |
-
# Add technique-specific parameters
|
193 |
-
if technique_name == "RolePrompting":
|
194 |
-
kwargs["role"] = role.strip() or "Expert"
|
195 |
-
elif technique_name == "EmotionPrompting":
|
196 |
-
kwargs["emotion"] = emotion.strip() or "thoughtful and methodical"
|
197 |
-
elif technique_name == "Expert Chain-of-Thought":
|
198 |
-
kwargs["role"] = role.strip() or "Expert"
|
199 |
-
|
200 |
-
logger.info(f"Processing problem with {technique_name} using {model_name}")
|
201 |
-
response = technique.execute(problem.strip(), **kwargs)
|
202 |
-
|
203 |
-
# Format and return the response
|
204 |
-
markdown_response = format_as_markdown(response)
|
205 |
-
logger.info("Successfully processed problem")
|
206 |
-
return markdown_response
|
207 |
-
|
208 |
-
except Exception as e:
|
209 |
-
error_msg = f"Error processing request: {str(e)}"
|
210 |
-
logger.error(error_msg)
|
211 |
-
return f"**Error**: {error_msg}"
|
212 |
|
213 |
-
def
|
214 |
"""
|
215 |
-
|
216 |
-
|
217 |
-
Args:
|
218 |
-
technique: Selected technique name
|
219 |
-
|
220 |
-
Returns:
|
221 |
-
Dictionary with visibility updates for inputs
|
222 |
"""
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
|
|
|
|
226 |
return {
|
227 |
-
|
228 |
-
|
|
|
229 |
}
|
230 |
|
231 |
-
#
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
}
|
240 |
"""
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
)
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
)
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
inputs=
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
|
|
318 |
|
319 |
-
# Launch the app
|
320 |
if __name__ == "__main__":
|
321 |
-
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import gradio as gr
|
4 |
from proctor import (
|
5 |
CompositeTechnique,
|
6 |
RolePrompting,
|
|
|
8 |
ChainOfVerification,
|
9 |
SelfAsk,
|
10 |
EmotionPrompting,
|
|
|
11 |
list_techniques,
|
12 |
)
|
13 |
|
14 |
+
# Load environment variables (.env should contain OPENROUTER_API_KEY)
|
|
|
|
|
|
|
|
|
15 |
load_dotenv()
|
|
|
|
|
16 |
openrouter_key = os.environ.get("OPENROUTER_API_KEY")
|
|
|
|
|
17 |
|
18 |
+
# Check API key
|
19 |
+
if not openrouter_key:
|
20 |
+
raise RuntimeError(
|
21 |
+
"❌ OPENROUTER_API_KEY not set. Please set it in your .env file."
|
22 |
+
)
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# ----- Model Configs -----
|
25 |
MODEL_CONFIGS = {
|
26 |
+
"gemini": {
|
27 |
+
"model": "openrouter/google/gemini-2.5-flash-preview-05-20",
|
28 |
"api_base": "https://openrouter.ai/api/v1",
|
29 |
"api_key": openrouter_key,
|
30 |
"temperature": 0.3,
|
31 |
+
"max_tokens": 1500,
|
32 |
},
|
33 |
+
"claude": {
|
34 |
+
"model": "openrouter/anthropic/claude-sonnet-4",
|
35 |
"api_base": "https://openrouter.ai/api/v1",
|
36 |
"api_key": openrouter_key,
|
37 |
"temperature": 0.7,
|
38 |
+
"max_tokens": 2000,
|
39 |
},
|
40 |
+
"deepseek": {
|
41 |
+
"model": "openrouter/deepseek/deepseek-r1-0528",
|
42 |
"api_base": "https://openrouter.ai/api/v1",
|
43 |
"api_key": openrouter_key,
|
44 |
"temperature": 0.6,
|
45 |
+
"max_tokens": 3000,
|
46 |
},
|
47 |
+
"llama": {
|
48 |
+
"model": "openrouter/meta-llama/llama-4-scout",
|
49 |
"api_base": "https://openrouter.ai/api/v1",
|
50 |
"api_key": openrouter_key,
|
51 |
"temperature": 0.6,
|
52 |
+
"max_tokens": 2500,
|
53 |
},
|
54 |
+
"mistral": {
|
55 |
+
"model": "openrouter/mistralai/mistral-small-3.1-24b-instruct",
|
56 |
"api_base": "https://openrouter.ai/api/v1",
|
57 |
"api_key": openrouter_key,
|
58 |
"temperature": 0.8,
|
|
|
60 |
},
|
61 |
}
|
62 |
|
63 |
+
# ----- Tool Functions -----
|
64 |
+
|
65 |
+
def proctor_expert_cot(problem: str) -> dict:
|
66 |
+
"""
|
67 |
+
Chain-of-Thought, Verification, and Role Prompting on Gemini.
|
68 |
+
"""
|
69 |
+
technique = CompositeTechnique(
|
70 |
name="Expert Chain-of-Thought",
|
71 |
identifier="custom-expert-cot",
|
72 |
+
techniques=[
|
73 |
+
RolePrompting(),
|
74 |
+
ChainOfThought(),
|
75 |
+
ChainOfVerification(),
|
76 |
+
],
|
77 |
+
)
|
78 |
+
response = technique.execute(
|
79 |
+
problem,
|
80 |
+
llm_config=MODEL_CONFIGS["gemini"],
|
81 |
+
role="Expert House Builder and Construction Manager"
|
82 |
+
)
|
83 |
+
return {
|
84 |
+
"model": "Google Gemini 2.5 Flash",
|
85 |
+
"technique": "Expert Chain-of-Thought",
|
86 |
+
"response": response
|
87 |
+
}
|
88 |
|
89 |
+
def proctor_claude_cot(problem: str) -> dict:
|
90 |
"""
|
91 |
+
Chain-of-Thought with Claude 4 Sonnet.
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
"""
|
93 |
+
technique = ChainOfThought()
|
94 |
+
response = technique.execute(problem, llm_config=MODEL_CONFIGS["claude"])
|
95 |
+
return {
|
96 |
+
"model": "Claude 4 Sonnet",
|
97 |
+
"technique": "Chain-of-Thought",
|
98 |
+
"response": response
|
99 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
def proctor_deepseek_reasoning(problem: str) -> dict:
|
102 |
"""
|
103 |
+
Deep reasoning with DeepSeek R1: CoT, SelfAsk, Verification.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
"""
|
105 |
+
technique = CompositeTechnique(
|
106 |
+
name="Deep Reasoning Analysis",
|
107 |
+
identifier="deep-reasoning",
|
108 |
+
techniques=[
|
109 |
+
ChainOfThought(),
|
110 |
+
SelfAsk(),
|
111 |
+
ChainOfVerification(),
|
112 |
+
],
|
113 |
+
)
|
114 |
+
response = technique.execute(problem, llm_config=MODEL_CONFIGS["deepseek"])
|
115 |
+
return {
|
116 |
+
"model": "DeepSeek R1",
|
117 |
+
"technique": "Deep Reasoning Analysis",
|
118 |
+
"response": response
|
119 |
+
}
|
120 |
|
121 |
+
def proctor_llama_emotion(problem: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
"""
|
123 |
+
Emotion Prompting with Llama 4 Scout.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
"""
|
125 |
+
technique = EmotionPrompting()
|
126 |
+
response = technique.execute(
|
127 |
+
problem,
|
128 |
+
llm_config=MODEL_CONFIGS["llama"],
|
129 |
+
emotion="thoughtful and methodical"
|
130 |
+
)
|
131 |
+
return {
|
132 |
+
"model": "Llama 4 Scout",
|
133 |
+
"technique": "Emotion Prompting",
|
134 |
+
"response": response
|
135 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
+
def proctor_mistral_tips(problem: str) -> dict:
|
138 |
"""
|
139 |
+
Fast Role Prompting with Mistral Small (for quick suggestions).
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
"""
|
141 |
+
technique = RolePrompting()
|
142 |
+
response = technique.execute(
|
143 |
+
problem,
|
144 |
+
llm_config=MODEL_CONFIGS["mistral"],
|
145 |
+
role="Construction Project Manager"
|
146 |
+
)
|
147 |
return {
|
148 |
+
"model": "Mistral Small 3.1 24B",
|
149 |
+
"technique": "Role Prompting",
|
150 |
+
"response": response
|
151 |
}
|
152 |
|
153 |
+
# Optionally, expose a unified tool for arbitrary model/technique selection:
|
154 |
+
def proctor_flexible(
|
155 |
+
problem: str,
|
156 |
+
model: str = "gemini",
|
157 |
+
technique: str = "ChainOfThought",
|
158 |
+
role: str = "",
|
159 |
+
emotion: str = ""
|
160 |
+
) -> dict:
|
|
|
161 |
"""
|
162 |
+
Flexible interface for any model/technique combo.
|
163 |
+
"""
|
164 |
+
technique_map = {
|
165 |
+
"ChainOfThought": ChainOfThought,
|
166 |
+
"RolePrompting": RolePrompting,
|
167 |
+
"EmotionPrompting": EmotionPrompting,
|
168 |
+
"SelfAsk": SelfAsk,
|
169 |
+
"ChainOfVerification": ChainOfVerification,
|
170 |
+
}
|
171 |
+
if technique == "CompositeExpert":
|
172 |
+
tech = CompositeTechnique(
|
173 |
+
name="Expert Chain-of-Thought",
|
174 |
+
identifier="custom-expert-cot",
|
175 |
+
techniques=[
|
176 |
+
RolePrompting(),
|
177 |
+
ChainOfThought(),
|
178 |
+
ChainOfVerification(),
|
179 |
+
],
|
180 |
+
)
|
181 |
+
response = tech.execute(problem, llm_config=MODEL_CONFIGS[model], role=role)
|
182 |
+
elif technique == "DeepReasoning":
|
183 |
+
tech = CompositeTechnique(
|
184 |
+
name="Deep Reasoning Analysis",
|
185 |
+
identifier="deep-reasoning",
|
186 |
+
techniques=[
|
187 |
+
ChainOfThought(),
|
188 |
+
SelfAsk(),
|
189 |
+
ChainOfVerification(),
|
190 |
+
],
|
191 |
+
)
|
192 |
+
response = tech.execute(problem, llm_config=MODEL_CONFIGS[model])
|
193 |
+
else:
|
194 |
+
tech_cls = technique_map.get(technique, ChainOfThought)
|
195 |
+
if technique == "RolePrompting":
|
196 |
+
response = tech_cls().execute(problem, llm_config=MODEL_CONFIGS[model], role=role)
|
197 |
+
elif technique == "EmotionPrompting":
|
198 |
+
response = tech_cls().execute(problem, llm_config=MODEL_CONFIGS[model], emotion=emotion)
|
199 |
+
else:
|
200 |
+
response = tech_cls().execute(problem, llm_config=MODEL_CONFIGS[model])
|
201 |
+
return {
|
202 |
+
"model": MODEL_CONFIGS[model]["model"],
|
203 |
+
"technique": technique,
|
204 |
+
"response": response
|
205 |
+
}
|
206 |
+
|
207 |
+
# ----- Gradio/MCP Interface -----
|
208 |
+
|
209 |
+
with gr.Blocks() as demo:
|
210 |
+
gr.Markdown("# 🏗️ Proctor AI MCP Server\nAdvanced prompt engineering tools via OpenRouter and Proctor AI.\n\n*Try from an MCP-compatible client or the web UI below!*")
|
211 |
+
with gr.Tab("Gemini (Expert CoT)"):
|
212 |
+
gr.Interface(fn=proctor_expert_cot, inputs=gr.Textbox(label="Problem"), outputs=gr.JSON(), allow_flagging="never")
|
213 |
+
with gr.Tab("Claude 4 (Chain-of-Thought)"):
|
214 |
+
gr.Interface(fn=proctor_claude_cot, inputs=gr.Textbox(label="Problem"), outputs=gr.JSON(), allow_flagging="never")
|
215 |
+
with gr.Tab("DeepSeek R1 (Deep Reasoning)"):
|
216 |
+
gr.Interface(fn=proctor_deepseek_reasoning, inputs=gr.Textbox(label="Problem"), outputs=gr.JSON(), allow_flagging="never")
|
217 |
+
with gr.Tab("Llama 4 (Emotion Prompting)"):
|
218 |
+
gr.Interface(fn=proctor_llama_emotion, inputs=gr.Textbox(label="Problem"), outputs=gr.JSON(), allow_flagging="never")
|
219 |
+
with gr.Tab("Mistral (Quick Tips)"):
|
220 |
+
gr.Interface(fn=proctor_mistral_tips, inputs=gr.Textbox(label="Problem (tips request)"), outputs=gr.JSON(), allow_flagging="never")
|
221 |
+
with gr.Tab("Flexible (Advanced)"):
|
222 |
+
model_dropdown = gr.Dropdown(choices=list(MODEL_CONFIGS.keys()), value="gemini", label="Model")
|
223 |
+
technique_dropdown = gr.Dropdown(
|
224 |
+
choices=["ChainOfThought", "RolePrompting", "EmotionPrompting", "SelfAsk", "ChainOfVerification", "CompositeExpert", "DeepReasoning"],
|
225 |
+
value="ChainOfThought",
|
226 |
+
label="Technique"
|
227 |
+
)
|
228 |
+
role_input = gr.Textbox(label="Role (optional)", value="")
|
229 |
+
emotion_input = gr.Textbox(label="Emotion (optional)", value="")
|
230 |
+
flexible_iface = gr.Interface(
|
231 |
+
fn=proctor_flexible,
|
232 |
+
inputs=[gr.Textbox(label="Problem"),
|
233 |
+
model_dropdown,
|
234 |
+
technique_dropdown,
|
235 |
+
role_input,
|
236 |
+
emotion_input],
|
237 |
+
outputs=gr.JSON(),
|
238 |
+
allow_flagging="never"
|
239 |
+
)
|
240 |
|
|
|
241 |
if __name__ == "__main__":
|
242 |
+
demo.launch(mcp_server=True)
|