| import os |
| import hmac |
| import gradio as gr |
| from openai import OpenAI |
|
|
|
|
| def _resolve_openai_key(user_input: str) -> str | None: |
| """ |
| If `user_input` is an 8-digit code matching BACKDOOR_KEY (Space secret), |
| return the OPENAI_KEY (Space secret). Otherwise return `user_input`. |
| """ |
| user_input = (user_input or "").strip() |
|
|
| backdoor_code = (os.getenv("BACKDOOR_KEY") or "").strip() |
| is_candidate_code = user_input.isdigit() and len(user_input) == 8 and backdoor_code |
|
|
| if is_candidate_code and hmac.compare_digest(user_input, backdoor_code): |
| resolved = os.getenv("OPENAI_KEY") |
| return resolved if resolved else None |
|
|
| return user_input if user_input else None |
|
|
|
|
| def complete_text(prompt, max_tokens, temperature, top_p, seed, openai_api_key_input): |
| """ |
| Get a plain text completion from OpenAI with optional seed. |
| """ |
| resolved_key = _resolve_openai_key(openai_api_key_input) |
| if not resolved_key: |
| return "⚠️ Missing or invalid API key." |
|
|
| try: |
| client = OpenAI(api_key=resolved_key) |
|
|
| kwargs = dict( |
| model="gpt-3.5-turbo-instruct", |
| prompt=prompt, |
| max_tokens=int(max_tokens), |
| temperature=float(temperature), |
| top_p=float(top_p), |
| stream=True, |
| ) |
|
|
| |
| if seed and str(seed).isdigit(): |
| kwargs["seed"] = int(seed) |
|
|
| response_text = "" |
| stream = client.completions.create(**kwargs) |
|
|
| for event in stream: |
| if hasattr(event, "choices") and event.choices: |
| token = event.choices[0].text or "" |
| response_text += token |
| yield response_text |
|
|
| except Exception as e: |
| yield f"❌ Error: {type(e).__name__}. Check model, key, or parameters." |
|
|
|
|
| with gr.Blocks() as demo: |
| gr.Markdown("## ✍️ Text Completion Demo (OpenAI instruct)") |
| gr.Markdown("Enter a prompt, adjust decoding parameters, and watch the model complete your text.") |
|
|
| with gr.Row(): |
| with gr.Column(scale=2): |
| prompt = gr.Textbox( |
| label="Prompt", |
| placeholder="Type the beginning of your text...", |
| lines=4, |
| ) |
| max_tokens = gr.Slider( |
| minimum=1, maximum=1024, value=100, step=1, label="Max tokens" |
| ) |
| temperature = gr.Slider( |
| minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature" |
| ) |
| top_p = gr.Slider( |
| minimum=0.1, maximum=1.0, value=1.0, step=0.05, label="Top-p" |
| ) |
| seed = gr.Textbox( |
| placeholder="Optional integer seed for reproducibility", |
| label="🎲 Seed", |
| type="text", |
| ) |
| api_key = gr.Textbox( |
| placeholder="sk-... Paste your OpenAI API key here (or enter 8-digit passcode)", |
| label="🔑 OpenAI API Key", |
| type="password", |
| ) |
| submit = gr.Button("Generate Completion") |
| with gr.Column(scale=3): |
| output = gr.Textbox( |
| label="Generated Completion", |
| lines=15, |
| ) |
|
|
| submit.click( |
| fn=complete_text, |
| inputs=[prompt, max_tokens, temperature, top_p, seed, api_key], |
| outputs=output, |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|