From 13199e1155ed26d49c2388d16b0fcf1a48513da9 Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Wed, 8 Jan 2025 11:38:49 +0100 Subject: [PATCH 1/5] add requirements.txt for Spaces deployment --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1324e63 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +git+https://github.com/huggingface/feel.git@main From 6ea04d8d043dd9fe4d9f5c582b22145781bd9556 Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Wed, 8 Jan 2025 11:54:45 +0100 Subject: [PATCH 2/5] add language information to system prompt --- app/app.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/app.py b/app/app.py index 1cc7ef4..aeec084 100644 --- a/app/app.py +++ b/app/app.py @@ -13,6 +13,8 @@ from huggingface_hub import InferenceClient from pandas import DataFrame +LANGUAGES = ["English", "Spanish", "Hebrew", "Dutch"] + client = InferenceClient( token=os.getenv("HF_TOKEN"), model=( @@ -32,6 +34,20 @@ def add_user_message(history, message): return history, gr.MultimodalTextbox(value=None, interactive=False) +def format_system_message(language: str, history: list): + if history: + if history[0]["role"] == "system": + history = history[1:] + system_message = [ + { + "role": "system", + "content": f"You are a helpful assistant that speaks {language}.", + } + ] + history = system_message + history + return history + + def format_history_as_messages(history: list): messages = [] current_role = None @@ -267,10 +283,23 @@ def submit_conversation(dataframe, session_id): visible=False, ) + language = gr.Dropdown( + label="Language", + choices=LANGUAGES, + # value="English", + interactive=True, + ) + chatbot = gr.Chatbot( elem_id="chatbot", editable="all", bubble_full_width=False, + value=[ + { + "role": "system", + "content": "You are a helpful assistant that speaks English.", + } + ], type="messages", feedback_options=["Like", "Dislike"], ) @@ -293,6 +322,12 @@ def submit_conversation(dataframe, session_id): # Deal with feedback ############################## + language.change( + fn=format_system_message, + inputs=[language, chatbot], + outputs=[chatbot], + ) + chat_input.submit( fn=add_user_message, inputs=[chatbot, chat_input], From af275df61fb21e34105fc804b9e5150667096042 Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Wed, 8 Jan 2025 12:41:40 +0100 Subject: [PATCH 3/5] add language dropdown --- app/app.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/app.py b/app/app.py index aeec084..58501f1 100644 --- a/app/app.py +++ b/app/app.py @@ -13,7 +13,7 @@ from huggingface_hub import InferenceClient from pandas import DataFrame -LANGUAGES = ["English", "Spanish", "Hebrew", "Dutch"] +LANGUAGES: list[str] = ["English", "Spanish", "Hebrew", "Dutch"] client = InferenceClient( token=os.getenv("HF_TOKEN"), @@ -34,6 +34,17 @@ def add_user_message(history, message): return history, gr.MultimodalTextbox(value=None, interactive=False) +def get_system_message(language: str) -> str: + if language == "English": + return "You are a helpful assistant that speaks English." + elif language == "Spanish": + return "Tu eres un asistente útil que habla español." + elif language == "Hebrew": + return "אתה עוזר טוב שמפגש בעברית." + elif language == "Dutch": + return "Je bent een handige assistent die Nederlands spreekt." + + def format_system_message(language: str, history: list): if history: if history[0]["role"] == "system": @@ -41,7 +52,7 @@ def format_system_message(language: str, history: list): system_message = [ { "role": "system", - "content": f"You are a helpful assistant that speaks {language}.", + "content": get_system_message(language), } ] history = system_message + history @@ -286,8 +297,9 @@ def submit_conversation(dataframe, session_id): language = gr.Dropdown( label="Language", choices=LANGUAGES, - # value="English", + value=LANGUAGES[0], interactive=True, + max_choices=1, ) chatbot = gr.Chatbot( @@ -297,7 +309,7 @@ def submit_conversation(dataframe, session_id): value=[ { "role": "system", - "content": "You are a helpful assistant that speaks English.", + "content": get_system_message(language.value), } ], type="messages", From 7ccbe379134124154a03a812c1c514937e6c8bed Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Wed, 8 Jan 2025 12:51:05 +0100 Subject: [PATCH 4/5] add language to dataset --- app/app.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/app/app.py b/app/app.py index 58501f1..387c7a1 100644 --- a/app/app.py +++ b/app/app.py @@ -113,16 +113,18 @@ def _process_content(content) -> str | list[str]: return content -def add_fake_like_data(history: list, session_id: str, liked: bool = False) -> None: +def add_fake_like_data( + history: list, session_id: str, language: str, liked: bool = False +) -> None: data = { "index": len(history) - 1, "value": history[-1], - "liked": True, + "liked": liked, } _, dataframe = wrangle_like_data( gr.LikeData(target=None, data=data), history.copy() ) - submit_conversation(dataframe, session_id) + submit_conversation(dataframe, session_id, language) def respond_system_message( @@ -205,7 +207,7 @@ def wrangle_like_data(x: gr.LikeData, history) -> DataFrame: def wrangle_edit_data( - x: gr.EditData, history: list, dataframe: DataFrame, session_id: str + x: gr.EditData, history: list, dataframe: DataFrame, session_id: str, language: str ) -> list: """Edit the conversation and add negative feedback if assistant message is edited, otherwise regenerate the message @@ -243,12 +245,12 @@ def wrangle_edit_data( def wrangle_retry_data( - x: gr.RetryData, history: list, dataframe: DataFrame, session_id: str + x: gr.RetryData, history: list, dataframe: DataFrame, session_id: str, language: str ) -> list: """Respond to the user message with a system message and add negative feedback on the original message Return the history with the new message""" - add_fake_like_data(history, session_id) + add_fake_like_data(history, session_id, language) # Return the history without a new message history = respond_system_message( @@ -259,10 +261,12 @@ def wrangle_retry_data( return history, update_dataframe(dataframe, history) -def submit_conversation(dataframe, session_id): +def submit_conversation(dataframe, session_id, language): """ "Submit the conversation to dataset repo""" - if dataframe.empty: - gr.Info("No messages to submit because the conversation was empty") + if "role" in dataframe.columns: + dataframe = dataframe[dataframe["role"] != "system"] + if dataframe.empty or len(dataframe) < 2: + gr.Info("No feedback to submit.") return (gr.Dataframe(value=None, interactive=False), []) dataframe["content"] = dataframe["content"].apply(_process_content) @@ -271,9 +275,10 @@ def submit_conversation(dataframe, session_id): "timestamp": datetime.now().isoformat(), "session_id": session_id, "conversation_id": str(uuid.uuid4()), + "language": language, } save_feedback(input_object=conversation_data) - gr.Info(f"Submitted {len(dataframe)} messages to the dataset") + gr.Info("Submitted your feedback!") return (gr.Dataframe(value=None, interactive=False), []) @@ -295,11 +300,7 @@ def submit_conversation(dataframe, session_id): ) language = gr.Dropdown( - label="Language", - choices=LANGUAGES, - value=LANGUAGES[0], - interactive=True, - max_choices=1, + label="Language", choices=LANGUAGES, value=LANGUAGES[0], interactive=True ) chatbot = gr.Chatbot( @@ -357,19 +358,19 @@ def submit_conversation(dataframe, session_id): chatbot.retry( fn=wrangle_retry_data, - inputs=[chatbot, dataframe, session_id], + inputs=[chatbot, dataframe, session_id, language], outputs=[chatbot, dataframe], ) chatbot.edit( fn=wrangle_edit_data, - inputs=[chatbot, dataframe, session_id], + inputs=[chatbot, dataframe, session_id, language], outputs=[chatbot], ).then(update_dataframe, inputs=[dataframe, chatbot], outputs=[dataframe]) submit_btn.click( fn=submit_conversation, - inputs=[dataframe, session_id], + inputs=[dataframe, session_id, language], outputs=[dataframe, chatbot], ) demo.load( From 16f921afb29dab2c57afef092705c47ad76a41dd Mon Sep 17 00:00:00 2001 From: davidberenstein1957 Date: Wed, 8 Jan 2025 13:00:25 +0100 Subject: [PATCH 5/5] update css to show dropdown options --- app/app.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/app/app.py b/app/app.py index 387c7a1..291e3e3 100644 --- a/app/app.py +++ b/app/app.py @@ -223,8 +223,10 @@ def wrangle_edit_data( if history[index]["role"] == "user": # Add feedback on original and corrected message - add_fake_like_data(history[: index + 2], session_id, liked=True) - add_fake_like_data(history[: index + 1] + [original_message], session_id) + add_fake_like_data(history[: index + 2], session_id, language, liked=True) + add_fake_like_data( + history[: index + 1] + [original_message], session_id, language + ) history = respond_system_message( history[: index + 1], temperature=random.randint(1, 100) / 100, @@ -233,8 +235,8 @@ def wrangle_edit_data( return history else: # Add feedback on original and corrected message - add_fake_like_data(history[: index + 1], session_id, liked=True) - add_fake_like_data(history[:index] + [original_message], session_id) + add_fake_like_data(history[: index + 1], session_id, language, liked=True) + add_fake_like_data(history[:index] + [original_message], session_id, language) history = history[: index + 1] # add chosen and rejected options history[-1]["options"] = [ @@ -263,15 +265,15 @@ def wrangle_retry_data( def submit_conversation(dataframe, session_id, language): """ "Submit the conversation to dataset repo""" - if "role" in dataframe.columns: - dataframe = dataframe[dataframe["role"] != "system"] if dataframe.empty or len(dataframe) < 2: gr.Info("No feedback to submit.") return (gr.Dataframe(value=None, interactive=False), []) dataframe["content"] = dataframe["content"].apply(_process_content) + conversation = dataframe.to_dict(orient="records") + conversation = conversation[1:] # remove system message conversation_data = { - "conversation": dataframe.to_dict(orient="records"), + "conversation": conversation, "timestamp": datetime.now().isoformat(), "session_id": session_id, "conversation_id": str(uuid.uuid4()), @@ -283,12 +285,14 @@ def submit_conversation(dataframe, session_id, language): css = """ -.options { +.options.svelte-pcaovb { + display: none !important; +} +.option.svelte-pcaovb { display: none !important; } """ - with gr.Blocks(css=css) as demo: ############################## # Chatbot @@ -299,9 +303,7 @@ def submit_conversation(dataframe, session_id, language): visible=False, ) - language = gr.Dropdown( - label="Language", choices=LANGUAGES, value=LANGUAGES[0], interactive=True - ) + language = gr.Dropdown(choices=LANGUAGES, label="Language", interactive=True) chatbot = gr.Chatbot( elem_id="chatbot",