RTHGV commited on
Commit
96f7258
·
verified ·
1 Parent(s): cc0da2c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip install SpeechRecognition googletrans==3.1.0a0 textblob nltk tensorflow gtts gradio
2
+
3
+ import speech_recognition as sr
4
+ from googletrans import Translator
5
+ from textblob import TextBlob
6
+ import nltk
7
+ from nltk.tokenize import word_tokenize
8
+ from nltk.corpus import stopwords
9
+ from nltk.stem import WordNetLemmatizer
10
+ from gtts import gTTS
11
+ import gradio as gr
12
+ import tempfile
13
+ import os
14
+
15
+ # Download necessary NLTK data
16
+ nltk.download('punkt', quiet=True)
17
+ nltk.download('stopwords', quiet=True)
18
+ nltk.download('wordnet', quiet=True)
19
+
20
+ # Initialize components
21
+ recognizer = sr.Recognizer()
22
+ translator = Translator()
23
+
24
+ def natural_language_understanding(text):
25
+ tokens = word_tokenize(text.lower())
26
+ stop_words = set(stopwords.words('english'))
27
+ lemmatizer = WordNetLemmatizer()
28
+
29
+ processed_tokens = [lemmatizer.lemmatize(token) for token in tokens if token not in stop_words]
30
+ return " ".join(processed_tokens)
31
+
32
+ def translate_text(text, target_language):
33
+ translated = translator.translate(text, dest=target_language)
34
+ return translated.text
35
+
36
+ def text_to_speech(text):
37
+ tts = gTTS(text)
38
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
39
+ tts.save(fp.name)
40
+ return fp.name
41
+
42
+ def process_input(input_text, input_audio, feature, target_language, output_language):
43
+ if input_audio is not None:
44
+ # Process audio input
45
+ try:
46
+ with sr.AudioFile(input_audio) as source:
47
+ audio = recognizer.record(source)
48
+ input_text = recognizer.recognize_google(audio)
49
+ except sr.UnknownValueError:
50
+ return "Could not understand audio", None
51
+ except sr.RequestError:
52
+ return "Could not request results from speech recognition service", None
53
+ except Exception as e:
54
+ return f"An error occurred: {str(e)}", None
55
+
56
+ if not input_text:
57
+ return "No input provided", None
58
+
59
+ processed_text = natural_language_understanding(input_text)
60
+
61
+ if feature == "Translation":
62
+ result = translate_text(processed_text, target_language)
63
+ elif feature == "Voice Command":
64
+ result = "Voice command feature not implemented in this example"
65
+
66
+ elif feature == "Transcription":
67
+ result = processed_text
68
+ else:
69
+ result = "Invalid feature selected"
70
+
71
+ if output_language:
72
+ result = translate_text(result, output_language)
73
+
74
+ return result, None
75
+
76
+ def tts_function(text):
77
+ if text:
78
+ return text_to_speech(text)
79
+ return None
80
+
81
+ # Create Gradio interface
82
+ with gr.Blocks() as iface:
83
+ gr.Markdown("# The Advanced Multi-Faceted Chatbot")
84
+ gr.Markdown("Enter text or speak to interact with the chatbot. Choose a feature and specify languages for translation if needed.")
85
+
86
+ with gr.Row():
87
+ input_text = gr.Textbox(label="Input Text")
88
+ input_audio = gr.Audio(label="Input Audio", type="filepath")
89
+
90
+ with gr.Row():
91
+ feature = gr.Radio(["Translation", "Voice Command", "Transcription"], label="Feature")
92
+ target_language = gr.Textbox(label="Target Language ")
93
+ output_language = gr.Textbox(label="Output Language ")
94
+
95
+ submit_button = gr.Button("Process")
96
+
97
+ result_text = gr.Textbox(label="Result")
98
+ tts_button = gr.Button("Convert to Speech")
99
+ audio_output = gr.Audio(label="Audio Output")
100
+
101
+ submit_button.click(
102
+ process_input,
103
+ inputs=[input_text, input_audio, feature, target_language, output_language],
104
+ outputs=[result_text, audio_output]
105
+ )
106
+
107
+ tts_button.click(
108
+ tts_function,
109
+ inputs=[result_text],
110
+ outputs=[audio_output]
111
+ )
112
+
113
+ # Launch the interface
114
+ iface.launch()