zmbfeng commited on
Commit
94aba93
·
1 Parent(s): 7753623

the real thing

Browse files
Files changed (2) hide show
  1. app.py +35 -4
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,7 +1,38 @@
1
  import gradio as gr
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import random
3
+ from gtts import gTTS
4
+ import base64
5
+ from io import BytesIO
6
+ from transformers import pipeline
7
+ #https://huggingface.co/facebook/opt-1.3b
8
+ generator = pipeline('text-generation', model="facebook/opt-1.3b")
9
 
10
+ def create_audio(input_str):
11
+ output_raw= generator(input_str)
12
+ """print (output_raw)"""
13
 
14
+ output_str = output_raw[0]['generated_text']
15
+ output_str = output_str.replace("\n", "")
16
+ output_str = output_str.replace(input_str, "")
17
+
18
+ # Convert the sentence to speech using gTTS
19
+ tts = gTTS(text=output_str, lang="en")
20
+ # Save the speech as a bytestring in memory
21
+ tts_io = BytesIO()
22
+ tts.write_to_fp(tts_io)
23
+ tts_io.seek(0)
24
+ tts_data = tts_io.read()
25
+ # Encode the speech data in base64
26
+ tts_base64 = base64.b64encode(tts_data).decode("utf-8")
27
+ # Return the html code to play the audio
28
+ html = f"<audio src='data:audio/mpeg;base64,{tts_base64}' controls autoplay></audio>"
29
+ return (output_raw, html)
30
+
31
+ demo = gr.Interface(
32
+ fn=create_audio,
33
+ inputs="text",
34
+ outputs=["text","html"],
35
+ title="Random Audio Sentence Generator",
36
+ description="This interface generates a random audio sentence and plays it as a html output."
37
+ )
38
+ demo.launch()
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- gradio
 
 
 
1
+ gradio
2
+ transformers
3
+ gTTS