anumukhe commited on
Commit
bcf7fc3
·
1 Parent(s): 0ca06fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import json
5
+
6
+ API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
7
+ headers = {"Authorization": f"Bearer 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"}
8
+
9
+ def translate(prompt_ , from_lang, to_lang, input_prompt = "translate this", seed = 42):
10
+
11
+
12
+ prompt = f"To say \"{prompt_}\" in {to_lang}, you would say "
13
+ print(prompt)
14
+ if len(prompt) == 0:
15
+ prompt = input_prompt
16
+
17
+ json_ = {
18
+ "inputs": prompt,
19
+ "parameters": {
20
+ "top_p": 0.9,
21
+ "top_k": 100,
22
+ "temperature": 1.1,
23
+ "max_new_tokens": 250,
24
+ "return_full_text": True,
25
+ "do_sample": True,
26
+ "num_beams": 3,
27
+ "seed": seed,
28
+ "early_stopping": False,
29
+ "length_penalty": 0.0,
30
+ "eos_token_id": None,
31
+ "repetition_penalty": 3.0,
32
+ },
33
+ "options": {
34
+ "use_cache": True,
35
+ "wait_for_model": True,
36
+ },
37
+ }
38
+ response = requests.post(API_URL, json=json_, headers=headers)
39
+ print(f"Response is : {response}")
40
+ output = response.json()
41
+ print(f"output is : {output}")
42
+ #output = json.loads(response.content.decode("utf-8"))
43
+ output_tmp = output[0]['generated_text']
44
+ print(f"output_tmp is: {output_tmp}")
45
+
46
+ solution = output_tmp.split(f"\n{to_lang}:")[0]
47
+
48
+
49
+ if '\n\n' in solution:
50
+ final_solution = solution.split("\n\n")[0]
51
+ else:
52
+ final_solution = solution
53
+
54
+ return final_solution
55
+
56
+ demo = gr.Blocks()
57
+
58
+ with demo:
59
+ gr.Markdown("<h1><center>Bloom Translation</center></h1>")
60
+
61
+ with gr.Row():
62
+ from_lang = gr.Dropdown(['English', 'Spanish', 'Hindi' , 'Bangla'],
63
+ value='English',
64
+ label='select From language : ')
65
+ to_lang = gr.Dropdown(['English', 'Spanish', 'Hindi'],
66
+ value='Hindi',
67
+ label= 'select to Language : ')
68
+
69
+ input_prompt = gr.Textbox(label="Enter the sentence : ",
70
+ value=f"Instruction: ... \ninput: \"from sentence\" \n{to_lang} :",
71
+ lines=6)
72
+
73
+ generated_txt = gr.Textbox(lines=3)
74
+
75
+ b1 = gr.Button("translate")
76
+ b1.click(translate,inputs=[ input_prompt, from_lang, to_lang], outputs=generated_txt)
77
+
78
+ demo.launch(enable_queue=True, debug=True)
79
+