Team Ai
Apppublic

jadend/Intro_to_AI_Prompt_Engineering

sourceHugging Facemitupdated 2y agoView on Hugging Face
1likes
app.py109 linesDownload Raw Back to root
1# import the relevant packages2import os3import csv4import openai5from openai import OpenAI6import gradio as gr7import huggingface_hub8from huggingface_hub import Repository9from datetime import datetime10 11client = OpenAI(12    api_key=os.environ.get("API_TOKEN"),13)14 15# recording requests to Hugging Face dataset16#DATASET_REPO_URL = "https://huggingface.co/datasets/jadend/mr_bot_requests"17#DATA_FILENAME = "prompt_input.csv"18#DATA_FILE = os.path.join("data", DATA_FILENAME)19 20#HF_TOKEN = os.environ.get("HF_TOKEN")21 22#repo = Repository(23#    local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN24#)25 26# function to store the message to the Hugging Face site, if you would like to add more columns or information to track add it here27def store_message(prompt: str, temperature: str, response: str):28    if prompt:29        with open(DATA_FILE, "a") as csvfile:30            writer = csv.DictWriter(csvfile, fieldnames=["prompt", "temperature", "response", "time"])31            writer.writerow(32                {"prompt": prompt, 33                 "temperature": temperature,34                 "response": response,35                 "time": str(datetime.now())}36            )37        commit_url = repo.push_to_hub()38        print(commit_url)39 40    return 41 42# defines who can enter the application with the secrets that are set up if used43user_db = { 44           os.environ["username"]: os.environ["password"],45          }46 47# the OpenAI function to use the given prompt to the model defined.48def mr_bot(prompt, tokens, temperature):49    response = client.chat.completions.create(50        model="gpt-3.5-turbo",51        messages=[{"role": "user", "content": prompt}],52        max_tokens=tokens,53        temperature=temperature,54        )55    # store_message(prompt=prompt, temperature=temperature, response=response.choices[0].message.content)56    57    return response.choices[0].message.content58 59 60# Gradio App interface and login set up61description = "Use the examples below to see how different prompts generate different results. Use the examples to see how different prompting " \62              "techniques can enhance your prompts. They are split into three sections, (creative writing, problem solving in business, and " \63              "philosophical debate) with three different levels of prompt engineering, (basic, intermediate, and advanced)."64 65demo = gr.Interface(fn=mr_bot, 66                    inputs=[gr.Textbox(placeholder="Select an example", interactive=False), gr.Slider(1, 800, value=800), gr.Slider(0.1, 1, value=0.75)], 67                    outputs="text",68                    title="Intro to AI, Prompt Engineering",69                    description=description,70                    cache_examples=False,71                    examples = [72                                            # Creative Writing73                        # Basic Prompt:74                        ["Write a story about a city.", 800, .90],75                        # Intermediate Prompt:76                        ["Write a story set in a futuristic city where technology controls everything.", 800, .90],77                        # Advanced Prompt:78                        ["Write a gripping tale set in a futuristic city named Neo-Philly, where an underground movement " \79                         "is fighting against the oppressive control of AI over human life, focusing on a protagonist who was once a loyal AI prompt engineer.", 800, .90],80 81                                            # Problem Solving in Business82                        # Basic Prompt:83                        ["A company needs to improve its profits.", 800, .90],84                        # Intermediate Prompt:85                        ["A retail company is seeing a decline in profits due to online competition. What can they do?", 800, .90],86                        # Advanced Prompt:87                        ["Devise a comprehensive strategy for a brick-and-mortar retail company that has been losing market share to e-commerce giants. " \88                         "Focus on innovative in-store experiences, digital integration, and customer loyalty programs to regain competitiveness.", 800, .90],89 90                                            # Philosophical Debate91                        # Basic Prompt:92                        ["Discuss the concept of happiness.", 800, .90],93                        # Intermediate Prompt:94                        ["Debate whether true happiness can be achieved through material wealth or if it's found in intangible experiences.", 800, .90],95                        # Advanced Prompt:96                        ["Critically analyze the philosophical arguments for and against the notion that true happiness is derived more from personal " \97                         "fulfillment and self-actualization than from material possessions, considering perspectives from both Eastern and " \98                         "Western philosophies.", 800, .90],99                    ]100                    )101 102if __name__ == "__main__":103    demo.launch(104        # enable_queue=False,105        ## if authentication is needed, uncomment below106        # auth=lambda u, p: user_db.get(u) == p,107        # auth_message="Welcome!"108               )109