from huggingface_hub import login, upload_folder # (optional) Login with your Hugging Face credentials login() # Push your model files upload_folder(folder_path=r"D:\Python\DEEP LEARNING\New folder\sentiment_analyzer", repo_id="kanha1111/sentiment_analysis", repo_type="model") # import gradio as gr # import pickle # import re # import string # import nltk # import numpy as np # from nltk.corpus import stopwords # from nltk.stem import WordNetLemmatizer # # =============================== # # DOWNLOAD NLTK FILES # # =============================== # nltk.download('stopwords') # nltk.download('wordnet') # # =============================== # # LOAD TFLITE MODEL # # =============================== # try: # import tensorflow as tf # interpreter = tf.lite.Interpreter(model_path="sentiment_model.tflite") # interpreter.allocate_tensors() # input_details = interpreter.get_input_details() # output_details = interpreter.get_output_details() # MODEL_LOADED = True # except Exception as e: # MODEL_LOADED = False # MODEL_ERROR = str(e) # # =============================== # # LOAD TOKENIZER # # =============================== # with open("tokenizer.pkl", "rb") as f: # tokenizer = pickle.load(f) # # =============================== # # NLP TOOLS # # =============================== # stop_words = set(stopwords.words('english')) # lemmatizer = WordNetLemmatizer() # MAX_LEN = 100 # # =============================== # # PAD SEQUENCES (manual) # # =============================== # def manual_pad_sequences(sequences, maxlen): # padded = np.zeros((len(sequences), maxlen), dtype=np.float32) # for i, seq in enumerate(sequences): # if len(seq) > maxlen: # padded[i] = seq[-maxlen:] # else: # padded[i, maxlen - len(seq):] = seq # return padded # # =============================== # # CLEAN TEXT # # =============================== # def clean_text(text): # text = text.lower() # text = re.sub(r'http\S+', '', text) # text = re.sub(r'@\w+', '', text) # text = re.sub(r'#', '', text) # text = re.sub(r'\d+', '', text) # text = text.translate(str.maketrans('', '', string.punctuation)) # text = text.strip() # words = text.split() # words = [lemmatizer.lemmatize(w) for w in words if w not in stop_words] # return ' '.join(words) # # =============================== # # PREDICT FUNCTION # # =============================== # def predict_sentiment(text): # if not text or text.strip() == "": # return error_card("Please enter some text to analyze.") # if not MODEL_LOADED: # return error_card(f"Model load failed: {MODEL_ERROR}") # try: # cleaned = clean_text(text) # sequence = tokenizer.texts_to_sequences([cleaned]) # padded = manual_pad_sequences(sequence, MAX_LEN) # # TFLite model expects batch size 256 - pad input accordingly # batch_size = input_details[0]['shape'][0] # full_input = np.zeros((batch_size, MAX_LEN), dtype=np.float32) # full_input[0] = padded[0] # interpreter.set_tensor(input_details[0]['index'], full_input) # interpreter.invoke() # score = float(interpreter.get_tensor(output_details[0]['index'])[0][0]) # is_positive = score > 0.5 # pct = score if is_positive else (1 - score) # pct_display = f"{pct:.4f}" # if is_positive: # sentiment_text = "Positive Sentiment" # sentiment_color = "#22c55e" # emoji = "๐" # else: # sentiment_text = "Negative Sentiment" # sentiment_color = "#ef4444" # emoji = "๐" # return f""" #
# Input Text: {text} #
## Cleaned Text: {cleaned} #
## Sentiment: # {sentiment_text} #
## Confidence Score: # {pct_display} #
#โ ๏ธ {msg}
## Real-Time Twitter Sentiment Analysis using BiLSTM #
#Try a sample โ
#