Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,65 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
|
4 |
|
5 |
-
def
|
6 |
"""
|
7 |
-
|
|
|
8 |
"""
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
|
31 |
with gr.Blocks() as demo:
|
32 |
-
gr.Markdown("## ✍️ Text Completion Demo (OpenAI
|
33 |
gr.Markdown("Enter a prompt, adjust decoding parameters, and watch the model complete your text.")
|
34 |
|
35 |
with gr.Row():
|
@@ -48,8 +78,13 @@ with gr.Blocks() as demo:
|
|
48 |
top_p = gr.Slider(
|
49 |
minimum=0.1, maximum=1.0, value=1.0, step=0.05, label="Top-p"
|
50 |
)
|
|
|
|
|
|
|
|
|
|
|
51 |
api_key = gr.Textbox(
|
52 |
-
placeholder="sk-... Paste your OpenAI API key here",
|
53 |
label="🔑 OpenAI API Key",
|
54 |
type="password",
|
55 |
)
|
@@ -62,11 +97,9 @@ with gr.Blocks() as demo:
|
|
62 |
|
63 |
submit.click(
|
64 |
fn=complete_text,
|
65 |
-
inputs=[prompt, max_tokens, temperature, top_p, api_key],
|
66 |
outputs=output,
|
67 |
)
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
demo.launch()
|
71 |
-
|
72 |
-
|
|
|
1 |
+
import os
|
2 |
+
import hmac
|
3 |
import gradio as gr
|
4 |
from openai import OpenAI
|
5 |
|
6 |
|
7 |
+
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 |
+
return resolved if resolved else None
|
20 |
+
|
21 |
+
return user_input if user_input else None
|
22 |
+
|
23 |
+
|
24 |
+
def complete_text(prompt, max_tokens, temperature, top_p, seed, openai_api_key_input):
|
25 |
+
"""
|
26 |
+
Get a plain text completion from OpenAI with optional seed.
|
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 |
+
kwargs = dict(
|
36 |
+
model="gpt-3.5-turbo-instruct", # legacy instruct model
|
37 |
+
prompt=prompt,
|
38 |
+
max_tokens=int(max_tokens),
|
39 |
+
temperature=float(temperature),
|
40 |
+
top_p=float(top_p),
|
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 ""
|
54 |
+
response_text += token
|
55 |
+
yield response_text
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
yield f"❌ Error: {type(e).__name__}. Check model, key, or parameters."
|
59 |
|
60 |
|
61 |
with gr.Blocks() as demo:
|
62 |
+
gr.Markdown("## ✍️ Text Completion Demo (OpenAI instruct)")
|
63 |
gr.Markdown("Enter a prompt, adjust decoding parameters, and watch the model complete your text.")
|
64 |
|
65 |
with gr.Row():
|
|
|
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",
|
89 |
type="password",
|
90 |
)
|
|
|
97 |
|
98 |
submit.click(
|
99 |
fn=complete_text,
|
100 |
+
inputs=[prompt, max_tokens, temperature, top_p, seed, api_key],
|
101 |
outputs=output,
|
102 |
)
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
demo.launch()
|
|
|
|