Mike014/PromptEngineeringLab
1
1import gradio as gr2from huggingface_hub import InferenceClient3import csv4import os5 6# Initialize the client with the open-source AI model7client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")8 9# Files for storing tested prompts and feedback10PROMPT_HISTORY_FILE = "prompt_history.csv"11FEEDBACK_FILE = "feedback.csv"12 13# Function to save prompts and responses in a CSV file14def log_prompt(prompt, response):15 file_exists = os.path.isfile(PROMPT_HISTORY_FILE)16 with open(PROMPT_HISTORY_FILE, mode="a", newline="", encoding="utf-8") as file:17 writer = csv.writer(file)18 if not file_exists:19 writer.writerow(["Prompt", "Response"])20 writer.writerow([prompt, response])21 22# Function to test prompts and store responses23def respond(24 message,25 history: list[tuple[str, str]],26 system_message,27 max_tokens,28 temperature,29 top_p,30):31 messages = [{"role": "system", "content": system_message}]32 33 for val in history:34 if val[0]:35 messages.append({"role": "user", "content": val[0]})36 if val[1]:37 messages.append({"role": "assistant", "content": val[1]})38 39 messages.append({"role": "user", "content": message})40 response = ""41 42 for message in client.chat_completion(43 messages,44 max_tokens=max_tokens,45 stream=True,46 temperature=temperature,47 top_p=top_p,48 ):49 token = message.choices[0].delta.content50 response += token51 yield response52 53 # Save the prompt and response54 log_prompt(message, response)55 56# Function to generate advanced prompts with different prompting techniques57def generate_prompt(prompt, technique):58 if technique == "Zero-shot":59 return prompt60 elif technique == "Few-shot":61 return f"Example 1: Input: 'Good morning', Output: 'Hello!'\nExample 2: Input: 'How are you?', Output: 'I'm fine, thank you!'\nNow generate a response for: '{prompt}'"62 elif technique == "Chain-of-Thought":63 return f"Step 1: Analyze the question '{prompt}'\nStep 2: Break down each element separately\nStep 3: Provide a detailed response."64 else:65 return prompt66 67# Function to collect feedback on AI responses68def rate_response(prompt, response, rating):69 with open(FEEDBACK_FILE, mode="a", newline="", encoding="utf-8") as file:70 writer = csv.writer(file)71 writer.writerow([prompt, response, rating])72 return "Thank you for your feedback! Your rating has been recorded."73 74# Main UI for testing Prompt Engineering75chat_interface = gr.ChatInterface(76 respond,77 additional_inputs=[78 gr.Textbox(value="You are a friendly chatbot.", label="System message"),79 gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),80 gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),81 gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),82 ],83 title="Prompt Engineering Lab",84 description="Test your prompts with an open-source AI model and analyze the results.",85)86 87# UI for selecting advanced prompting techniques88prompt_interface = gr.Interface(89 fn=generate_prompt,90 inputs=[gr.Textbox(label="Prompt"), gr.Radio(["Zero-shot", "Few-shot", "Chain-of-Thought"], label="Technique")],91 outputs="text",92 title="Prompt Engineering Techniques",93 description="Select an advanced technique to generate more effective prompts.",94)95 96# UI for collecting feedback on AI responses97rating_interface = gr.Interface(98 fn=rate_response,99 inputs=[100 gr.Textbox(label="Prompt"),101 gr.Textbox(label="AI Output"),102 gr.Slider(1, 5, step=1, label="Rating (1-5)"),103 ],104 outputs="text",105 title="Prompt Quality Evaluation",106 description="Rate the quality of the AI response to improve the prompts.",107)108 109# Creating a tabbed interface with all components110demo = gr.TabbedInterface(111 [chat_interface, prompt_interface, rating_interface],112 ["AI Chat", "Prompt Engineering", "Output Evaluation"]113)114 115# Launch the UI116if __name__ == "__main__":117 demo.launch()118 119 