0xZee commited on
Commit
c38b030
·
1 Parent(s): 43b49bd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from gtts import gTTS
4
+
5
+ # gpt2-large model text generation
6
+ def text_gen(prompt):
7
+ generator = pipeline("text-generation", model='gpt2-medium')
8
+ #generator = pipeline("text-generation", model='distilgpt2')
9
+ #res_base = generator(prompt, max_new_tokens=100, num_return_sequences=1)
10
+ #res_p = generator(prompt, max_new_tokens=100, num_return_sequences=1, do_sample=True, top_p=0.095)
11
+ #res_k = generator(prompt, max_new_tokens=120, num_return_sequences=1, do_sample=True, top_k=4)
12
+ #res_c = generator(prompt, max_new_tokens=100, num_return_sequences=1, penalty_alpha=0.6, top_k=4)
13
+ #<span style="font-family:Papyrus; font-size:2em;">👽 0xZee</span>
14
+ res = generator(prompt, max_new_tokens=100, num_return_sequences=1)
15
+ res = res[0]['generated_text']
16
+ # text_to_audio
17
+ audio = gTTS(text=res, lang='en', slow=False)
18
+ audio.save("res.wav")
19
+ return(res , "res.wav")
20
+
21
+ def complete_with_gpt(text):
22
+ complete = pipeline("text-generation", model='openai-gpt')
23
+ res = complete(text[-50:], max_new_tokens=100, num_return_sequences=1)
24
+ return res[0]['generated_text']
25
+
26
+
27
+ ##
28
+ with gr.Blocks() as demo:
29
+ with gr.Row():
30
+ gr.Markdown("""
31
+ > 👽 0xZee
32
+ # 📝 NLP : GPT Text Generation & Text-to-Speech Demo
33
+ > Demo #1 : **gtp2** Model (finetuned) and **gTTS** for text to audio
34
+ *Enter a text with context : Will genrate text and display audio.*
35
+ """)
36
+ #
37
+ with gr.Row():
38
+ with gr.Column():
39
+ prompt = gr.Textbox(label="Text Generation", placeholder="Text and context here")
40
+ 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])
41
+ btn = gr.Button(value="Go GPT")
42
+ #
43
+ with gr.Column():
44
+ audio_out = gr.Audio()
45
+ text_out = gr.Textbox(label="tuned mode")
46
+ btn.click(fn=text_gen, inputs=prompt, outputs=[text_out,audio_out])
47
+ #
48
+ with gr.Row():
49
+ gr.Markdown("""
50
+ #
51
+ # 🎲 NLP : GPT Text Auto Complete
52
+ > Demo #2 : Auto-Complete : **openai-gpt** Model
53
+ *Enter a text : will complete the text provided.*
54
+ """)
55
+ #
56
+ with gr.Row():
57
+ text = gr.Textbox(label="Text Complete", placeholder="Text here", lines=4)
58
+ with gr.Row():
59
+ gr.Examples(examples=["The Ai can help the world with", "tell me a story about" , "John is a cowboy, suddenly he is"], inputs=[text])
60
+ with gr.Row():
61
+ btn = gr.Button(value="Go & Complete")
62
+ btn.click(fn=complete_with_gpt, inputs=text, outputs=text, queue=False)
63
+
64
+
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch(share=True, debug=True)