Spaces:
Build error
Build error
| import streamlit as st | |
| import joblib as j | |
| import re as rg | |
| from nltk.stem import PorterStemmer | |
| import speech_recognition as sr | |
| import os | |
| import gdown | |
| import joblib as j | |
| ''' | |
| model_file = 'random_forest_model.pkl' | |
| model_url = 'https://drive.google.com/uc?id=1suMJ0qgG5-oLhm_Jmyy7sst_myuiI2zg' | |
| if not os.path.exists(model_file): | |
| print("Model file not found. Downloading from Google Drive...") | |
| gdown.download(model_url, model_file, quiet=False) | |
| else: | |
| print("Model file found. Proceeding to load.") | |
| model = j.load(model_file) | |
| ''' | |
| # ----------------------------------------------------------- | |
| # 1) Load Vectorizer & Model | |
| # ----------------------------------------------------------- | |
| vectorizer = j.load('tfidf_vectorizer.pkl') | |
| model = j.load('random_forest_model.pkl') | |
| pt = PorterStemmer() | |
| # ----------------------------------------------------------- | |
| # 2) Preprocessing Function | |
| # ----------------------------------------------------------- | |
| def preprocessing(text): | |
| text = rg.sub('[^a-zA-Z0-9\\s]', '', text.lower()) | |
| words = [pt.stem(word) for word in text.split()] | |
| return " ".join(words) | |
| # ----------------------------------------------------------- | |
| # 3) Prediction Function | |
| # ----------------------------------------------------------- | |
| def predict(text): | |
| preprocessed_text = preprocessing(text) | |
| vector = vectorizer.transform([preprocessed_text]) | |
| return model.predict(vector)[0] | |
| # ----------------------------------------------------------- | |
| # 4) Speech Recognition | |
| # ----------------------------------------------------------- | |
| def recognize_speech(): | |
| recognizer = sr.Recognizer() | |
| with sr.Microphone() as source: | |
| st.write("Listening... Please speak now.") | |
| try: | |
| audio = recognizer.listen(source, timeout=5) | |
| text = recognizer.recognize_google(audio) | |
| return text | |
| except sr.WaitTimeoutError: | |
| st.write("Listening timed out while waiting for phrase to start.") | |
| except sr.UnknownValueError: | |
| st.write("Sorry, could not understand the audio.") | |
| except sr.RequestError as e: | |
| st.write(f"Could not request results from Google Speech Recognition service; {e}") | |
| return "" | |
| # ----------------------------------------------------------- | |
| # 5) Set up Session State | |
| # ----------------------------------------------------------- | |
| # Initialize a key in session_state to store spoken text | |
| if "spoken_text" not in st.session_state: | |
| st.session_state["spoken_text"] = "" | |
| # ----------------------------------------------------------- | |
| # 6) Streamlit UI | |
| # ----------------------------------------------------------- | |
| st.title("Mental Health Sentiment Analysis") | |
| # Radio for input method | |
| input_option = st.radio("Choose input method:", ("Type Text", "Speak Text")) | |
| if input_option == "Type Text": | |
| # Use a local variable for typed text | |
| input_text = st.text_area("Enter text for sentiment analysis:") | |
| if st.button("Predict"): | |
| if input_text.strip(): | |
| result = predict(input_text) | |
| st.write(f"Predicted Sentiment: {result}") | |
| else: | |
| st.write("Please enter some text.") | |
| elif input_option == "Speak Text": | |
| # Display what is currently stored in session_state | |
| if st.session_state["spoken_text"]: | |
| st.write(f"You said: {st.session_state['spoken_text']}") | |
| # Button to start recording | |
| if st.button("Start Recording"): | |
| recognized = recognize_speech() | |
| if recognized: | |
| st.session_state["spoken_text"] = recognized | |
| st.write(f"You said: {recognized}") | |
| # Button to predict | |
| if st.button("Predict"): | |
| if st.session_state["spoken_text"].strip(): | |
| result = predict(st.session_state["spoken_text"]) | |
| st.write(f"Predicted Sentiment: {result}") | |
| else: | |
| st.write("Please record some speech first.") | |