Spaces:
Sleeping
Sleeping
import gradio as gr | |
from deep_translator import GoogleTranslator | |
import nltk | |
from nltk.tokenize import word_tokenize | |
from nltk.corpus import stopwords | |
from nltk.stem import WordNetLemmatizer | |
# Download necessary NLTK data | |
nltk.download('punkt', quiet=True) | |
nltk.download('stopwords', quiet=True) | |
nltk.download('wordnet', quiet=True) | |
def natural_language_understanding(text): | |
tokens = word_tokenize(text.lower()) | |
stop_words = set(stopwords.words('english')) | |
lemmatizer = WordNetLemmatizer() | |
processed_tokens = [lemmatizer.lemmatize(token) for token in tokens if token not in stop_words] | |
return " ".join(processed_tokens) | |
def translate_text(text, target_language): | |
translator = GoogleTranslator(source='auto', target=target_language) | |
return translator.translate(text) | |
def process_input(input_text, feature, target_language): | |
if not input_text: | |
return "No input provided" | |
processed_text = natural_language_understanding(input_text) | |
if feature == "Translation": | |
result = translate_text(processed_text, target_language) | |
elif feature == "Transcription": | |
result = processed_text | |
else: | |
result = "Invalid feature selected" | |
return result | |
# Create Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# The Advanced Multi-Faceted Chatbot") | |
gr.Markdown("Enter text to interact with the chatbot. Choose a feature and specify language for translation if needed.") | |
input_text = gr.Textbox(label="Input Text") | |
with gr.Row(): | |
feature = gr.Radio(["Translation", "Transcription"], label="Feature") | |
target_language = gr.Textbox(label="Target Language (e.g., 'fr' for French)") | |
submit_button = gr.Button("Process") | |
result_text = gr.Textbox(label="Result") | |
submit_button.click( | |
process_input, | |
inputs=[input_text, feature, target_language], | |
outputs=result_text | |
) | |
# Launch the interface | |
demo.launch() |