Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +73 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import the relevant packages
|
| 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 |
+
# set up for the Hugging Face dataset, based on the directory you set up
|
| 11 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/jadend/mr_bot_requests"
|
| 12 |
+
DATA_FILENAME = "premise_input.csv"
|
| 13 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 14 |
+
|
| 15 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 16 |
+
|
| 17 |
+
repo = Repository(
|
| 18 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 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(premise: str, temperature: str, response: str):
|
| 23 |
+
if premise:
|
| 24 |
+
with open(DATA_FILE, "a") as csvfile:
|
| 25 |
+
writer = csv.DictWriter(csvfile, fieldnames=["premise", "temperature", "response", "time"])
|
| 26 |
+
writer.writerow(
|
| 27 |
+
{"premise": premise,
|
| 28 |
+
"temperature": temperature,
|
| 29 |
+
"response": response,
|
| 30 |
+
"time": str(datetime.now())}
|
| 31 |
+
)
|
| 32 |
+
commit_url = repo.push_to_hub()
|
| 33 |
+
print(commit_url)
|
| 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(premise, tokens, temperature):
|
| 44 |
+
openai.api_key = os.environ["API_TOKEN"]
|
| 45 |
+
response = openai.Completion.create(
|
| 46 |
+
model="text-davinci-003",
|
| 47 |
+
prompt=premise,
|
| 48 |
+
max_tokens=tokens,
|
| 49 |
+
temperature=temperature,
|
| 50 |
+
)
|
| 51 |
+
store_message(premise=premise, temperature=temperature, response=response['choices'][0]["text"])
|
| 52 |
+
|
| 53 |
+
return response['choices'][0]["text"]
|
| 54 |
+
|
| 55 |
+
# Gradio App interface and login set up
|
| 56 |
+
description = "Ask Mr. Bot a question or give him a command. Entered Prompts are logged and stored securely."
|
| 57 |
+
|
| 58 |
+
demo = gr.Interface(fn=mr_bot,
|
| 59 |
+
inputs=["text", gr.Slider(1, 800, default=800), gr.Slider(0.1, 1, default=0.75)],
|
| 60 |
+
outputs="text",
|
| 61 |
+
title="Mr. Bot",
|
| 62 |
+
description=description,
|
| 63 |
+
cache_examples=False,
|
| 64 |
+
examples = [["Write me a rap song about how the price of rent is too high.", 800, .75],
|
| 65 |
+
["Make a weekly meal plan for me comprised of an entirely vegan diet.", 800, .50]]
|
| 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 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
https://gradio-builds.s3.amazonaws.com/75ed61524f55e542414d165ed60a976091c24f77/gradio-3.16.2-py3-none-any.whl
|
| 2 |
+
openai==0.26.1
|
| 3 |
+
huggingface_hub==0.4.0
|