Skip to content
Snippets Groups Projects
Commit 901edce2 authored by Lucas Mikael Roni Suomela's avatar Lucas Mikael Roni Suomela
Browse files

Completed the assignment

parent d37b9746
Branches master
No related tags found
No related merge requests found
venv
__pycache__
\ No newline at end of file
__pycache__
screenshots
\ No newline at end of file
......@@ -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
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()
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()
......@@ -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|>"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment