File size: 2,897 Bytes
c38b030
 
 
5371175
 
 
 
c38b030
 
 
 
 
 
 
 
 
 
5371175
c38b030
 
 
 
 
 
 
 
5371175
c38b030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71bdf3b
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
import gradio as gr
from transformers import pipeline
from gtts import gTTS
import torch

# GPU control
device = 0 if torch.cuda.is_available() else -1

# gpt2-large model text generation
def text_gen(prompt):
  generator = pipeline("text-generation", model='gpt2-medium')
  #generator = pipeline("text-generation", model='distilgpt2')
  #res_base = generator(prompt, max_new_tokens=100, num_return_sequences=1)
  #res_p = generator(prompt, max_new_tokens=100, num_return_sequences=1, do_sample=True, top_p=0.095)
  #res_k = generator(prompt, max_new_tokens=120, num_return_sequences=1, do_sample=True, top_k=4)
  #res_c = generator(prompt, max_new_tokens=100, num_return_sequences=1, penalty_alpha=0.6, top_k=4)
  #<span style="font-family:Papyrus; font-size:2em;">👽 0xZee</span>
  res = generator(prompt, max_lenght=120, num_return_sequences=1, device=device)
  res = res[0]['generated_text']
  # text_to_audio
  audio = gTTS(text=res, lang='en', slow=False)
  audio.save("res.wav")
  return(res , "res.wav")

def complete_with_gpt(text):
  complete = pipeline("text-generation", model='openai-gpt')
  res = complete(text[-50:], max_new_tokens=100, num_return_sequences=1, device=device)
  return res[0]['generated_text']


##
with gr.Blocks() as demo:
    with gr.Row():
      gr.Markdown("""
                  > 👽 0xZee
                  # 📝 NLP : GPT Text Generation & Text-to-Speech Demo
                  > Demo #1 : **gtp2** Model (finetuned) and **gTTS** for text to audio
                  *Enter a text with context : Will genrate text and display audio.*
                  """)
#
    with gr.Row():
        with gr.Column():   
          prompt = gr.Textbox(label="Text Generation", placeholder="Text and context here")
          gr.Examples(examples=["The Ai can help the world with", "The Moon's orbit around Earth has" , "John is a cowboy, suddenly he"], inputs=[prompt])
          btn = gr.Button(value="Go GPT")
#
        with gr.Column():   
          audio_out = gr.Audio()
          text_out = gr.Textbox(label="tuned mode")
          btn.click(fn=text_gen, inputs=prompt, outputs=[text_out,audio_out])
#
    with gr.Row():
      gr.Markdown("""
                  #
                  # 🎲 NLP : GPT Text Auto Complete
                  > Demo #2 : Auto-Complete : **openai-gpt** Model 
                  *Enter a text : will complete the text provided.*
                  """)
#
    with gr.Row():
          text = gr.Textbox(label="Text Complete", placeholder="Text here", lines=4)
    with gr.Row():
          gr.Examples(examples=["The Ai can help the world with", "tell me a story about" , "John is a cowboy, suddenly he is"], inputs=[text])     
    with gr.Row():          
          btn = gr.Button(value="Go & Complete")
          btn.click(fn=complete_with_gpt, inputs=text, outputs=text, queue=False)



if __name__ == "__main__":
  demo.launch(debug=True)