Commit
·
935119a
1
Parent(s):
c510317
commit
Browse files- README.md +8 -0
- app.py +93 -4
- requirements.txt +2 -1
README.md
CHANGED
|
@@ -11,3 +11,11 @@ short_description: Reformats system prompts in the 2nd person and other edits
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
+
|
| 15 |
+
## OpenAI Key (Optional)
|
| 16 |
+
|
| 17 |
+
- Enter your OpenAI API key to use GPT-4o-mini for reformatting.
|
| 18 |
+
- Leave it blank to use the offline heuristic (no external calls).
|
| 19 |
+
- You can also set a Space Secret `OPENAI_API_KEY` to avoid typing it.
|
| 20 |
+
|
| 21 |
+
If the key is missing or the request fails, the app automatically falls back to the offline heuristic.
|
app.py
CHANGED
|
@@ -1,7 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
Reformats system prompts according to the specified guidelines
|
| 7 |
"""
|
|
@@ -43,6 +61,71 @@ def reformat_system_prompt(input_text):
|
|
| 43 |
|
| 44 |
return f"```\n{reformatted}\n```", name, description
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def extract_assistant_name(text):
|
| 47 |
"""
|
| 48 |
Extract a name for the assistant based on its functionality
|
|
@@ -109,6 +192,12 @@ with gr.Blocks(title="System Prompt Reformatter", theme=gr.themes.Soft()) as dem
|
|
| 109 |
lines=10,
|
| 110 |
max_lines=20
|
| 111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
submit_btn = gr.Button("Reformat Prompt", variant="primary")
|
| 113 |
|
| 114 |
with gr.Column(scale=1):
|
|
@@ -129,7 +218,7 @@ with gr.Blocks(title="System Prompt Reformatter", theme=gr.themes.Soft()) as dem
|
|
| 129 |
|
| 130 |
submit_btn.click(
|
| 131 |
fn=reformat_system_prompt,
|
| 132 |
-
inputs=[input_text],
|
| 133 |
outputs=[output_text, assistant_name, description]
|
| 134 |
)
|
| 135 |
|
|
@@ -219,4 +308,4 @@ You assist the user in the following manner.
|
|
| 219 |
""")
|
| 220 |
|
| 221 |
if __name__ == "__main__":
|
| 222 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
SYSTEM_INSTRUCTIONS = (
|
| 8 |
+
"Your task is to lightly edit system prompts for AI tools to comply with a specific stylistic convention.\n\n"
|
| 9 |
+
"Editing rules:\n"
|
| 10 |
+
"- Write in second person (you/your).\n"
|
| 11 |
+
"- Organize with brief paragraphs and optional markdown headers when helpful.\n"
|
| 12 |
+
"- Make ecosystem-agnostic (e.g., replace 'custom GPT'/'ChatGPT' with 'assistant').\n"
|
| 13 |
+
"- Generalize user-specific language to 'the user'.\n\n"
|
| 14 |
+
"Outputs (JSON object ONLY): {\n"
|
| 15 |
+
" \"reformatted\": string (the edited system prompt, no extra commentary, no backticks),\n"
|
| 16 |
+
" \"name\": string (assistant name),\n"
|
| 17 |
+
" \"description\": string (single-sentence description)\n"
|
| 18 |
+
"}"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def reformat_system_prompt_offline(input_text):
|
| 23 |
"""
|
| 24 |
Reformats system prompts according to the specified guidelines
|
| 25 |
"""
|
|
|
|
| 61 |
|
| 62 |
return f"```\n{reformatted}\n```", name, description
|
| 63 |
|
| 64 |
+
|
| 65 |
+
def reformat_system_prompt_llm(input_text: str, api_key: str):
|
| 66 |
+
"""
|
| 67 |
+
Use an LLM (OpenAI-compatible chat.completions) to reformat the prompt and
|
| 68 |
+
produce name + description. Returns (reformatted_md, name, description).
|
| 69 |
+
|
| 70 |
+
- api_key: user-provided OpenAI API key (not stored). Uses gpt-4o-mini.
|
| 71 |
+
"""
|
| 72 |
+
if not input_text.strip():
|
| 73 |
+
return "Please provide a system prompt to reformat.", "", ""
|
| 74 |
+
|
| 75 |
+
if not api_key:
|
| 76 |
+
# Try environment fallback (Space secret) before giving up
|
| 77 |
+
api_key = os.environ.get("OPENAI_API_KEY", "").strip()
|
| 78 |
+
if not api_key:
|
| 79 |
+
# Fallback to offline if key still missing
|
| 80 |
+
return reformat_system_prompt_offline(input_text)
|
| 81 |
+
|
| 82 |
+
base_url = (os.environ.get("OPENAI_BASE_URL") or "https://api.openai.com/v1").rstrip("/")
|
| 83 |
+
url = f"{base_url}/chat/completions"
|
| 84 |
+
|
| 85 |
+
messages = [
|
| 86 |
+
{"role": "system", "content": SYSTEM_INSTRUCTIONS},
|
| 87 |
+
{"role": "user", "content": input_text.strip()},
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
payload = {
|
| 91 |
+
"model": "gpt-4o-mini",
|
| 92 |
+
"messages": messages,
|
| 93 |
+
"temperature": 0.2,
|
| 94 |
+
"response_format": {"type": "json_object"},
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
resp = requests.post(url, headers=headers, data=json.dumps(payload), timeout=30)
|
| 101 |
+
if resp.status_code != 200:
|
| 102 |
+
# Graceful fallback
|
| 103 |
+
return reformat_system_prompt_offline(input_text)
|
| 104 |
+
data = resp.json()
|
| 105 |
+
content = data["choices"][0]["message"]["content"]
|
| 106 |
+
parsed = json.loads(content)
|
| 107 |
+
reformatted = parsed.get("reformatted", "").strip()
|
| 108 |
+
name = parsed.get("name", "AI Assistant").strip()
|
| 109 |
+
description = parsed.get("description", "Provides AI assistance for various tasks.").strip()
|
| 110 |
+
if not reformatted:
|
| 111 |
+
return reformat_system_prompt_offline(input_text)
|
| 112 |
+
return f"```\n{reformatted}\n```", name, description
|
| 113 |
+
except Exception:
|
| 114 |
+
# Any parsing/network error → fallback
|
| 115 |
+
return reformat_system_prompt_offline(input_text)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def reformat_system_prompt(input_text, api_key):
|
| 119 |
+
"""
|
| 120 |
+
Use LLM if an API key is provided (or available via OPENAI_API_KEY),
|
| 121 |
+
otherwise use the offline heuristic.
|
| 122 |
+
"""
|
| 123 |
+
live_key = api_key or os.environ.get("OPENAI_API_KEY", "").strip()
|
| 124 |
+
if live_key:
|
| 125 |
+
return reformat_system_prompt_llm(input_text, live_key)
|
| 126 |
+
else:
|
| 127 |
+
return reformat_system_prompt_offline(input_text)
|
| 128 |
+
|
| 129 |
def extract_assistant_name(text):
|
| 130 |
"""
|
| 131 |
Extract a name for the assistant based on its functionality
|
|
|
|
| 192 |
lines=10,
|
| 193 |
max_lines=20
|
| 194 |
)
|
| 195 |
+
api_key = gr.Textbox(
|
| 196 |
+
label="OpenAI API Key (optional)",
|
| 197 |
+
placeholder="sk-... Leave blank to use offline reformatter",
|
| 198 |
+
type="password",
|
| 199 |
+
info="Tip: Set Space Secret OPENAI_API_KEY to avoid typing your key. The key is only used for this session."
|
| 200 |
+
)
|
| 201 |
submit_btn = gr.Button("Reformat Prompt", variant="primary")
|
| 202 |
|
| 203 |
with gr.Column(scale=1):
|
|
|
|
| 218 |
|
| 219 |
submit_btn.click(
|
| 220 |
fn=reformat_system_prompt,
|
| 221 |
+
inputs=[input_text, api_key],
|
| 222 |
outputs=[output_text, assistant_name, description]
|
| 223 |
)
|
| 224 |
|
|
|
|
| 308 |
""")
|
| 309 |
|
| 310 |
if __name__ == "__main__":
|
| 311 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
gradio==5.47.2
|
|
|
|
|
|
| 1 |
+
gradio==5.47.2
|
| 2 |
+
requests>=2.31.0
|