|
"""Translate via Bloom.""" |
|
|
|
from textwrap import dedent |
|
from logzero import logger |
|
|
|
import gradio as gr |
|
import httpx |
|
|
|
|
|
api_url = "https://api-inference.huggingface.co/models/bigscience/bloom" |
|
timeout_ = httpx.Timeout(None, connect=10) |
|
|
|
|
|
def bloom_tr(prompt_, from_lang, to_lang, input_prompt="translate this", seed=2, timeout=timeout_): |
|
"""Translate via Bloom.""" |
|
prompt = dedent( |
|
f""" |
|
Instruction : Given an {from_lang} input sentence translate it into {to_lang} sentence. \n input : \"{prompt_}\" \n {to_lang} : |
|
""" |
|
).strip() |
|
if len(prompt) == 0: |
|
prompt = input_prompt |
|
|
|
json_ = { |
|
"inputs": prompt, |
|
"parameters": { |
|
"top_p": 0.9, |
|
"temperature": 1.1, |
|
"max_new_tokens": 250, |
|
"return_full_text": False, |
|
"do_sample": False, |
|
"seed": seed, |
|
"early_stopping": False, |
|
"length_penalty": 0.0, |
|
"eos_token_id": None, |
|
}, |
|
"options": { |
|
"use_cache": True, |
|
"wait_for_model": True, |
|
}, |
|
} |
|
|
|
|
|
|
|
try: |
|
response = httpx.post(api_url, json=json_, timeout=timeout) |
|
except Exception as exc: |
|
logger.error(exc) |
|
return str(exc) |
|
|
|
|
|
try: |
|
output = response.json() |
|
except Exception as exc: |
|
logger.error(exc) |
|
return str(exc) |
|
|
|
try: |
|
output_tmp = output[0]["generated_text"] |
|
except Exception as exc: |
|
logger.error(exc) |
|
return str(exc) |
|
|
|
solution = output_tmp.split(f"\n{to_lang}:")[0] |
|
|
|
if "\n\n" in solution: |
|
final_solution = solution.split("\n\n")[0] |
|
else: |
|
final_solution = solution |
|
|
|
try: |
|
_ = final_solution.splitlines()[-1] |
|
except Exception as exc: |
|
logger.error(exc) |
|
return str(exc) |
|
|
|
return _ |
|
|
|
|
|
langs = [ |
|
"German", |
|
"French", |
|
"Italian", |
|
"Japanese", |
|
"Russian" |
|
"Spanish", |
|
"Hindi", |
|
] |
|
|
|
demo = gr.Blocks() |
|
|
|
with demo: |
|
gr.Markdown("<h1><center>Translate with Bloom</center></h1>") |
|
gr.Markdown( |
|
dedent( |
|
""" |
|
## Model Details |
|
Reer to the space created by [Kishore](https://www.linkedin.com/in/kishore-kunisetty-925a3919a/) inorder to participate in [EuroPython22](https://huggingface.co/EuroPython2022) |
|
please like his project to support his contribution to EuroPython22. π |
|
""" |
|
).strip() |
|
) |
|
with gr.Row(): |
|
from_lang = gr.Dropdown( |
|
["English", "Chinese", ] + langs, |
|
value="English", |
|
label="select From language : ", |
|
) |
|
to_lang = gr.Dropdown( |
|
["Chinese", "English", ] + langs, |
|
value="Hindi", |
|
label="select to Language : ", |
|
) |
|
|
|
input_prompt = gr.Textbox( |
|
label="Enter the sentence : ", |
|
value=f'Instruction: ... \ninput: "from sentence" \n{to_lang} :', |
|
lines=6, |
|
) |
|
|
|
generated_txt = gr.Textbox(lines=7) |
|
|
|
b1 = gr.Button("translate") |
|
b1.click( |
|
|
|
bloom_tr, |
|
inputs=[input_prompt, from_lang, to_lang], |
|
outputs=generated_txt, |
|
) |
|
|
|
demo.launch(enable_queue=True, debug=True) |
|
|