File size: 3,293 Bytes
274b298
 
59378ca
05aed58
59378ca
274b298
 
b063a57
 
274b298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f38cd
274b298
 
b483714
274b298
 
 
 
 
 
 
b40e3e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
import requests
import os



##Bloom Inference API
API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"

HF_TOKEN = os.environ["HF_TOKEN"]
headers = {"Authorization": f"Bearer {HF_TOKEN}"}


def text_generate(prompt, generated_txt): 
  #Prints to debug the code
  print(f"*****Inside text_generate - Prompt is :{prompt}")
  
  json_ = {"inputs": prompt,
            "parameters":
            {
            "top_p": 0.8,
          "temperature": 1.1,
          "max_new_tokens": 128,
          "return_full_text": True,
          "do_sample":False,
          }, 
          "options": 
          {"use_cache": False,
          "wait_for_model": True,
          },}
  response = requests.post(API_URL, headers=headers, json=json_)
  print(f"Response  is : {response}")
  output = response.json()
  print(f"output is : {output}") 
  output_tmp = output[0]['generated_text']
  print(f"output_tmp is: {output_tmp}")
  solution = output_tmp.split("\nQ:")[0]   
  print(f"Final response after splits is: {solution}")
  if '\nOutput:' in solution:
    final_solution = solution.split("\nOutput:")[0] 
    print(f"Response after removing output is: {final_solution}")
  elif '\n\n' in solution:
    final_solution = solution.split("\n\n")[0] 
    print(f"Response after removing new line entries is: {final_solution}")
  else:
    final_solution = solution
  
  
  if len(generated_txt) == 0 :
    display_output = final_solution
  else:
    display_output = generated_txt[:-len(prompt)] + final_solution
  new_prompt = final_solution[len(prompt):]
  print(f"new prompt for next cycle is : {new_prompt}")
  print(f"display_output for printing on screen is : {display_output}")
  if len(new_prompt) == 0:
    temp_text = display_output[::-1]
    print(f"What is the last character of sentence? : {temp_text[0]}")
    if temp_text[1] == '.':
      first_period_loc = temp_text[2:].find('.') + 1
      print(f"Location of last Period is: {first_period_loc}")
      new_prompt = display_output[-first_period_loc:-1]
      print(f"Not sending blank as prompt so new prompt for next cycle is : {new_prompt}")
    else:
      print("HERE")
      first_period_loc = temp_text.find('.')
      print(f"Location of last Period is : {first_period_loc}")
      new_prompt = display_output[-first_period_loc:-1]
      print(f"Not sending blank as prompt so new prompt for next cycle is : {new_prompt}")
    display_output = display_output[:-1]
    
  return display_output, new_prompt  


demo = gr.Blocks()

with demo:
  gr.Markdown("<h1><center>Write Stories Using Bloom</center></h1>")
  gr.Markdown(
        """This is my playground to test Marathi and Hindi Text Generation"""
        )
  with gr.Row():
    input_prompt = gr.Textbox(label="Write some text to get started...", lines=3, value="मैं असीम अकेला सन्नाटा हूँ \nमैं अनित्य चेतना-सनातन रचना हूँ \nमैं अनासक्त समय हूँ ")
  with gr.Row():
    generated_txt = gr.Textbox(lines=25, visible = True)
  
  b1 = gr.Button("Generate Your Story")

  b1.click(text_generate, inputs=[input_prompt, generated_txt], outputs=[generated_txt, input_prompt]) 

demo.launch(enable_queue=True, debug=True)