Spaces:
Sleeping
Sleeping
Update app.py
Browse files28 unmodified lines293031323334352 unmodified lines383940414243444546474849505152535455565758596013 unmodified lines747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914028 unmodified linesenc = _get_encoding(model)return len(enc.encode(text))# Default banned words (static style list)DEFAULT_BANNED_WORDS = ["Hurdles", "Tapestry", "Bustling", "Harnessing", "Unveiling the power","Realm", "Depicted", "Demistify", "Insurmountable", "New Era",2 unmodified lines"Tailored", "Elegant", "Dive", "Ever-evolving", "Adventure","Journey", "Navigate", "Navigation"]# Expressions/tones to catch and removeDEFAULT_FILTER_PATTERNS = [r"As an AI language model", r"I(?:’|'|i)m sorry", r"I apologize",r"In conclusion", r"At the end of the day"]# Original style prefixORIGINAL_PREFIX = ("You are a clear-writing assistant. Follow these rules for every response:\n""1. Write in plain, everyday language.\n""2. Avoid fluff, jargon, and tautological expressions.\n""3. Use short, direct sentences.\n""4. Do not use complicated English words—choose simple alternatives.\n""5. Do not use any of the default banned words.\n""Whenever you’d normally reach for a banned word, pick a simple synonym or rephrase.\n")# Additional prompting pointsADDITIONAL_POINTS = {"Tone & Voice": [13 unmodified lines]}# Build the Gradio interfacewith gr.Blocks() as demo:gr.Markdown("# Custom Prompt Builder for LLMs")model_dropdown = gr.Dropdown(ALL_MODELS, value=ALL_MODELS[0], label="Select Model")user_query = gr.Textbox(label="User Query", placeholder="Type your question here...", lines=3)with gr.Accordion("Original Style Instructions", open=False):gr.Textbox(value=ORIGINAL_PREFIX, interactive=False, lines=8)# Checkboxes for additional pointspoint_widgets = []for category, pts in ADDITIONAL_POINTS.items():widget = gr.CheckboxGroup(pts, label=category)point_widgets.append(widget)# Input for extra banned expressions (regex patterns)custom_bans = gr.Textbox(label="Custom banned expressions (comma-separated regex)", lines=2,placeholder="e.g. 'As an AI language model','I apologize'")# Generate and display the AI responsedef build_and_respond(query, model, *args):# Last arg is custom bans, rest are selected points*selected_lists, custom_raw = argsselections = [item for sublist in selected_lists for item in sublist]# Construct full prefixstyle_block = ORIGINAL_PREFIX + "\n" + "\n".join(selections)full_prompt = style_block + "\n\nNow, answer the user’s question.\n\n" + query# Call the OpenAI APIresp = client.chat.completions.create(model=model,messages=[{"role": "user", "content": full_prompt}])reply = resp.choices[0].message.content# Build complete banned listcustom_list = [b.strip() for b in custom_raw.split(",") if b.strip()]banned_words = DEFAULT_BANNED_WORDSbanned_patterns = DEFAULT_FILTER_PATTERNS + custom_list# Remove banned words and patternsfor word in banned_words:reply = re.sub(rf"\b{re.escape(word)}\b", "", reply, flags=re.IGNORECASE)for pat in banned_patterns:reply = re.sub(pat, "", reply, flags=re.IGNORECASE)# Clean extra whitespacereply = re.sub(r"\s{2,}", " ", reply).strip()# Token usagetokens_used = count_tokens(full_prompt, model) + count_tokens(reply, model)return reply + f"\n\n(Tokens used: {tokens_used})"inputs = [user_query, model_dropdown] + point_widgets + [custom_bans]output = gr.Chatbot(label="AI Response")user_query.submit(build_and_respond, inputs=inputs, outputs=output)user_query.submit(lambda: "", None, user_query)demo.launch()28 unmodified lines293031323334352 unmodified lines383940414243444546474813 unmodified lines626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212328 unmodified linesenc = _get_encoding(model)return len(enc.encode(text))# Default banned words and patternsDEFAULT_BANNED_WORDS = ["Hurdles", "Tapestry", "Bustling", "Harnessing", "Unveiling the power","Realm", "Depicted", "Demistify", "Insurmountable", "New Era",2 unmodified lines"Tailored", "Elegant", "Dive", "Ever-evolving", "Adventure","Journey", "Navigate", "Navigation"]DEFAULT_FILTER_PATTERNS = [r"As an AI language model", r"I(?:’|'|i)m sorry", r"I apologize",r"In conclusion", r"At the end of the day"]# Additional prompting pointsADDITIONAL_POINTS = {"Tone & Voice": [13 unmodified lines]}with gr.Blocks() as demo:gr.Markdown("# Custom Prompt Builder for LLMs")model_dropdown = gr.Dropdown(ALL_MODELS, value=ALL_MODELS[0], label="Select Model")with gr.TabbedInterface():with gr.Tab("Style Settings"):prefix_box = gr.Textbox(label="Style Prefix",value=("You are a clear-writing assistant. Follow these rules for every response:\n""1. Write in plain, everyday language.\n""2. Avoid fluff, jargon, and tautological expressions.\n""3. Use short, direct sentences.\n""4. Do not use complicated English words—choose simple alternatives.\n""5. List or ban any unwanted expressions below."), lines=6)banned_words = gr.Textbox(label="Banned Words (comma-separated)",value=",".join(DEFAULT_BANNED_WORDS), lines=2)banned_patterns = gr.Textbox(label="Banned Patterns (regex, comma-separated)",value=",".join(DEFAULT_FILTER_PATTERNS), lines=2)with gr.Tab("Prompt Points"):sel_points = []for cat, pts in ADDITIONAL_POINTS.items():panel = gr.CheckboxGroup(pts, label=cat)sel_points.append(panel)with gr.Tab("Compose"):user_query = gr.Textbox(label="User Query", placeholder="Type your question here...", lines=4)send_btn = gr.Button("Send Prompt")output = gr.Chatbot(label="AI Response")def build_and_respond(model, prefix, banned_w, banned_p, *points_and_query):*selected_lists, query = points_and_queryselections = [item for sublist in selected_lists for item in sublist]bw = [w.strip() for w in banned_w.split(",") if w.strip()]bp = [p.strip() for p in banned_p.split(",") if p.strip()]style_text = prefix + "\n" + "\n".join(selections)full_prompt = style_text + "\n\n" + queryresp = client.chat.completions.create(model=model,messages=[{"role": "user", "content": full_prompt}])reply = resp.choices[0].message.contentfor w in bw:reply = re.sub(rf"\b{re.escape(w)}\b", "", reply, flags=re.IGNORECASE)for p in bp:reply = re.sub(p, "", reply, flags=re.IGNORECASE)reply = re.sub(r"\s{2,}", " ", reply).strip()tokens = count_tokens(full_prompt, model) + count_tokens(reply, model)return reply + f"\n\nTokens used: {tokens}"inputs = [model_dropdown, prefix_box, banned_words, banned_patterns] + sel_points + [user_query]send_btn.click(build_and_respond, inputs=inputs, outputs=output)demo.launch()
app.py+45-62