Jour commited on
Commit
9c9173d
1 Parent(s): 6c65174

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import gradio as gr
4
+ import requests
5
+ import os
6
+ import json #
7
+
8
+ ##Bloom
9
+ API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom-350m"
10
+ # HF_TOKEN = os.environ["HF_TOKEN"]
11
+ # headers = {"Authorization": f"Bearer {HF_TOKEN}"}
12
+
13
+ def 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=headers
38
+ # 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 = solution
48
+ return final_solution
49
+
50
+ demo = gr.Blocks()
51
+
52
+ with demo:
53
+ gr.Markdown("<h1><center>Translate with Bloom</center></h1>")
54
+ gr.Markdown('''
55
+ ## Model Details
56
+ BLOOM is an autoregressive Large Language Model (LLM), trained to continue text
57
+ from a prompt on vast amounts of text data using industrial-scale computational
58
+ resources. As such, it is able to output coherent text in 46 languages and 13
59
+ programming languages that is hardly distinguishable from text written by humans.
60
+ BLOOM can also be instructed to perform text tasks it hasn't been explicitly trained
61
+ for, by casting them as text generation tasks.
62
+
63
+ ## Project Details
64
+ In this project we are going to explore the translation capabitlies of "BLOOM".
65
+
66
+ ## How to use
67
+ At the moment this space has only capacity to translate between English, Spanish and Hindi languages.
68
+ from languange is the languge you put in text box and to langauge is to what language you are intended to translate.
69
+ Select from language from the drop down.
70
+ Select to language from the drop down.
71
+
72
+ people are encouraged to improve this space by contributing.
73
+
74
+ this space is created by [Kishore](https://www.linkedin.com/in/kishore-kunisetty-925a3919a/) inorder to participate in [EuroPython22](https://huggingface.co/EuroPython2022)
75
+ please like the project to support my contribution to EuroPython22. 😊
76
+ ''')
77
+ with gr.Row():
78
+ from_lang = gr.Dropdown(['English', 'Spanish', 'Hindi'],
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
+
94
+ demo.launch(enable_queue=True, debug=True)
95
+