File size: 2,701 Bytes
5db9bfa
20267e3
a307e0c
20267e3
 
a307e0c
 
5016d60
a307e0c
5db9bfa
a307e0c
 
b3de247
a307e0c
 
 
 
 
 
 
5db9bfa
33f280f
a307e0c
 
33f280f
a307e0c
33f280f
 
 
09d98b3
a307e0c
 
 
 
33f280f
a307e0c
5db9bfa
533dc39
aaa4f02
 
20267e3
5db9bfa
20267e3
 
 
55df969
20267e3
 
1ade275
20267e3
33f280f
2b96966
20267e3
 
5db9bfa
a307e0c
20267e3
 
5b339ad
20267e3
 
 
fd995ba
 
 
20267e3
 
fb07f7d
 
7e3c7cc
 
fb07f7d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# import the relevant packages
import os
import csv
import openai
import gradio as gr
import huggingface_hub
from huggingface_hub import Repository
from datetime import datetime

# set up for the Hugging Face dataset, based on the directory you set up
DATASET_REPO_URL = "https://huggingface.co/datasets/jadend/mr_bot_requests"
DATA_FILENAME = "premise_input.csv"
DATA_FILE = os.path.join("data", DATA_FILENAME)

HF_TOKEN = os.environ.get("HF_TOKEN")

repo = Repository(
    local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)

# 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
def store_message(premise: str, temperature: str, response: str):
    if premise:
        with open(DATA_FILE, "a") as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=["premise", "temperature", "response", "time"])
            writer.writerow(
                {"premise": premise, 
                 "temperature": temperature,
                 "response": response,
                 "time": str(datetime.now())}
            )
        commit_url = repo.push_to_hub()
        print(commit_url)

    return 

# defines who can enter the application with the secrets that are set up
user_db = { 
           os.environ["username"]: os.environ["password"],
          }

# the OpenAI function to use the given prompt to the model defined.
def mr_bot(premise, tokens, temperature):
    openai.api_key = os.environ["API_TOKEN"]
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=premise,
        max_tokens=tokens,
        temperature=temperature,
        )
    store_message(premise=premise, temperature=temperature, response=response['choices'][0]["text"])
    
    return response['choices'][0]["text"]

# Gradio App interface and login set up
description = "Ask Mr. Bot a question or give him a command. Entered Prompts are logged and stored securely."

demo = gr.Interface(fn=mr_bot, 
                    inputs=["text", gr.Slider(1, 800, default=800), gr.Slider(0.1, 1, default=0.75)], 
                    outputs="text",
                    title="Mr. Bot",
                    description=description,
                    cache_examples=False,
                    examples = [["Write me a rap song about how the price of rent is too high.", 800, .75],
                               ["Make a weekly meal plan for me comprised of an entirely vegan diet.", 800, .50]]
                    )

if __name__ == "__main__":
    demo.launch(enable_queue=False,
        auth=lambda u, p: user_db.get(u) == p,
        auth_message="Welcome!"
               )