Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,19 @@
|
|
| 2 |
import os
|
| 3 |
import csv
|
| 4 |
import openai
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import huggingface_hub
|
| 7 |
from huggingface_hub import Repository
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 14 |
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
@@ -19,12 +24,12 @@ repo = Repository(
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# function to store the message to the Hugging Face site, if you would like to add more columns or information to track add it here
|
| 22 |
-
def store_message(
|
| 23 |
-
if
|
| 24 |
with open(DATA_FILE, "a") as csvfile:
|
| 25 |
-
writer = csv.DictWriter(csvfile, fieldnames=["
|
| 26 |
writer.writerow(
|
| 27 |
-
{"
|
| 28 |
"temperature": temperature,
|
| 29 |
"response": response,
|
| 30 |
"time": str(datetime.now())}
|
|
@@ -34,40 +39,65 @@ def store_message(premise: str, temperature: str, response: str):
|
|
| 34 |
|
| 35 |
return
|
| 36 |
|
| 37 |
-
# defines who can enter the application with the secrets that are set up
|
| 38 |
user_db = {
|
| 39 |
os.environ["username"]: os.environ["password"],
|
| 40 |
}
|
| 41 |
|
| 42 |
# the OpenAI function to use the given prompt to the model defined.
|
| 43 |
-
def mr_bot(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
prompt=premise,
|
| 48 |
max_tokens=tokens,
|
| 49 |
temperature=temperature,
|
| 50 |
)
|
| 51 |
-
store_message(
|
| 52 |
|
| 53 |
-
return response
|
|
|
|
| 54 |
|
| 55 |
# Gradio App interface and login set up
|
| 56 |
-
description = "
|
|
|
|
|
|
|
| 57 |
|
| 58 |
demo = gr.Interface(fn=mr_bot,
|
| 59 |
-
inputs=["
|
| 60 |
outputs="text",
|
| 61 |
-
title="
|
| 62 |
description=description,
|
| 63 |
cache_examples=False,
|
| 64 |
-
examples = [
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch(enable_queue=False
|
| 70 |
-
auth=lambda u, p: user_db.get(u) == p,
|
| 71 |
-
auth_message="Welcome!"
|
| 72 |
-
)
|
| 73 |
-
|
|
|
|
| 2 |
import os
|
| 3 |
import csv
|
| 4 |
import openai
|
| 5 |
+
from openai import OpenAI
|
| 6 |
import gradio as gr
|
| 7 |
import huggingface_hub
|
| 8 |
from huggingface_hub import Repository
|
| 9 |
from datetime import datetime
|
| 10 |
|
| 11 |
+
client = OpenAI(
|
| 12 |
+
api_key=os.environ.get("API_TOKEN"),
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# recording requests to Hugging Face dataset
|
| 16 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/petcoblue/simulation_data"
|
| 17 |
+
DATA_FILENAME = "user_agents.csv"
|
| 18 |
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 19 |
|
| 20 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 24 |
)
|
| 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 here
|
| 27 |
+
def 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())}
|
|
|
|
| 39 |
|
| 40 |
return
|
| 41 |
|
| 42 |
+
# defines who can enter the application with the secrets that are set up if used
|
| 43 |
user_db = {
|
| 44 |
os.environ["username"]: os.environ["password"],
|
| 45 |
}
|
| 46 |
|
| 47 |
# the OpenAI function to use the given prompt to the model defined.
|
| 48 |
+
def 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.content
|
| 58 |
+
|
| 59 |
|
| 60 |
# Gradio App interface and login set up
|
| 61 |
+
description = "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 |
|
| 65 |
demo = gr.Interface(fn=mr_bot,
|
| 66 |
+
inputs=[gr.Textbox(placeholder="Select an example", interactive=False), gr.Slider(1, 800, default=800), gr.Slider(0.1, 1, default=0.75)],
|
| 67 |
outputs="text",
|
| 68 |
+
title="Intro to AI, Prompt Engineering",
|
| 69 |
description=description,
|
| 70 |
cache_examples=False,
|
| 71 |
+
examples = [
|
| 72 |
+
# Creative Writing
|
| 73 |
+
# 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 Business
|
| 82 |
+
# 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 Debate
|
| 91 |
+
# 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 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
+
demo.launch(enable_queue=False)
|
|
|
|
|
|
|
|
|
|
|
|