Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,32 +8,43 @@ def _resolve_openai_key(user_input: str) -> str | None:
|
|
| 8 |
"""
|
| 9 |
If `user_input` is an 8-digit code matching BACKDOOR_KEY (Space secret),
|
| 10 |
return the OPENAI_KEY (Space secret). Otherwise return `user_input`.
|
|
|
|
| 11 |
"""
|
| 12 |
user_input = (user_input or "").strip()
|
| 13 |
|
| 14 |
backdoor_code = (os.getenv("BACKDOOR_KEY") or "").strip()
|
|
|
|
| 15 |
is_candidate_code = user_input.isdigit() and len(user_input) == 8 and backdoor_code
|
| 16 |
|
| 17 |
if is_candidate_code and hmac.compare_digest(user_input, backdoor_code):
|
|
|
|
| 18 |
resolved = os.getenv("OPENAI_KEY")
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
return user_input if user_input else None
|
| 22 |
|
| 23 |
|
| 24 |
-
def complete_text(prompt, max_tokens, temperature, top_p,
|
| 25 |
"""
|
| 26 |
-
Get a plain text completion from OpenAI
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
resolved_key = _resolve_openai_key(openai_api_key_input)
|
| 29 |
if not resolved_key:
|
| 30 |
-
return "⚠️ Missing or invalid API key."
|
| 31 |
|
| 32 |
try:
|
| 33 |
client = OpenAI(api_key=resolved_key)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
prompt=prompt,
|
| 38 |
max_tokens=int(max_tokens),
|
| 39 |
temperature=float(temperature),
|
|
@@ -41,13 +52,6 @@ def complete_text(prompt, max_tokens, temperature, top_p, seed, openai_api_key_i
|
|
| 41 |
stream=True,
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# Only pass seed if user entered one
|
| 45 |
-
if seed and str(seed).isdigit():
|
| 46 |
-
kwargs["seed"] = int(seed)
|
| 47 |
-
|
| 48 |
-
response_text = ""
|
| 49 |
-
stream = client.completions.create(**kwargs)
|
| 50 |
-
|
| 51 |
for event in stream:
|
| 52 |
if hasattr(event, "choices") and event.choices:
|
| 53 |
token = event.choices[0].text or ""
|
|
@@ -55,12 +59,15 @@ def complete_text(prompt, max_tokens, temperature, top_p, seed, openai_api_key_i
|
|
| 55 |
yield response_text
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.Markdown("## ✍️ Text Completion Demo (OpenAI instruct)")
|
| 63 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=2):
|
|
@@ -78,11 +85,6 @@ with gr.Blocks() as demo:
|
|
| 78 |
top_p = gr.Slider(
|
| 79 |
minimum=0.1, maximum=1.0, value=1.0, step=0.05, label="Top-p"
|
| 80 |
)
|
| 81 |
-
seed = gr.Textbox(
|
| 82 |
-
placeholder="Optional integer seed for reproducibility",
|
| 83 |
-
label="🎲 Seed",
|
| 84 |
-
type="text",
|
| 85 |
-
)
|
| 86 |
api_key = gr.Textbox(
|
| 87 |
placeholder="sk-... Paste your OpenAI API key here (or enter 8-digit passcode)",
|
| 88 |
label="🔑 OpenAI API Key",
|
|
@@ -97,9 +99,10 @@ with gr.Blocks() as demo:
|
|
| 97 |
|
| 98 |
submit.click(
|
| 99 |
fn=complete_text,
|
| 100 |
-
inputs=[prompt, max_tokens, temperature, top_p,
|
| 101 |
outputs=output,
|
| 102 |
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
|
|
|
| 105 |
demo.launch()
|
|
|
|
| 8 |
"""
|
| 9 |
If `user_input` is an 8-digit code matching BACKDOOR_KEY (Space secret),
|
| 10 |
return the OPENAI_KEY (Space secret). Otherwise return `user_input`.
|
| 11 |
+
Never prints or returns secrets; just resolves which to use.
|
| 12 |
"""
|
| 13 |
user_input = (user_input or "").strip()
|
| 14 |
|
| 15 |
backdoor_code = (os.getenv("BACKDOOR_KEY") or "").strip()
|
| 16 |
+
# Only treat as backdoor attempt if it's exactly 8 digits and we have a BACKDOOR_KEY.
|
| 17 |
is_candidate_code = user_input.isdigit() and len(user_input) == 8 and backdoor_code
|
| 18 |
|
| 19 |
if is_candidate_code and hmac.compare_digest(user_input, backdoor_code):
|
| 20 |
+
# Use the real key stored as a Space secret
|
| 21 |
resolved = os.getenv("OPENAI_KEY")
|
| 22 |
+
if not resolved:
|
| 23 |
+
# Fail safe if OPENAI_KEY isn't configured
|
| 24 |
+
return None
|
| 25 |
+
return resolved
|
| 26 |
|
| 27 |
+
# Fallback to whatever the user typed (normal behavior)
|
| 28 |
return user_input if user_input else None
|
| 29 |
|
| 30 |
|
| 31 |
+
def complete_text(prompt, max_tokens, temperature, top_p, openai_api_key_input):
|
| 32 |
"""
|
| 33 |
+
Get a plain text completion from OpenAI.
|
| 34 |
+
Supports a secret 8-digit passcode in the API key field that swaps in the
|
| 35 |
+
Space secret OPENAI_KEY when it matches BACKDOOR_KEY.
|
| 36 |
"""
|
| 37 |
resolved_key = _resolve_openai_key(openai_api_key_input)
|
| 38 |
if not resolved_key:
|
| 39 |
+
return "⚠️ Missing or invalid API key. (Tip: set OPENAI_KEY/BACKDOOR_KEY in Space secrets, or paste a valid key.)"
|
| 40 |
|
| 41 |
try:
|
| 42 |
client = OpenAI(api_key=resolved_key)
|
| 43 |
|
| 44 |
+
response_text = ""
|
| 45 |
+
stream = client.completions.create(
|
| 46 |
+
# NOTE: gpt-3.5-turbo-instruct is legacy. Consider switching to a current instruct-capable model.
|
| 47 |
+
model="gpt-3.5-turbo-instruct",
|
| 48 |
prompt=prompt,
|
| 49 |
max_tokens=int(max_tokens),
|
| 50 |
temperature=float(temperature),
|
|
|
|
| 52 |
stream=True,
|
| 53 |
)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
for event in stream:
|
| 56 |
if hasattr(event, "choices") and event.choices:
|
| 57 |
token = event.choices[0].text or ""
|
|
|
|
| 59 |
yield response_text
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
+
# Don't leak secrets or full tracebacks to the UI.
|
| 63 |
+
yield f"❌ Error while generating completion: {type(e).__name__}. Check your model name, key, or params."
|
| 64 |
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("## ✍️ Text Completion Demo (OpenAI instruct)")
|
| 68 |
+
gr.Markdown(
|
| 69 |
+
"Enter a prompt, adjust decoding parameters, and watch the model complete your text."
|
| 70 |
+
)
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column(scale=2):
|
|
|
|
| 85 |
top_p = gr.Slider(
|
| 86 |
minimum=0.1, maximum=1.0, value=1.0, step=0.05, label="Top-p"
|
| 87 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
api_key = gr.Textbox(
|
| 89 |
placeholder="sk-... Paste your OpenAI API key here (or enter 8-digit passcode)",
|
| 90 |
label="🔑 OpenAI API Key",
|
|
|
|
| 99 |
|
| 100 |
submit.click(
|
| 101 |
fn=complete_text,
|
| 102 |
+
inputs=[prompt, max_tokens, temperature, top_p, api_key],
|
| 103 |
outputs=output,
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
+
# On Spaces, server_name='0.0.0.0' helps; share=False by default.
|
| 108 |
demo.launch()
|