Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import oxen
|
| 4 |
+
from oxen import RemoteRepo
|
| 5 |
+
from oxen.auth import config_auth
|
| 6 |
+
from oxen.user import config_user, current_user
|
| 7 |
+
import json
|
| 8 |
+
import argparse
|
| 9 |
+
|
| 10 |
+
# These will be initialized in the main function
|
| 11 |
+
repo = None
|
| 12 |
+
def download_prompts(branch):
|
| 13 |
+
'''
|
| 14 |
+
Downloads prompts.jsonl from the main remote repository
|
| 15 |
+
'''
|
| 16 |
+
# Download takes a file or directory a commit id
|
| 17 |
+
# write stuff for when prompts.jsonl doesnt exist
|
| 18 |
+
repo.download("prompts.jsonl", revision=branch)
|
| 19 |
+
return "Successfully downloaded prompts.jsonl from the main remote repository."
|
| 20 |
+
|
| 21 |
+
# Function to update the bad_prompt variable
|
| 22 |
+
def add_prompt(api_key, prompt):
|
| 23 |
+
'''
|
| 24 |
+
saves prompt to prompts.json
|
| 25 |
+
'''
|
| 26 |
+
|
| 27 |
+
# write lock file
|
| 28 |
+
lock_file = "LOCK"
|
| 29 |
+
if os.path.exists(lock_file):
|
| 30 |
+
return "Someone else is currently adding prompts. Please try again later."
|
| 31 |
+
else:
|
| 32 |
+
with open(lock_file, 'w') as f:
|
| 33 |
+
f.write("locked")
|
| 34 |
+
|
| 35 |
+
if api_key == "":
|
| 36 |
+
return "Please enter an API key"
|
| 37 |
+
|
| 38 |
+
# this will not work in high concurrent environments, but for now it's fine
|
| 39 |
+
try:
|
| 40 |
+
config_auth(api_key)
|
| 41 |
+
except:
|
| 42 |
+
return "Invalid API Key"
|
| 43 |
+
|
| 44 |
+
user = current_user()
|
| 45 |
+
|
| 46 |
+
# convert name into a branch name with dashes
|
| 47 |
+
username = user.name.replace(" ", "-")
|
| 48 |
+
branch_name = f"add-prompt-{username}"
|
| 49 |
+
|
| 50 |
+
# create branch if doesn't exist, switch to it if it does
|
| 51 |
+
try:
|
| 52 |
+
repo.create_checkout_branch(branch_name)
|
| 53 |
+
except:
|
| 54 |
+
return "You do not have permission to create branches. Please contact @greg.schoeninger in the Oxen.ai discord for access. https://discord.com/invite/s3tBEn7Ptg"
|
| 55 |
+
|
| 56 |
+
# Download the last set of prompts
|
| 57 |
+
download_prompts(branch_name)
|
| 58 |
+
|
| 59 |
+
file_path = "prompts.jsonl"
|
| 60 |
+
data = set()
|
| 61 |
+
with open(file_path) as f:
|
| 62 |
+
for line in f:
|
| 63 |
+
print(line)
|
| 64 |
+
data.add(line.strip())
|
| 65 |
+
|
| 66 |
+
data.add(json.dumps({"prompt": prompt, "user": user.name}))
|
| 67 |
+
with open(file_path, 'w') as writer:
|
| 68 |
+
for line in data:
|
| 69 |
+
writer.write(line)
|
| 70 |
+
writer.write('\n')
|
| 71 |
+
|
| 72 |
+
repo.add('prompts.jsonl')
|
| 73 |
+
repo.commit(f"Adding '{prompt}' to dataset")
|
| 74 |
+
|
| 75 |
+
# remove lock file
|
| 76 |
+
os.remove(lock_file)
|
| 77 |
+
|
| 78 |
+
return "Prompt added to prompts.jsonl"
|
| 79 |
+
|
| 80 |
+
def is_configured():
|
| 81 |
+
return oxen.is_configured() and 'OXEN_USER_NAME' in os.environ and 'OXEN_USER_EMAIL' in os.environ and 'OXEN_AUTH_TOKEN' in os.environ
|
| 82 |
+
|
| 83 |
+
def print_error_not_configured():
|
| 84 |
+
print("Please configure your Oxen credentials:\n\n\toxen config --auth API_KEY\n\toxen config --name YOUR_NAME --email YOUR_EMAIL\n\nor set the OXEN_AUTH_TOKEN, OXEN_USER_NAME and OXEN_USER_EMAIL environment variables.")
|
| 85 |
+
|
| 86 |
+
def main():
|
| 87 |
+
|
| 88 |
+
global repo
|
| 89 |
+
repo_name = "datasets/UnanswerableQuestions"
|
| 90 |
+
repo = RemoteRepo(repo_name)
|
| 91 |
+
|
| 92 |
+
if not repo.exists():
|
| 93 |
+
print(f"Repository {repo_name} does not exist")
|
| 94 |
+
|
| 95 |
+
print(f"Saving prompts to {repo_name}")
|
| 96 |
+
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
gr.Markdown("# Unanswerable Questions")
|
| 99 |
+
gr.Markdown("This is a dataset of unanswerable questions to test whether an LLM \"knows when it does not know\" and minimize hallucinations. Click Save to Oxen to save the prompts to Oxen.ai")
|
| 100 |
+
gr.Markdown("To view the dataset, visit https://oxen.ai/datasets/UnanswerableQuestions")
|
| 101 |
+
|
| 102 |
+
gr.Markdown("## Save prompt to Oxen")
|
| 103 |
+
success_message = gr.Markdown()
|
| 104 |
+
api_key = gr.Textbox(label="Enter Oxen.ai API Key here", lines=1, type="password")
|
| 105 |
+
prompt = gr.Textbox(label="Enter prompt here", lines=2)
|
| 106 |
+
button1 = gr.Button(value="Save Prompt")
|
| 107 |
+
button1.click(add_prompt, inputs = [api_key, prompt], outputs=success_message)
|
| 108 |
+
|
| 109 |
+
demo.launch()
|
| 110 |
+
demo.close()
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
main()
|