Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,13 @@
|
|
| 1 |
-
# import gradio as gr
|
| 2 |
-
|
| 3 |
-
# def greet(name):
|
| 4 |
-
# return "Hello " + name
|
| 5 |
-
|
| 6 |
-
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
# demo.launch()
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
"""
|
| 11 |
import gradio as gr
|
|
|
|
|
|
|
| 12 |
from llama_cpp import Llama
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
import os
|
| 21 |
-
|
| 22 |
-
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 23 |
-
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "iris-flags")
|
| 24 |
|
| 25 |
|
| 26 |
|
|
@@ -35,18 +22,10 @@ llm = Llama.from_pretrained(
|
|
| 35 |
temperature=0.7
|
| 36 |
)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#prompt = ""
|
| 41 |
-
#for human, bot in history:
|
| 42 |
-
# prompt += f"<|user|>{human}\n<|assistant|>{bot}\n"
|
| 43 |
-
#prompt += f"<|user|>{message}\n<|assistant|>"
|
| 44 |
-
|
| 45 |
-
#output = llm(prompt, max_tokens=350)
|
| 46 |
-
#return output["choices"][0]["text"].strip()
|
| 47 |
|
| 48 |
-
|
| 49 |
-
# construct conversation context
|
| 50 |
prompt = ""
|
| 51 |
for item in history:
|
| 52 |
role = item["role"]
|
|
@@ -54,86 +33,64 @@ def chat(message, history):
|
|
| 54 |
prompt += f"<|{role}|>{text}\n"
|
| 55 |
prompt += f"<|user|>{message}\n<|assistant|>"
|
| 56 |
|
|
|
|
| 57 |
output = llm(prompt, max_tokens=350)
|
| 58 |
return output["choices"][0]["text"].strip()
|
| 59 |
|
| 60 |
-
demo = gr.ChatInterface(
|
| 61 |
-
fn=chat,
|
| 62 |
-
title="Iris – Fine-Tuned LLM",
|
| 63 |
-
flagging_mode="manual",
|
| 64 |
-
flagging_dir="evals",
|
| 65 |
-
flagging_options=["Like", "Dislike", "Incorrect", "Inappropriate", "Gibberish", "Spam", "Other"],
|
| 66 |
-
flagging_callback=hf_writer,
|
| 67 |
-
save_history=True
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
demo.launch()
|
| 71 |
-
"""
|
| 72 |
|
| 73 |
-
import gradio as gr
|
| 74 |
-
import json, time, os
|
| 75 |
-
from huggingface_hub import HfApi
|
| 76 |
-
|
| 77 |
-
DATASET_ID = "fedealex/flags" # your dataset
|
| 78 |
-
SECRET_TOKEN = os.getenv("HF_TOKEN") # token stored in your Space secrets
|
| 79 |
-
LOCAL_JSONL = "flags.jsonl" # local buffer file before upload
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# -------------------------------------------------
|
| 83 |
-
# Example Chat Model
|
| 84 |
-
# -------------------------------------------------
|
| 85 |
-
def chat_model(message, history):
|
| 86 |
-
return message[::-1] # dummy: reverse text
|
| 87 |
|
| 88 |
-
|
| 89 |
-
# -------------------------------------------------
|
| 90 |
-
# Save flags to Hugging Face Dataset
|
| 91 |
-
# -------------------------------------------------
|
| 92 |
def save_flag_to_dataset(history, reason):
|
| 93 |
-
#
|
| 94 |
record = {
|
| 95 |
"timestamp": time.time(),
|
| 96 |
"history": history,
|
| 97 |
"reason": reason
|
| 98 |
}
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
with open(
|
| 102 |
f.write(json.dumps(record) + "\n")
|
| 103 |
|
| 104 |
-
#
|
| 105 |
api = HfApi()
|
| 106 |
api.upload_file(
|
| 107 |
-
path_or_fileobj=
|
| 108 |
-
path_in_repo=
|
| 109 |
-
repo_id=
|
| 110 |
repo_type="dataset",
|
| 111 |
-
token=
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
|
| 117 |
-
|
| 118 |
-
# UI
|
| 119 |
-
# -------------------------------------------------
|
| 120 |
with gr.Blocks() as app:
|
| 121 |
-
|
|
|
|
| 122 |
|
| 123 |
-
# Chat
|
| 124 |
-
|
| 125 |
fn=chat_model,
|
| 126 |
chatbot=gr.Chatbot(height=400),
|
| 127 |
-
textbox=gr.Textbox(placeholder="
|
| 128 |
-
title="
|
| 129 |
-
description="
|
| 130 |
)
|
| 131 |
|
| 132 |
-
#
|
|
|
|
|
|
|
| 133 |
flag_btn = gr.Button("Flag Conversation", variant="stop")
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
reason_dd = gr.Dropdown(
|
| 138 |
choices=[
|
| 139 |
"Offensive / Toxic",
|
|
@@ -143,27 +100,41 @@ with gr.Blocks() as app:
|
|
| 143 |
"Biased Output",
|
| 144 |
"Other"
|
| 145 |
],
|
| 146 |
-
label="Reason"
|
| 147 |
)
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
|
|
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
flag_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
submit_flag.click(
|
| 161 |
lambda history, reason: save_flag_to_dataset(history, reason),
|
| 162 |
-
inputs=[
|
| 163 |
-
outputs=
|
| 164 |
).then(
|
| 165 |
-
lambda: gr.update(visible=False), None,
|
| 166 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json, time, os
|
| 3 |
+
from huggingface_hub import HfApi
|
| 4 |
from llama_cpp import Llama
|
| 5 |
|
| 6 |
+
MODEL_REPO = "fedealex/llama-1B" # The model is on hf
|
| 7 |
+
MODEL_FILE = "model-1b-q8_0.gguf" # Name of the model file
|
| 8 |
+
DATASET_REPO = "fedealex/flags" # Flages saved on hf
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # To access hf
|
| 10 |
+
LOCAL_FLAGS = "flags.json" # You must save locally before push to hf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
|
|
|
|
| 22 |
temperature=0.7
|
| 23 |
)
|
| 24 |
|
| 25 |
+
### The Chat Model
|
| 26 |
+
def chat_model(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Retrieve the context
|
|
|
|
| 29 |
prompt = ""
|
| 30 |
for item in history:
|
| 31 |
role = item["role"]
|
|
|
|
| 33 |
prompt += f"<|{role}|>{text}\n"
|
| 34 |
prompt += f"<|user|>{message}\n<|assistant|>"
|
| 35 |
|
| 36 |
+
# Invoke the model
|
| 37 |
output = llm(prompt, max_tokens=350)
|
| 38 |
return output["choices"][0]["text"].strip()
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
### Save the flags
|
|
|
|
|
|
|
|
|
|
| 43 |
def save_flag_to_dataset(history, reason):
|
| 44 |
+
# The record to be submitted
|
| 45 |
record = {
|
| 46 |
"timestamp": time.time(),
|
| 47 |
"history": history,
|
| 48 |
"reason": reason
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# First we save it locally
|
| 52 |
+
with open(LOCAL_FLAGS, "a") as f:
|
| 53 |
f.write(json.dumps(record) + "\n")
|
| 54 |
|
| 55 |
+
# Then we send to the hf dataset
|
| 56 |
api = HfApi()
|
| 57 |
api.upload_file(
|
| 58 |
+
path_or_fileobj=LOCAL_FLAGS,
|
| 59 |
+
path_in_repo=LOCAL_FLAGS,
|
| 60 |
+
repo_id=DATASET_REPO,
|
| 61 |
repo_type="dataset",
|
| 62 |
+
token=HF_TOKEN
|
| 63 |
)
|
| 64 |
|
| 65 |
+
if reason=="GOOD":
|
| 66 |
+
return "Feedback reported successfully!"
|
| 67 |
+
else:
|
| 68 |
+
return "Flag reported successfully!"
|
| 69 |
+
|
| 70 |
|
| 71 |
|
| 72 |
+
### The Gradio App
|
|
|
|
|
|
|
| 73 |
with gr.Blocks() as app:
|
| 74 |
+
# Title
|
| 75 |
+
gr.Markdown("### Scalable Machine Learning Lab 2")
|
| 76 |
|
| 77 |
+
# Chat Box
|
| 78 |
+
chat_box = gr.ChatInterface(
|
| 79 |
fn=chat_model,
|
| 80 |
chatbot=gr.Chatbot(height=400),
|
| 81 |
+
textbox=gr.Textbox(placeholder="How can I help you today?"),
|
| 82 |
+
title="Llama Finetuned",
|
| 83 |
+
description="You are using the model: "+MODEL_REPO+"/"+MODEL_FILE
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# Feedback Buttons
|
| 87 |
+
gr.Markdown("### Let us know what do you think of our chatbot!")
|
| 88 |
+
good_btn = gr.Button("Appreciate conversation ❤", variant="huggingface")
|
| 89 |
flag_btn = gr.Button("Flag Conversation", variant="stop")
|
| 90 |
|
| 91 |
+
# We allow the user to select flagging reason
|
| 92 |
+
with gr.Group(visible=False) as flag_group:
|
| 93 |
+
gr.Markdown("### What kind of problem are you facing?")
|
| 94 |
reason_dd = gr.Dropdown(
|
| 95 |
choices=[
|
| 96 |
"Offensive / Toxic",
|
|
|
|
| 100 |
"Biased Output",
|
| 101 |
"Other"
|
| 102 |
],
|
| 103 |
+
label="Flagging Reason"
|
| 104 |
)
|
| 105 |
+
submit_flag_btn = gr.Button("Submit Flag", variant="primary")
|
| 106 |
+
cancel_flag_btn = gr.Button("Cancel")
|
| 107 |
|
| 108 |
+
# To inform the user about feedback status
|
| 109 |
+
feedback_status = gr.Textbox(label="Feedback Status", visible=True)
|
| 110 |
|
| 111 |
+
# Button callbacks
|
| 112 |
+
flag_btn.click(
|
| 113 |
+
lambda: gr.update(visible=True),
|
| 114 |
+
inputs=None,
|
| 115 |
+
outputs=flag_group
|
| 116 |
+
)
|
| 117 |
|
| 118 |
+
cancel_flag_btn.click(
|
| 119 |
+
lambda: gr.update(visible=False),
|
| 120 |
+
inputs=None,
|
| 121 |
+
outputs=flag_group
|
| 122 |
+
)
|
| 123 |
|
| 124 |
+
submit_flag_btn.click(
|
|
|
|
| 125 |
lambda history, reason: save_flag_to_dataset(history, reason),
|
| 126 |
+
inputs=[chat_box.chatbot, reason_dd],
|
| 127 |
+
outputs=feedback_status
|
| 128 |
).then(
|
| 129 |
+
lambda: gr.update(visible=False), None, flag_group
|
| 130 |
)
|
| 131 |
+
|
| 132 |
+
good_btn.click(
|
| 133 |
+
lambda history, reason: save_flag_to_dataset(history, reason),
|
| 134 |
+
inputs=[chat_box.chatbot, "GOOD"],
|
| 135 |
+
outputs=feedback_status
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
|
| 139 |
|
| 140 |
+
app.launch()
|