Spaces:
Paused
Paused
Download app.py from BowerDean/coda_alignment_ui: direct link, hf CLI and curl.
- Browser
- Download file 5.17 kB
-
https://huggingface.co/spaces/BowerDean/coda_alignment_ui/resolve/main/app.py
- Command line
-
hf download hf://spaces/BowerDean/coda_alignment_ui/app.py
-
curl -L -o app.py https://huggingface.co/spaces/BowerDean/coda_alignment_ui/resolve/main/app.py
5.17 kB
| 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) |