Ashish Ranjan Karn commited on
Commit
61ffcf2
·
1 Parent(s): 321d902

initial commit

Browse files
Files changed (2) hide show
  1. app.py +127 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import re
4
+ import os
5
+ import speech_recognition as sr
6
+
7
+
8
+ stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
9
+ r = sr.Recognizer()
10
+
11
+ def transcribe(audio):
12
+ with sr.AudioFile(audio) as source:
13
+ audio_ = r.listen(source)
14
+ text = r.recognize_google(audio_)#, language = 'en-IN')# , show_all=True)
15
+ return text
16
+
17
+ #########
18
+
19
+ import gradio as gr
20
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
21
+ # import torch
22
+
23
+ # this model was loaded from https://hf.co/models
24
+ model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
25
+ tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
26
+ device = -1 #0 if torch.cuda.is_available() else -1
27
+ LANGS = ["ace_Arab", "eng_Latn", "fra_Latn", "spa_Latn"]
28
+
29
+ def translate(text, src_lang, tgt_lang):
30
+ """
31
+ Translate the text from source lang to target lang
32
+ """
33
+ translation_pipeline = pipeline("translation", model=model, tokenizer=tokenizer, src_lang=src_lang, tgt_lang=tgt_lang, max_length=400, device=device)
34
+ result = translation_pipeline(text)
35
+ return result[0]['translation_text']
36
+
37
+ # demo = gr.Interface(
38
+ # fn=translate,
39
+ # inputs=[
40
+ # gr.components.Textbox(label="Text"),
41
+ # gr.components.Dropdown(label="Source Language", choices=LANGS),
42
+ # gr.components.Dropdown(label="Target Language", choices=LANGS),
43
+ # ],
44
+ # outputs=["text"],
45
+ # examples=[["Building a translation demo with Gradio is so easy!", "eng_Latn", "spa_Latn"]],
46
+ # cache_examples=False,
47
+ # title="Translation Demo",
48
+ # description="This demo is a simplified version of the original [NLLB-Translator](https://huggingface.co/spaces/Narrativaai/NLLB-Translator) space"
49
+ # )
50
+
51
+
52
+ with gr.Blocks() as demo:
53
+
54
+ gr.Markdown("Translate your voice to any of following language - Speech to text -> Translation - Facebook NLL")
55
+ with gr.Tab("Audio Input"):
56
+ audio_input = gr.Audio(source="microphone", type="filepath")
57
+ sr_lang1 = gr.Dropdown(label="Source Language", choices=LANGS)
58
+ tr_lang1 = gr.Dropdown(label="Target Language", choices=LANGS)
59
+ submit_audio_button = gr.Button("Translate")
60
+ text_output = gr.Textbox(label="You said:")
61
+
62
+ with gr.Tab("Text Input"):
63
+ text_input = gr.Textbox(label="Enter text")
64
+ sr_lang = gr.Dropdown(label="Source Language", choices=LANGS)
65
+ tr_lang = gr.Dropdown(label="Target Language", choices=LANGS)
66
+ submit_button_text = gr.Button("Translate")
67
+
68
+
69
+
70
+ translated_output = gr.Textbox(label="Output Box")
71
+ # sd_output = gr.Gallery().style(grid=2, height="auto")
72
+
73
+ submit_audio_button.click(fn=transcribe, inputs=audio_input, outputs=text_output)
74
+ text_output.change(fn=translate, inputs=[text_output, sr_lang1, tr_lang1], outputs=translated_output)
75
+ submit_button_text.click(fn=translate, inputs=[text_input, sr_lang2, tr_lang2], outputs=translated_output)
76
+
77
+
78
+ demo.launch()
79
+
80
+
81
+ ##########
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ # def get_images(prompt):
97
+ # gallery_dir = stable_diffusion(prompt, fn_index=2)
98
+ # return [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]
99
+
100
+ # with gr.Blocks() as demo:
101
+
102
+ # gr.Markdown("Stable diffusion magic -> Get the photo from whatever you can think of!")
103
+ # with gr.Tab("Audio Input"):
104
+ # audio_input = gr.Audio(source="microphone", type="filepath")
105
+ # submit_audio_button = gr.Button("Convert to Image")
106
+ # text_output = gr.Textbox(label="Recorded text")
107
+
108
+ # with gr.Tab("Text Input"):
109
+ # text_input = gr.Textbox(label="Enter text")
110
+ # submit_button_text = gr.Button("Convert to Image")
111
+
112
+
113
+
114
+ # # output = gr.Textbox(label="Output Box")
115
+ # sd_output = gr.Gallery().style(grid=2, height="auto")
116
+
117
+ # submit_audio_button.click(fn=transcribe, inputs=audio_input, outputs=text_output)
118
+ # text_output.change(fn=get_images, inputs=text_output, outputs=sd_output)
119
+ # submit_button_text.click(fn=get_images, inputs=text_input, outputs=sd_output)
120
+
121
+
122
+ # demo.launch()
123
+
124
+
125
+
126
+
127
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ SpeechRecognition==3.8.1
2
+ transformers