Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from proctor import (
|
| 5 |
CompositeTechnique,
|
| 6 |
RolePrompting,
|
|
@@ -12,6 +14,10 @@ from proctor import (
|
|
| 12 |
list_techniques,
|
| 13 |
)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
| 17 |
|
|
@@ -28,33 +34,34 @@ MODELS = {
|
|
| 28 |
"Llama 4 Scout": "openrouter/meta-llama/llama-4-scout",
|
| 29 |
"Mistral Small 3.1 24B": "openrouter/mistralai/mistral-small-3.1-24b-instruct",
|
| 30 |
}
|
|
|
|
| 31 |
TECHNIQUES = list_techniques()
|
| 32 |
|
| 33 |
-
# Model configurations
|
| 34 |
MODEL_CONFIGS = {
|
| 35 |
"openrouter/google/gemini-2.5-flash-preview-05-20": {
|
| 36 |
"api_base": "https://openrouter.ai/api/v1",
|
| 37 |
"api_key": openrouter_key,
|
| 38 |
"temperature": 0.3,
|
| 39 |
-
"max_tokens":
|
| 40 |
},
|
| 41 |
"openrouter/anthropic/claude-sonnet-4": {
|
| 42 |
"api_base": "https://openrouter.ai/api/v1",
|
| 43 |
"api_key": openrouter_key,
|
| 44 |
"temperature": 0.7,
|
| 45 |
-
"max_tokens":
|
| 46 |
},
|
| 47 |
"openrouter/deepseek/deepseek-r1-0528": {
|
| 48 |
"api_base": "https://openrouter.ai/api/v1",
|
| 49 |
"api_key": openrouter_key,
|
| 50 |
"temperature": 0.6,
|
| 51 |
-
"max_tokens":
|
| 52 |
},
|
| 53 |
"openrouter/meta-llama/llama-4-scout": {
|
| 54 |
"api_base": "https://openrouter.ai/api/v1",
|
| 55 |
"api_key": openrouter_key,
|
| 56 |
"temperature": 0.6,
|
| 57 |
-
"max_tokens":
|
| 58 |
},
|
| 59 |
"openrouter/mistralai/mistral-small-3.1-24b-instruct": {
|
| 60 |
"api_base": "https://openrouter.ai/api/v1",
|
|
@@ -83,11 +90,19 @@ TECHNIQUE_CONFIGS = {
|
|
| 83 |
"ZeroShotCoT": ZeroShotCoT(),
|
| 84 |
}
|
| 85 |
|
| 86 |
-
def format_as_markdown(response):
|
| 87 |
"""
|
| 88 |
Format the response as Markdown for better readability.
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
"""
|
|
|
|
|
|
|
|
|
|
| 91 |
lines = response.split("\n")
|
| 92 |
formatted_lines = []
|
| 93 |
in_list = False
|
|
@@ -100,12 +115,12 @@ def format_as_markdown(response):
|
|
| 100 |
continue
|
| 101 |
|
| 102 |
# Check for headings (e.g., "Target Market:")
|
| 103 |
-
if line.endswith(":") and not line.startswith("-"):
|
| 104 |
formatted_lines.append(f"### {line}")
|
| 105 |
continue
|
| 106 |
|
| 107 |
# Check for list items (e.g., "- Item" or "1. Item")
|
| 108 |
-
if line.startswith("-") or line[0].isdigit() and line[1:3] in [". ", ".("]:
|
| 109 |
in_list = True
|
| 110 |
formatted_lines.append(line)
|
| 111 |
continue
|
|
@@ -118,66 +133,191 @@ def format_as_markdown(response):
|
|
| 118 |
|
| 119 |
return "\n".join(formatted_lines)
|
| 120 |
|
| 121 |
-
def
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
return "Please enter a problem statement."
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
|
| 133 |
try:
|
| 134 |
-
#
|
| 135 |
kwargs = {"llm_config": llm_config}
|
|
|
|
|
|
|
| 136 |
if technique_name == "RolePrompting":
|
| 137 |
-
kwargs["role"] = role or "Expert"
|
| 138 |
elif technique_name == "EmotionPrompting":
|
| 139 |
-
kwargs["emotion"] = emotion or "thoughtful and methodical"
|
| 140 |
elif technique_name == "Expert Chain-of-Thought":
|
| 141 |
-
kwargs["role"] = role or "Expert"
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
# Format the response as Markdown
|
| 145 |
markdown_response = format_as_markdown(response)
|
|
|
|
| 146 |
return markdown_response
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
gr.Markdown("Enter a problem, select a technique and model, and get a response powered by OpenRouter.")
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
submit_button.click(
|
| 174 |
fn=process_problem,
|
| 175 |
inputs=[problem_input, technique_dropdown, model_dropdown, role_input, emotion_input],
|
| 176 |
outputs=output
|
| 177 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
# Launch the app
|
| 180 |
if __name__ == "__main__":
|
| 181 |
interface.launch(
|
| 182 |
-
share=True
|
| 183 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import os
|
| 4 |
+
import logging
|
| 5 |
+
from typing import Dict, Any, Optional
|
| 6 |
from proctor import (
|
| 7 |
CompositeTechnique,
|
| 8 |
RolePrompting,
|
|
|
|
| 14 |
list_techniques,
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Configure logging
|
| 18 |
+
logging.basicConfig(level=logging.INFO)
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
# Load environment variables
|
| 22 |
load_dotenv()
|
| 23 |
|
|
|
|
| 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 configurations with optimized parameters
|
| 41 |
MODEL_CONFIGS = {
|
| 42 |
"openrouter/google/gemini-2.5-flash-preview-05-20": {
|
| 43 |
"api_base": "https://openrouter.ai/api/v1",
|
| 44 |
"api_key": openrouter_key,
|
| 45 |
"temperature": 0.3,
|
| 46 |
+
"max_tokens": 15000,
|
| 47 |
},
|
| 48 |
"openrouter/anthropic/claude-sonnet-4": {
|
| 49 |
"api_base": "https://openrouter.ai/api/v1",
|
| 50 |
"api_key": openrouter_key,
|
| 51 |
"temperature": 0.7,
|
| 52 |
+
"max_tokens": 12000,
|
| 53 |
},
|
| 54 |
"openrouter/deepseek/deepseek-r1-0528": {
|
| 55 |
"api_base": "https://openrouter.ai/api/v1",
|
| 56 |
"api_key": openrouter_key,
|
| 57 |
"temperature": 0.6,
|
| 58 |
+
"max_tokens": 8000,
|
| 59 |
},
|
| 60 |
"openrouter/meta-llama/llama-4-scout": {
|
| 61 |
"api_base": "https://openrouter.ai/api/v1",
|
| 62 |
"api_key": openrouter_key,
|
| 63 |
"temperature": 0.6,
|
| 64 |
+
"max_tokens": 12500,
|
| 65 |
},
|
| 66 |
"openrouter/mistralai/mistral-small-3.1-24b-instruct": {
|
| 67 |
"api_base": "https://openrouter.ai/api/v1",
|
|
|
|
| 90 |
"ZeroShotCoT": ZeroShotCoT(),
|
| 91 |
}
|
| 92 |
|
| 93 |
+
def format_as_markdown(response: str) -> str:
|
| 94 |
"""
|
| 95 |
Format the response as Markdown for better readability.
|
| 96 |
+
|
| 97 |
+
Args:
|
| 98 |
+
response: The raw response text to format
|
| 99 |
+
|
| 100 |
+
Returns:
|
| 101 |
+
Formatted markdown string
|
| 102 |
"""
|
| 103 |
+
if not response:
|
| 104 |
+
return ""
|
| 105 |
+
|
| 106 |
lines = response.split("\n")
|
| 107 |
formatted_lines = []
|
| 108 |
in_list = False
|
|
|
|
| 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
|
|
|
|
| 133 |
|
| 134 |
return "\n".join(formatted_lines)
|
| 135 |
|
| 136 |
+
def validate_inputs(problem: str, technique_name: str, model_name: str) -> Optional[str]:
|
| 137 |
+
"""
|
| 138 |
+
Validate user inputs and return error message if invalid.
|
| 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 |
+
if not problem or not problem.strip():
|
| 149 |
return "Please enter a problem statement."
|
| 150 |
|
| 151 |
+
if technique_name not in TECHNIQUE_CONFIGS:
|
| 152 |
+
return f"Technique '{technique_name}' not found."
|
| 153 |
+
|
| 154 |
+
if model_name not in MODELS:
|
| 155 |
+
return f"Model '{model_name}' not found."
|
| 156 |
+
|
| 157 |
+
return None
|
| 158 |
+
|
| 159 |
+
def process_problem(
|
| 160 |
+
problem: str,
|
| 161 |
+
technique_name: str,
|
| 162 |
+
model_name: str,
|
| 163 |
+
role: str = "",
|
| 164 |
+
emotion: str = ""
|
| 165 |
+
) -> str:
|
| 166 |
+
"""
|
| 167 |
+
Process the problem using the selected technique and model.
|
| 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 |
+
# Validate inputs
|
| 180 |
+
validation_error = validate_inputs(problem, technique_name, model_name)
|
| 181 |
+
if validation_error:
|
| 182 |
+
return f"**Error**: {validation_error}"
|
| 183 |
|
| 184 |
+
technique = TECHNIQUE_CONFIGS[technique_name]
|
| 185 |
+
model_id = MODELS[model_name]
|
| 186 |
+
llm_config = MODEL_CONFIGS[model_id]
|
| 187 |
|
| 188 |
try:
|
| 189 |
+
# Prepare kwargs for technique execution
|
| 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 update_input_visibility(technique: str) -> Dict[str, Any]:
|
| 214 |
+
"""
|
| 215 |
+
Update visibility of role and emotion inputs based on selected technique.
|
|
|
|
| 216 |
|
| 217 |
+
Args:
|
| 218 |
+
technique: Selected technique name
|
| 219 |
+
|
| 220 |
+
Returns:
|
| 221 |
+
Dictionary with visibility updates for inputs
|
| 222 |
+
"""
|
| 223 |
+
show_role = technique in ["RolePrompting", "Expert Chain-of-Thought"]
|
| 224 |
+
show_emotion = technique == "EmotionPrompting"
|
| 225 |
|
| 226 |
+
return {
|
| 227 |
+
role_input: gr.update(visible=show_role),
|
| 228 |
+
emotion_input: gr.update(visible=show_emotion)
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
# Create Gradio interface with improved styling
|
| 232 |
+
with gr.Blocks(
|
| 233 |
+
title="Proctor AI Prompt Engineering App",
|
| 234 |
+
theme=gr.themes.Soft(),
|
| 235 |
+
css="""
|
| 236 |
+
.gradio-container {
|
| 237 |
+
max-width: 1200px !important;
|
| 238 |
+
margin: auto !important;
|
| 239 |
+
}
|
| 240 |
+
"""
|
| 241 |
+
) as interface:
|
| 242 |
+
gr.Markdown(
|
| 243 |
+
"""
|
| 244 |
+
# 🤖 Proctor AI Prompt Engineering App
|
| 245 |
+
|
| 246 |
+
**Enhance your problem-solving with advanced AI prompting techniques**
|
| 247 |
+
|
| 248 |
+
Enter a problem, select a technique and model, and get intelligent responses powered by OpenRouter.
|
| 249 |
+
"""
|
| 250 |
+
)
|
| 251 |
|
| 252 |
+
with gr.Row():
|
| 253 |
+
with gr.Column(scale=2):
|
| 254 |
+
problem_input = gr.Textbox(
|
| 255 |
+
label="Problem Statement",
|
| 256 |
+
placeholder="e.g., How to build a sustainable house for a family of 4?",
|
| 257 |
+
lines=3,
|
| 258 |
+
max_lines=5
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
with gr.Row():
|
| 262 |
+
technique_dropdown = gr.Dropdown(
|
| 263 |
+
choices=list(TECHNIQUE_CONFIGS.keys()),
|
| 264 |
+
label="Prompting Technique",
|
| 265 |
+
value=list(TECHNIQUE_CONFIGS.keys())[0] if TECHNIQUE_CONFIGS else None
|
| 266 |
+
)
|
| 267 |
+
model_dropdown = gr.Dropdown(
|
| 268 |
+
choices=list(MODELS.keys()),
|
| 269 |
+
label="Model",
|
| 270 |
+
value=list(MODELS.keys())[0] if MODELS else None
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
role_input = gr.Textbox(
|
| 274 |
+
label="Role (for RolePrompting or Expert CoT)",
|
| 275 |
+
placeholder="e.g., Expert Architect",
|
| 276 |
+
visible=False
|
| 277 |
+
)
|
| 278 |
+
emotion_input = gr.Textbox(
|
| 279 |
+
label="Emotion (for EmotionPrompting)",
|
| 280 |
+
placeholder="e.g., thoughtful and methodical",
|
| 281 |
+
visible=False
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
submit_button = gr.Button(
|
| 285 |
+
"🚀 Generate Response",
|
| 286 |
+
variant="primary",
|
| 287 |
+
size="lg"
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
with gr.Column(scale=3):
|
| 291 |
+
output = gr.Markdown(
|
| 292 |
+
label="Response",
|
| 293 |
+
value="*Your response will appear here...*"
|
| 294 |
+
)
|
| 295 |
|
| 296 |
+
# Event handlers
|
| 297 |
+
technique_dropdown.change(
|
| 298 |
+
fn=update_input_visibility,
|
| 299 |
+
inputs=technique_dropdown,
|
| 300 |
+
outputs=[role_input, emotion_input]
|
| 301 |
+
)
|
| 302 |
|
| 303 |
submit_button.click(
|
| 304 |
fn=process_problem,
|
| 305 |
inputs=[problem_input, technique_dropdown, model_dropdown, role_input, emotion_input],
|
| 306 |
outputs=output
|
| 307 |
)
|
| 308 |
+
|
| 309 |
+
# Add examples
|
| 310 |
+
gr.Examples(
|
| 311 |
+
examples=[
|
| 312 |
+
["How can I improve team productivity in a remote work environment?", "Expert Chain-of-Thought", "Claude 4 Sonnet", "Management Consultant", ""],
|
| 313 |
+
["What are the key factors to consider when starting a tech startup?", "Deep Reasoning Analysis", "Google Gemini 2.5 Flash", "", ""],
|
| 314 |
+
["How do I create a sustainable garden in a small urban space?", "RolePrompting", "DeepSeek R1", "Urban Gardening Expert", ""],
|
| 315 |
+
],
|
| 316 |
+
inputs=[problem_input, technique_dropdown, model_dropdown, role_input, emotion_input],
|
| 317 |
+
)
|
| 318 |
|
| 319 |
# Launch the app
|
| 320 |
if __name__ == "__main__":
|
| 321 |
interface.launch(
|
| 322 |
+
share=True,
|
| 323 |
)
|