Vishwas1 commited on
Commit
274b298
·
1 Parent(s): 984e549

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ ##Bloom Inference API
6
+ API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
7
+ HF_TOKEN = os.environ["HF_TOKEN"]
8
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
9
+
10
+
11
+ def text_generate(prompt, generated_txt):
12
+ #Prints to debug the code
13
+ print(f"*****Inside text_generate - Prompt is :{prompt}")
14
+
15
+ json_ = {"inputs": prompt,
16
+ "parameters":
17
+ {
18
+ "top_p": 0.8,
19
+ "temperature": 1.1,
20
+ "max_new_tokens": 128,
21
+ "return_full_text": True,
22
+ "do_sample":False,
23
+ },
24
+ "options":
25
+ {"use_cache": False,
26
+ "wait_for_model": True,
27
+ },}
28
+ response = requests.post(API_URL, headers=headers, json=json_)
29
+ print(f"Response is : {response}")
30
+ output = response.json()
31
+ print(f"output is : {output}")
32
+ output_tmp = output[0]['generated_text']
33
+ print(f"output_tmp is: {output_tmp}")
34
+ solution = output_tmp.split("\nQ:")[0]
35
+ print(f"Final response after splits is: {solution}")
36
+ if '\nOutput:' in solution:
37
+ final_solution = solution.split("\nOutput:")[0]
38
+ print(f"Response after removing output is: {final_solution}")
39
+ elif '\n\n' in solution:
40
+ final_solution = solution.split("\n\n")[0]
41
+ print(f"Response after removing new line entries is: {final_solution}")
42
+ else:
43
+ final_solution = solution
44
+
45
+
46
+ if len(generated_txt) == 0 :
47
+ display_output = final_solution
48
+ else:
49
+ display_output = generated_txt[:-len(prompt)] + final_solution
50
+ new_prompt = final_solution[len(prompt):]
51
+ print(f"new prompt for next cycle is : {new_prompt}")
52
+ print(f"display_output for printing on screen is : {display_output}")
53
+ if len(new_prompt) == 0:
54
+ temp_text = display_output[::-1]
55
+ print(f"What is the last character of sentence? : {temp_text[0]}")
56
+ if temp_text[1] == '.':
57
+ first_period_loc = temp_text[2:].find('.') + 1
58
+ print(f"Location of last Period is: {first_period_loc}")
59
+ new_prompt = display_output[-first_period_loc:-1]
60
+ print(f"Not sending blank as prompt so new prompt for next cycle is : {new_prompt}")
61
+ else:
62
+ print("HERE")
63
+ first_period_loc = temp_text.find('.')
64
+ print(f"Location of last Period is : {first_period_loc}")
65
+ new_prompt = display_output[-first_period_loc:-1]
66
+ print(f"Not sending blank as prompt so new prompt for next cycle is : {new_prompt}")
67
+ display_output = display_output[:-1]
68
+
69
+ return display_output, new_prompt
70
+
71
+
72
+ demo = gr.Blocks()
73
+
74
+ with demo:
75
+ gr.Markdown("<h1><center>Write Stories Using Bloom</center></h1>")
76
+ gr.Markdown(
77
+ """This is my playground to test Marathi and Hindi Text Generation""
78
+ )
79
+ with gr.Row():
80
+ input_prompt = gr.Textbox(label="Write some text to get started...", lines=3, value="क्षितिजावर नजर जाईल तिथें")
81
+
82
+ with gr.Row():
83
+ generated_txt = gr.Textbox(lines=25, visible = True)
84
+
85
+ b1 = gr.Button("Generate Your Story")
86
+
87
+ b1.click(text_generate, inputs=[input_prompt, generated_txt], outputs=[generated_txt, input_prompt])
88
+
89
+ demo.launch(enable_queue=True, debug=True)