danielrosehill commited on
Commit
74fe076
·
unverified ·
1 Parent(s): 1c4d0ed
Files changed (2) hide show
  1. app.py +73 -11
  2. requirements.txt +4 -2
app.py CHANGED
@@ -1,14 +1,27 @@
1
  import gradio as gr
2
  import openai
3
  import os
 
 
4
 
5
  title = "System Prompt Depersonalizer"
6
  description = """
7
  This app transforms personalized system prompts into generalized versions that can be shared with a wider audience.
 
8
  """
9
 
 
 
 
 
 
 
 
10
  # Define depersonalization function using OpenAI (v1.0+ syntax)
11
- def depersonalize_prompt(prompt, api_key):
 
 
 
12
  try:
13
  client = openai.OpenAI(api_key=api_key)
14
  response = client.chat.completions.create(
@@ -40,48 +53,97 @@ Task Breakdown:
40
 
41
  Output Format:
42
  Provide only the depersonalized system prompt in Markdown format inside a code block. Do not include any other commentary or explanation.
43
- """},
44
  {"role": "user", "content": prompt}
45
  ],
46
  max_tokens=1200
47
  )
48
  return response.choices[0].message.content
49
  except Exception as e:
50
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # Build Gradio UI
53
  with gr.Blocks() as demo:
54
  gr.Markdown(f"# {title}")
55
  gr.Markdown(description)
56
-
 
 
 
57
  api_key_input = gr.Textbox(
58
  label="OpenAI API Key",
59
  placeholder="sk-...",
60
- type="password"
 
61
  )
62
-
63
  input_prompt = gr.Textbox(
64
  label="Personalized System Prompt",
65
  placeholder="Paste your personalized system prompt here...",
66
  lines=10
67
  )
68
-
69
  output_prompt = gr.Textbox(
70
  label="Depersonalized System Prompt",
71
  lines=10,
72
  interactive=True
73
  )
74
-
75
  with gr.Row():
76
  run_btn = gr.Button("Depersonalize")
77
  copy_btn = gr.Button("Copy Result")
78
-
 
 
 
 
 
 
 
 
 
 
79
  run_btn.click(
80
  fn=depersonalize_prompt,
81
- inputs=[input_prompt, api_key_input],
82
  outputs=output_prompt
83
  )
84
-
85
  copy_btn.click(
86
  fn=lambda x: x,
87
  inputs=[output_prompt],
 
1
  import gradio as gr
2
  import openai
3
  import os
4
+ import re
5
+ from transformers import pipeline
6
 
7
  title = "System Prompt Depersonalizer"
8
  description = """
9
  This app transforms personalized system prompts into generalized versions that can be shared with a wider audience.
10
+ You can use either OpenAI's API (requires API key) or Hugging Face's models (free).
11
  """
12
 
13
+ # Validate OpenAI API key format
14
+ def validate_api_key(api_key):
15
+ if not api_key:
16
+ return False
17
+ # Check if it starts with "sk-" and has appropriate length
18
+ return bool(re.match(r'^sk-[A-Za-z0-9]{32,}$', api_key))
19
+
20
  # Define depersonalization function using OpenAI (v1.0+ syntax)
21
+ def depersonalize_prompt_openai(prompt, api_key):
22
+ if not validate_api_key(api_key):
23
+ return "Error: Invalid API key format. OpenAI API keys should start with 'sk-' followed by at least 32 characters."
24
+
25
  try:
26
  client = openai.OpenAI(api_key=api_key)
27
  response = client.chat.completions.create(
 
53
 
54
  Output Format:
55
  Provide only the depersonalized system prompt in Markdown format inside a code block. Do not include any other commentary or explanation.
56
+ """},
57
  {"role": "user", "content": prompt}
58
  ],
59
  max_tokens=1200
60
  )
61
  return response.choices[0].message.content
62
  except Exception as e:
63
+ error_msg = str(e)
64
+ if "API key" in error_msg.lower() or "authentication" in error_msg.lower():
65
+ return "Error: Your API key was rejected by OpenAI. Please check that you've entered a valid API key."
66
+ else:
67
+ return f"Error: {error_msg}"
68
+
69
+ # Define depersonalization function using Hugging Face models
70
+ def depersonalize_prompt_hf(prompt):
71
+ try:
72
+ # Use a text generation pipeline with a suitable model
73
+ generator = pipeline('text2text-generation', model='google/flan-t5-base')
74
+
75
+ # Create a prompt that instructs the model to depersonalize
76
+ instruction = """
77
+ Transform this personalized system prompt into a generalized version by removing personal elements
78
+ (names, specific hardware/software, locations, unique use cases, personal preferences)
79
+ while preserving the core functionality and purpose:
80
+ """
81
+
82
+ full_prompt = instruction + "\n\n" + prompt
83
+
84
+ # Generate the depersonalized version
85
+ result = generator(full_prompt, max_length=1024, do_sample=False)
86
+
87
+ return result[0]['generated_text']
88
+ except Exception as e:
89
+ return f"Error with Hugging Face model: {str(e)}"
90
+
91
+ # Function to route to the appropriate depersonalization method
92
+ def depersonalize_prompt(prompt, api_key, use_openai):
93
+ if use_openai:
94
+ if not api_key.strip():
95
+ return "Error: OpenAI API key is required when using OpenAI. Please enter your API key or switch to Hugging Face."
96
+ return depersonalize_prompt_openai(prompt, api_key)
97
+ else:
98
+ return depersonalize_prompt_hf(prompt)
99
 
100
  # Build Gradio UI
101
  with gr.Blocks() as demo:
102
  gr.Markdown(f"# {title}")
103
  gr.Markdown(description)
104
+
105
+ with gr.Row():
106
+ use_openai = gr.Checkbox(label="Use OpenAI (requires API key)", value=True)
107
+
108
  api_key_input = gr.Textbox(
109
  label="OpenAI API Key",
110
  placeholder="sk-...",
111
+ type="password",
112
+ visible=True
113
  )
114
+
115
  input_prompt = gr.Textbox(
116
  label="Personalized System Prompt",
117
  placeholder="Paste your personalized system prompt here...",
118
  lines=10
119
  )
120
+
121
  output_prompt = gr.Textbox(
122
  label="Depersonalized System Prompt",
123
  lines=10,
124
  interactive=True
125
  )
126
+
127
  with gr.Row():
128
  run_btn = gr.Button("Depersonalize")
129
  copy_btn = gr.Button("Copy Result")
130
+
131
+ # Update API key input visibility based on checkbox
132
+ def update_api_key_visibility(use_openai):
133
+ return gr.update(visible=use_openai)
134
+
135
+ use_openai.change(
136
+ fn=update_api_key_visibility,
137
+ inputs=[use_openai],
138
+ outputs=[api_key_input]
139
+ )
140
+
141
  run_btn.click(
142
  fn=depersonalize_prompt,
143
+ inputs=[input_prompt, api_key_input, use_openai],
144
  outputs=output_prompt
145
  )
146
+
147
  copy_btn.click(
148
  fn=lambda x: x,
149
  inputs=[output_prompt],
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
- gradio
2
- openai
 
 
 
1
+ gradio>=5.29.0
2
+ openai>=1.0.0
3
+ transformers>=4.30.0
4
+ torch>=2.0.0