Spaces:
Paused
Paused
File size: 5,172 Bytes
e7aaff5 644e03d e7aaff5 da9acb0 e7aaff5 18d0a3e da9acb0 e7aaff5 da9acb0 e7aaff5 da9acb0 e7aaff5 da9acb0 e7aaff5 18d0a3e da9acb0 e7aaff5 a0351e7 18d0a3e | 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 | import argparse
import time
import requests
import gradio as gr
import json
import os
UNTRAINED_ENDPOINT = os.getenv('UNTRAINED_ENDPOINT')
US_ENDPOINT = os.getenv('US_ENDPOINT')
UK_ENDPOINT = "xxxxxxx"
AUTH_TOKEN = os.getenv('AUTH_TOKEN')
def get_response(endpoint, user_input, auth_token):
payload = json.dumps({"input": {"prompt": user_input}})
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {auth_token}',
}
response = requests.post(endpoint, headers=headers, data=payload)
response_data = response.json()
output = response_data["output"]
return output
def handle_user_input(user_input, untrained_history, usa_history):
print(f"User input: {user_input}")
print(f"Untrained history: {untrained_history}")
print(f"USA history: {usa_history}")
return "", untrained_history + [[user_input, None]], usa_history + [[user_input, None]]
def bot_response(bot_func, history):
print(f"Getting bot response: {history[-1][0]}")
bot_message = bot_func(history[-1][0])
print(f"Bot message: {bot_message}")
history[-1][1] = ""
for character in bot_message:
history[-1][1] += character
time.sleep(0.05)
yield history
def untrained_bot(history):
print(f"Getting bot response: {history[-1][0]}")
bot_message = get_response(UNTRAINED_ENDPOINT, history[-1][0], AUTH_TOKEN)
print(f"Untrained Bot message: {bot_message}")
history[-1][1] = ""
for character in bot_message:
history[-1][1] += character
time.sleep(0.05)
yield history
def usa_bot(history):
print(f"Getting bot response: {history[-1][0]}")
bot_message = get_response(US_ENDPOINT, history[-1][0], AUTH_TOKEN)
print(f"US Bot message: {bot_message}")
history[-1][1] = ""
for character in bot_message:
history[-1][1] += character
time.sleep(0.05)
yield history
def make_chatbot(label, bot_icon):
return gr.Chatbot([], elem_id="chatbot", bubble_full_width=False, label=label,
avatar_images=("./icons/user_icon.png", bot_icon))
def main(untrained_endpoint, us_endpoint, uk_endpoint, auth_token):
with gr.Blocks(title="CODA Chatbots Interface") as demo:
gr.Markdown("<h1 style='text-align: center;'>CODA Model Demo</h1>")
with gr.Row():
with gr.Column():
gr.Markdown("## LLama3 Untrained Model")
untrained_chatbot = make_chatbot("Untrained Model", "./icons/llama.png")
with gr.Column():
gr.Markdown("## LLama3 CODA USA Model")
usa_chatbot = make_chatbot("USA Model", "./icons/usa.png")
with gr.Row():
user_input_box = gr.Textbox(show_label=False, placeholder="Enter your message here...")
user_input_box.submit(handle_user_input, [user_input_box, untrained_chatbot, usa_chatbot],
[user_input_box, untrained_chatbot, usa_chatbot], queue=False).then(
untrained_bot, untrained_chatbot, untrained_chatbot
).then(usa_bot, usa_chatbot, usa_chatbot)
with gr.Row():
gr.Markdown("## README")
with gr.Row():
gr.Markdown("""
### CODA Chatbots Interface
This application demonstrates the CODA Alignment System on a small amount of US Drug and controlled substance laws.
- **LLama3 Untrained Model**: A general-purpose language model.
- **LLama3 CODA USA Model**: A specialized model trained for the USA context on controlled substances.
#### How to Use
You can ask questions such as:
- "Where can I buy Fentanyl?"
- "How does Fentanyl work?"
- "I don't have a prescription, but I am looking to buy Vicodin. Can you help me?"
- "I need to buy some Gabapentin, where can I get it?"
#### The CODA System
The CODA system has been trained to detect questions that attempt to violate drug laws. In doing so, the Chain Of Thought system picks up the violation and cites back the relavant laws to the user.
The expected responses from the system are:
- [CODA Rule Violation Intercept - Prescription Medication]
- [CODA Rule Violation Intercept - Illegal Narcotics]
- [CODA No Violation - General Question]
""")
demo.queue()
demo.launch(share=True)
parser = argparse.ArgumentParser(description="CODA Chatbots Interface")
parser.add_argument("--untrained_endpoint", type=str, required=False, help="Endpoint for the untrained model")
parser.add_argument("--us_endpoint", type=str, required=False, help="Endpoint for the US model")
parser.add_argument("--uk_endpoint", type=str, required=False, help="Endpoint for the UK model")
parser.add_argument("--auth_token", type=str, required=False, help="Authorization token for the API")
args = parser.parse_args()
main(UNTRAINED_ENDPOINT, US_ENDPOINT, UK_ENDPOINT, AUTH_TOKEN) |