KishoreK commited on
Commit
53610ad
1 Parent(s): 344f724

Add application file

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