From 901edce251ab3c1ff61631414e67046d6f0a8545 Mon Sep 17 00:00:00 2001 From: Lucas Mikael Roni Suomela <lucasmikaelroni.suomela@uzh.ch> Date: Mon, 20 May 2024 16:43:35 +0200 Subject: [PATCH] Completed the assignment --- assignment7/.gitignore | 3 +- assignment7/README.md | 3 ++ assignment7/chatbot.py | 35 +++++++++++++++++-- assignment7/translator.py | 71 +++++++++++++++++++++++++++++++++++++++ assignment7/utils.py | 28 +++++++++++++++ 5 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 assignment7/translator.py diff --git a/assignment7/.gitignore b/assignment7/.gitignore index d75edea..5d408a0 100644 --- a/assignment7/.gitignore +++ b/assignment7/.gitignore @@ -1,2 +1,3 @@ venv -__pycache__ \ No newline at end of file +__pycache__ +screenshots \ No newline at end of file diff --git a/assignment7/README.md b/assignment7/README.md index 3957b3c..cf3d01b 100644 --- a/assignment7/README.md +++ b/assignment7/README.md @@ -28,3 +28,6 @@ full_prompt = "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\nHowd This was the output: "Well, I reckon we're just chattin' and seein' where the conversation takes us! I'm happy to just shoot the breeze and see what kind of fun we can have. We could talk about your favorite hobbies, or share some funny stories, or even play a game or two. What sounds good to you?" +## Note for Translation Interface +I tried many different variations for the base prompt, but could not get Llama 3 to output Japanese, Chinese, and Korean in their respective scripts. Interestingly enough, Russian was outputted in Cyrillic script. +In regards to the target languages, I just created a list of a few languages that came to mind, as it was not specified if we should let the user input a language (for example, in a text box) or if we should just provide a dropdown list of languages. I chose the latter, as it seemed more in line with the assignment's difficulty level. \ No newline at end of file diff --git a/assignment7/chatbot.py b/assignment7/chatbot.py index 67a63d8..463866f 100644 --- a/assignment7/chatbot.py +++ b/assignment7/chatbot.py @@ -1,7 +1,36 @@ +import requests +import json import gradio as gr +from utils import format_as_chat, generate_payload -def echo(message, history): - return message +# Endpoint_url +post_url = 'https://uf9t072wj5ki2ho4.eu-west-1.aws.endpoints.huggingface.cloud/generate' +# Headers +headers = { + 'Content-Type': 'application/json' +} -demo = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="Echo Bot") + +def echo(message: str, history: list): + # Concatenate history and message + formatted_prompt = format_as_chat(message, history) + # Generate the payload dictionary + payload = generate_payload(formatted_prompt) + + # Post to API + response = requests.post( + post_url, + data=json.dumps(payload), + headers=headers + ) + + # Format the reply + reply = response.json()['generated_text'] + reply = reply.replace('assistant', '').strip() + + return reply + + +demo = gr.ChatInterface(fn=echo, examples=["wassup?", "grüezi", "ã‚‚ã—ã‚‚ã—"], + title="Llama 3 8B Instruct") demo.launch() diff --git a/assignment7/translator.py b/assignment7/translator.py new file mode 100644 index 0000000..2b71c1a --- /dev/null +++ b/assignment7/translator.py @@ -0,0 +1,71 @@ +import requests +import json +import time +import gradio as gr +from utils import generate_payload, format_for_translator + +# Endpoint_url +post_url = 'https://uf9t072wj5ki2ho4.eu-west-1.aws.endpoints.huggingface.cloud/generate' +# Headers +headers = { + 'Content-Type': 'application/json; charset=utf-8' +} + + +def translate(sentence: str, trg_lang: str): + + # Because I leave the other parameters as default, I arbitrarily set an max input length + if len(sentence) > 100: + return f"Sentence too long! Up to 100 characters are allowed, but {len(sentence)} were sent." + + # Wrap sentence with translation instruction and format it + formatted_prompt = format_for_translator(sentence, trg_lang) + # Generate payload + payload = generate_payload(formatted_prompt) + + # Send post request to API + response = requests.post( + post_url, + data=json.dumps(payload), + headers=headers + ) + + # Format the reply + reply = response.json()['generated_text'] + reply = reply.replace('assistant', '').strip() + + return reply + + +with gr.Blocks() as translator: + gr.Markdown( + """ + # Simple Translator + Translate any sentence from English to a target language of your choice! + *Runs on the Llama 3 8B Instruct language model* + """) + with gr.Row(): + gr.Interface( + fn=translate, + inputs=[ + gr.Textbox( + placeholder="Write an English sentence to translate.", + label="Input Sentence" + ), + gr.Dropdown( + choices=[ + "German", "French", "Italian", "Spanish", "Portuguese", "Russian", + "Korean", "Chinese", "Japanese", "Swedish", "Norwegian", "Finnish" + ], # Just chose some languages that came to mind + value="German", + label="Target Language", + ) + ], + outputs=gr.Textbox( + label="Translated Sentence" + ) + ) + + +if __name__ == '__main__': + translator.launch() diff --git a/assignment7/utils.py b/assignment7/utils.py index a7a7de9..e03d868 100644 --- a/assignment7/utils.py +++ b/assignment7/utils.py @@ -28,3 +28,31 @@ def format_as_chat(message: str, history: List[List[str]]) -> str: f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|>" return formatted_prompt + + +def generate_payload(prompt: str): + """ + Given a formatted prompt, returns an object to be used when posting to the API + + :param prompt: A formatted prompt following Llama 3's multi-turn conversation structure + """ + payload = { + "inputs": prompt, + "parameters": { + "do_sample": False + } + } + return payload + + +def format_for_translator(sentence: str, trg_lang: str): + # Wrap sentence with translation instruction + instruction = ( + f'Translate the following sentence into { + trg_lang} using the native script: "{sentence}". ' + "Only output the translated sentence in the native script." + ) + + # Create the prompt structure + formatted_prompt = "<|begin_of_text|>" + return formatted_prompt + f"<|start_header_id|>user<|end_header_id|>\n\n{instruction}<|eot_id|>" -- GitLab