CoolFace
Apppublic

EuroPython2022/Translate-with-Bloom

sourceHugging Facemitupdated 4y agoView on Hugging Face
33likes
app.py96 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2 3import gradio as gr4import requests5import os 6import json #7 8##Bloom9API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"10# HF_TOKEN = os.environ["HF_TOKEN"]11# headers = {"Authorization": f"Bearer {HF_TOKEN}"}12 13def translate(prompt_ , from_lang, to_lang, input_prompt = "translate this", seed = 42): 14 15  prompt =  f"Instruction : Given an {from_lang} input sentence translate it into {to_lang} sentence. \n input : \"{prompt_}\" \n {to_lang} : " 16  if len(prompt) == 0:17    prompt = input_prompt 18    19  json_ = {20            "inputs": prompt,21            "parameters": {22                            "top_p": 0.9,23                            "temperature": 1.1,24                            "max_new_tokens": 250,25                            "return_full_text": False,26                            "do_sample": False,27                            "seed": seed,28                            "early_stopping": False,29                            "length_penalty": 0.0,30                            "eos_token_id": None,31                          }, 32          "options": {33              "use_cache": True,34              "wait_for_model": True,35                     },36        }37  response = requests.request("POST", API_URL,  json=json_) # headers=headers38  # output = response.json()39  output = json.loads(response.content.decode("utf-8"))40  output_tmp = output[0]['generated_text']41  solution = output_tmp.split(f"\n{to_lang}:")[0]  42  43 44  if '\n\n' in solution:45    final_solution = solution.split("\n\n")[0] 46  else:47    final_solution = solution48  return final_solution49 50demo = gr.Blocks()51 52with demo:53  gr.Markdown("<h1><center>Translate with Bloom</center></h1>")54  gr.Markdown('''55## Model Details56BLOOM is an autoregressive Large Language Model (LLM), trained to continue text 57from a prompt on vast amounts of text data using industrial-scale computational 58resources. As such, it is able to output coherent text in 46 languages and 13 59programming languages that is hardly distinguishable from text written by humans. 60BLOOM can also be instructed to perform text tasks it hasn't been explicitly trained 61for, by casting them as text generation tasks.62 63## Project Details64In this project we are going to explore the translation capabitlies of "BLOOM".65 66## How to use67At the moment this space has only capacity to translate between English, Spanish and Hindi languages.68from languange is the languge you put in text box and to langauge is to what language you are intended to translate.69Select from language from the drop down. 70Select to language from the drop down.71 72people are encouraged to improve this space by contributing.73 74this space is created by [Kishore](https://www.linkedin.com/in/kishore-kunisetty-925a3919a/) inorder to participate in [EuroPython22](https://huggingface.co/EuroPython2022)75please like the project to support my contribution to EuroPython22. ๐Ÿ˜Š76''')77  with gr.Row():78    from_lang = gr.Dropdown(['English', 'Spanish', 'Hindi' , 'Bangla'], 79                            value='English', 80                            label='select From language : ')81    to_lang = gr.Dropdown(['English', 'Spanish', 'Hindi'], 82                          value='Hindi', 83                          label= 'select to Language : ')84 85  input_prompt = gr.Textbox(label="Enter the sentence : ", 86                            value=f"Instruction: ... \ninput: \"from sentence\" \n{to_lang} :",87                            lines=6)88  89  generated_txt = gr.Textbox(lines=3)90 91  b1 = gr.Button("translate")92  b1.click(translate,inputs=[ input_prompt, from_lang, to_lang], outputs=generated_txt) 93    94demo.launch(enable_queue=True, debug=True)95 96