Spaces:
Sleeping
Sleeping
File size: 4,622 Bytes
be29f82 8c42ede be29f82 b6a2305 8c42ede be29f82 b6a2305 be29f82 b6a2305 be29f82 b6a2305 2a08ed3 b6a2305 be29f82 b6a2305 b508eaf 2a08ed3 b508eaf b6a2305 be29f82 f3d6309 be29f82 b508eaf |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
import openai
import gradio as gr
import itertools
import os
import requests
from urllib.parse import urlsplit, parse_qs
import random
openai.api_key = os.getenv("OPENAI_API_KEY") # Replace this with your API key: https://beta.openai.com/docs/quickstart/add-your-api-key
def openai_chat(prompt, history):
start_sequence = "\nAI:"
restart_sequence = "\nHuman: "
prompt_engineering = """The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly. The AI assistant goal is to help the user find a mission statement that best aligns their beliefs in the DAO. The DAO is called CityDAO wants to build the citys of the future.\n
AI: I am an AI created by OpenAI. How can I help you define your mission statement?
"""
history = [ (human_prompt.replace("<p>", "Human:").replace("</p>", "\n"), ai_prompt.replace("<p>", "AI:").replace("</p>", "\n" )) for human_prompt, ai_prompt in history]
flat_history = list(itertools.chain(*history))
prompt_history = prompt_engineering + ''.join(flat_history)
prompt = prompt_history + ("Human: " + prompt + "\n")
print("prompt:", repr(prompt))
completions = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=[" Human:", " AI:"]
)
message = completions.choices[0].text
return message.strip()
# Create a Request Function that will submit id, identity, and message to the API
def createZKP_request(id, identity,group, attestation):
# Create a dictionary of the parameters
data = {
"id": id,
"identity": identity,
"group": attestation,
"attestation": "Missionbot", # Atttesation must be a string of 32 bytes or less
}
# Make a POST request to the AP
response = requests.post("https://eczl8gqxk5.execute-api.eu-central-1.amazonaws.com/default/Semaphore-proof-function", json=data)
return response.ok
def chatbot(input, history=[]):
output = openai_chat(input, history)
history.append((input, output))
return history, history
def echo(input, history = []):
history.append((input, "Hello World"))
return history, history
# Gradio Interface using gradio blocks
# Retrive Gradio URL parameter from the URL
with gr.Blocks() as demo:
history = gr.State([])
with gr.Column(visible=True) as inital_column:
# gr.Markdown(
# """
# # Fellow Community Member!
# I am missionBot! I am here to know your mission. Please start in the chat so I can get a sense of your mission,
# and when you are done just submit your mission!
# """)
outputDialogue = gr.Chatbot( )
inputText = gr.Textbox()
greet_btn = gr.Button(value="Submit my Mission", visible=False)
with gr.Column(visible=False) as final_column:
gr.Markdown(
"""
Thank you for your mission! Your mission has been sent to the DAO for review.
To start a new mission please refresh the page!
"""
)
markdownID = gr.Markdown()
def btn_click(input, history):
# print("Request headers dictionary:", request.headers)
# referrer = request.headers.get("Referer")
# query = urlsplit(referrer).query
# params = parse_qs(query)
# print("Params:", params)
# # if params id exists enter if statement
# if "id" in params:
# response = createZKP_request(int(params["id"]), "None", "None", str(history))
# # Update the state and rerender the interface
# return {
# inital_column: gr.update(visible=False),
# final_column: gr.update(visible=True),
# markdownID: gr.update(value=f"Your mission number is: {params['id']}")
# }
# else:
random_id = random.randrange(1000,8100)
response = createZKP_request(int(random_id), "None", "None", str(history))
# Update the state and rerender the interface
return {
inital_column: gr.update(visible=False),
final_column: gr.update(visible=True),
markdownID: gr.update(value=f"Your mission number is: {random_id}")
}
inputText.submit(fn=chatbot, inputs=[inputText, history], outputs=[outputDialogue, history])
greet_btn.click(fn=btn_click, inputs=[inputText,history], outputs=[inital_column, final_column, markdownID, outputDialogue])
outputDialogue.change(fn=lambda value: gr.update(visible=True) , inputs=outputDialogue, outputs=greet_btn)
demo.launch()
|