nouman66 commited on
Commit
44b8c4f
1 Parent(s): 7b10778

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -27
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import streamlit as st
2
- import speech_recognition as sr
3
  from transformers import pipeline
4
 
5
- st.title("Speech-to-Text and Translation Chatbot")
6
 
7
  # Choose the translation models from Hugging Face
8
  translation_models = {
@@ -12,6 +12,10 @@ translation_models = {
12
  "French to English": "Helsinki-NLP/opus-mt-fr-en",
13
  "English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
14
  "Urdu to English": "Helsinki-NLP/opus-mt-ur-en",
 
 
 
 
15
  # Add more language pairs as needed
16
  }
17
 
@@ -20,28 +24,33 @@ selected_translation = st.selectbox("Select translation model", list(translation
20
  # Load the translation pipeline
21
  translator = pipeline(task="translation", model=translation_models[selected_translation])
22
 
23
- # Function to perform speech-to-text and translation
24
- def speech_to_text_and_translate():
25
- recognizer = sr.Recognizer()
26
-
27
- st.info("Speak into your microphone...")
28
-
29
- with sr.Microphone() as source:
30
- audio_data = recognizer.listen(source, timeout=10)
31
-
32
- try:
33
- text = recognizer.recognize_google(audio_data)
34
- st.success(f"Speech-to-Text Result: {text}")
35
-
36
- # Perform translation
37
- translated_text = translator(text, max_length=500)[0]['translation_text']
38
- st.success(f"Translated Text: {translated_text}")
39
-
40
- except sr.UnknownValueError:
41
- st.warning("Speech recognition could not understand audio.")
42
- except sr.RequestError as e:
43
- st.error(f"Error with the speech recognition service: {e}")
44
-
45
- # Use speech-to-text and translation
46
- if st.button("Start Speech-to-Text and Translation"):
47
- speech_to_text_and_translate()
 
 
 
 
 
 
1
  import streamlit as st
2
+ import time
3
  from transformers import pipeline
4
 
5
+ st.title("Enhanced Multilingual Translator Chatbot")
6
 
7
  # Choose the translation models from Hugging Face
8
  translation_models = {
 
12
  "French to English": "Helsinki-NLP/opus-mt-fr-en",
13
  "English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
14
  "Urdu to English": "Helsinki-NLP/opus-mt-ur-en",
15
+ "English to Spanish": "Helsinki-NLP/opus-mt-en-es",
16
+ "Spanish to English": "Helsinki-NLP/opus-mt-es-en",
17
+ "English to Chinese": "Helsinki-NLP/opus-mt-en-zh",
18
+ "Chinese to English": "Helsinki-NLP/opus-mt-zh-en",
19
  # Add more language pairs as needed
20
  }
21
 
 
24
  # Load the translation pipeline
25
  translator = pipeline(task="translation", model=translation_models[selected_translation])
26
 
27
+ # User input for translation
28
+ user_input = st.text_area("Enter text for translation:", "")
29
+
30
+ # Display loading indicator
31
+ if st.button("Translate"):
32
+ with st.spinner("Translating..."):
33
+ # Simulate translation delay for demonstration
34
+ time.sleep(2)
35
+ if user_input:
36
+ # Perform translation
37
+ translated_text = translator(user_input, max_length=500)[0]['translation_text']
38
+ st.success(f"Translated Text: {translated_text}")
39
+ else:
40
+ st.warning("Please enter text for translation.")
41
+
42
+ # Clear button to reset input and result
43
+ if st.button("Clear"):
44
+ user_input = ""
45
+ st.success("Input cleared.")
46
+ st.empty() # Clear previous results if any
47
+
48
+ st.markdown("---")
49
+
50
+ st.subheader("About")
51
+ st.write(
52
+ "This is an enhanced Multilingual Translator chatbot that uses the Hugging Face Transformers library."
53
+ )
54
+ st.write(
55
+ "Select a translation model from the dropdown, enter text, and click 'Translate' to see the translation."
56
+ )