RTHGV commited on
Commit
3b79365
·
verified ·
1 Parent(s): 3f41c36

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from googletrans import Translator
3
+ import nltk
4
+ from nltk.tokenize import word_tokenize
5
+ from nltk.corpus import stopwords
6
+ from nltk.stem import WordNetLemmatizer
7
+
8
+ # Download necessary NLTK data
9
+ nltk.download('punkt', quiet=True)
10
+ nltk.download('stopwords', quiet=True)
11
+ nltk.download('wordnet', quiet=True)
12
+
13
+ # Initialize components
14
+ translator = Translator()
15
+
16
+ def natural_language_understanding(text):
17
+ tokens = word_tokenize(text.lower())
18
+ stop_words = set(stopwords.words('english'))
19
+ lemmatizer = WordNetLemmatizer()
20
+ processed_tokens = [lemmatizer.lemmatize(token) for token in tokens if token not in stop_words]
21
+ return " ".join(processed_tokens)
22
+
23
+ def translate_text(text, target_language):
24
+ translated = translator.translate(text, dest=target_language)
25
+ return translated.text
26
+
27
+ def process_input(input_text, feature, target_language):
28
+ if not input_text:
29
+ return "No input provided"
30
+
31
+ processed_text = natural_language_understanding(input_text)
32
+
33
+ if feature == "Translation":
34
+ result = translate_text(processed_text, target_language)
35
+ elif feature == "Transcription":
36
+ result = processed_text
37
+ else:
38
+ result = "Invalid feature selected"
39
+
40
+ return result
41
+
42
+ # Create Gradio interface
43
+ iface = gr.Interface(
44
+ fn=process_input,
45
+ inputs=[
46
+ gr.Textbox(label="Input Text"),
47
+ gr.Radio(["Translation", "Transcription"], label="Feature"),
48
+ gr.Textbox(label="Target Language (for translation)")
49
+ ],
50
+ outputs=gr.Textbox(label="Result"),
51
+ title="Simple Multi-Faceted Chatbot",
52
+ description="Enter text, choose a feature, and specify a target language for translation if needed."
53
+ )
54
+
55
+ # Launch the interface
56
+ iface.launch()