# PromptCraft: Gradio App for Prompt Engineering on Hugging Face import gradio as gr import openai # or transformers, depending on which model you prefer # Your OpenAI API key setup (use environment variables in production) openai.api_key = "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" # === PROMPT ENGINEERING CORE LOGIC === def generate_4T_prompt(trait, task, tone, style, target): return ( f"You are {trait}. Your task is to {task}. " f"Use a {tone} tone and write in a {style} style. " f"The intended audience is {target}." ) # === ADVANCED TECHNIQUE DESCRIPTIONS === TECHNIQUES = { "Zero-shot": "Direct instruction with no examples. Great for classification, summarization.", "Few-shot": "Add examples to steer model behavior. Works well for formatting tasks or when zero-shot is too vague.", "Chain-of-Thought": "Encourage reasoning by adding 'Let's think step by step.' Useful for logic/math.", "ReAct": "Model reasons + takes actions (e.g., using tools). Ideal for interactive, tool-integrated tasks.", "Prompt Chaining": "Use LLM output as input for the next prompt. Great for step-by-step workflows.", "Self-Consistency": "Use multiple reasoning paths and pick most consistent. Improves CoT reliability." # Add more from your notes as needed } # === EXAMPLE USE CASE BUTTONS === EXAMPLES = { "AI Tutor with CoT": "You are a friendly math tutor. Help solve this logic problem step-by-step... Let's think step by step.", "Medical Diagnosis (RAG)": "You are an expert diagnostic assistant. Use external medical documents to help answer...", "Agent Tool Use (ReAct)": "You are an assistant who thinks and acts. First think through the problem, then take action (e.g. search, calculate)...", "Creative Storytelling (Few-shot)": "You are a story generator. Given 3 examples of creative stories, continue the next one..." } def get_technique_blurb(technique): return TECHNIQUES.get(technique, "Coming soon.") def load_example_prompt(example): return EXAMPLES.get(example, "") # === LLM CALL === def run_prompt_with_openai(prompt): try: response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], max_tokens=500 ) return response.choices[0].message.content except Exception as e: return f"Error: {e}" # === UI === def build_app(): with gr.Blocks(css=get_custom_css()) as app: gr.Markdown("# โœจ PromptCraft: Prompt Engineering Tool") with gr.Tab("๐Ÿ”ง Build Prompt (4Ts)"): trait = gr.Textbox(label="Trait (Role + Experience)") task = gr.Textbox(label="Task (What should the model do?)") tone = gr.Dropdown(["Academic", "Casual", "Confident", "Formal", "Sarcastic"], label="Tone") style = gr.Dropdown(["Analytical", "Conversational", "Creative", "Instructive"], label="Writing Style") target = gr.Textbox(label="Target Audience") generate_btn = gr.Button("Generate Prompt", elem_classes=["submit-btn"]) generated_prompt = gr.Textbox(label="Generated Prompt", lines=5) generate_btn.click(generate_4T_prompt, [trait, task, tone, style, target], generated_prompt) with gr.Tab("๐Ÿง  Advanced Prompting Techniques"): technique = gr.Dropdown(list(TECHNIQUES.keys()), label="Select a Technique") blurb = gr.Textbox(label="Technique Description", lines=5, interactive=False) technique.change(get_technique_blurb, technique, blurb) with gr.Tab("๐Ÿ“Œ Example Prompts by Use Case"): gr.Markdown("### Click a scenario to load a high-impact example prompt:") with gr.Row(): buttons = [gr.Button(k, elem_classes=["example-scenario-btn"]) for k in EXAMPLES] example_output = gr.Textbox(label="Example Prompt") for btn in buttons: btn.click(fn=load_example_prompt, inputs=[], outputs=example_output, queue=False, show_progress=False, preprocess=False) with gr.Tab("๐Ÿงช Test Prompt Output"): prompt_input = gr.Textbox(label="Paste your prompt here", lines=5) run_btn = gr.Button("Run Prompt with OpenAI") prompt_response = gr.Textbox(label="Model Response", lines=15) run_btn.click(run_prompt_with_openai, prompt_input, prompt_response) return app # === THEME CSS === def get_custom_css(): with open("theme.css", "r") as f: return f.read() if __name__ == "__main__": build_app().launch()