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""" #
#

# {emoji} Prediction Result #

#

# Input Text: {text} #

#

# Cleaned Text: {cleaned} #

#

# Sentiment:  # {sentiment_text} #

#

# Confidence Score:  # {pct_display} #

#
# """ # except Exception as e: # return error_card(f"Prediction error: {str(e)}") # def error_card(msg): # return f""" #
#

โš ๏ธ {msg}

#
# """ # # =============================== # # CSS # # =============================== # css = """ # body, .gradio-container { # background: #0f172a !important; # font-family: Inter, sans-serif !important; # } # .gradio-container { # max-width: 720px !important; # margin: auto !important; # padding: 30px 20px !important; # } # textarea { # background: #1e2d40 !important; # border: 1px solid #2d4a6b !important; # border-radius: 12px !important; # color: #f1f5f9 !important; # font-size: 15px !important; # } # textarea::placeholder { color: #64748b !important; } # textarea:focus { # border-color: #3b82f6 !important; # box-shadow: 0 0 0 3px rgba(59,130,246,0.2) !important; # } # label span { color: #94a3b8 !important; font-size: 13px !important; } # button.primary { # background: linear-gradient(135deg, #3b82f6, #6366f1) !important; # border: none !important; # border-radius: 12px !important; # color: white !important; # font-size: 16px !important; # font-weight: 600 !important; # } # button.primary:hover { # transform: translateY(-2px) !important; # box-shadow: 0 8px 25px rgba(59,130,246,0.4) !important; # } # footer { display: none !important; } # """ # # =============================== # # UI # # =============================== # with gr.Blocks() as demo: # gr.HTML(""" #
#

# AI Sentiment Analyzer #

#

# Real-Time Twitter Sentiment Analysis using BiLSTM #

#
# """) # text_input = gr.Textbox( # label="Enter Text", # placeholder="Type or paste any tweet, review, or sentence...", # lines=4, # elem_id="text_input", # ) # gr.HTML(""" #
#

Try a sample โ†’

#
# # # # #
#
# # """) # analyze_btn = gr.Button("Analyze Sentiment", variant="primary", size="lg") # result_out = gr.HTML() # analyze_btn.click( # fn=predict_sentiment, # inputs=text_input, # outputs=result_out, # ) # gr.HTML(""" #
# Built with TFLite + Gradio  ยท  # GitHub โ†— #
# """) # demo.launch(ssr_mode=False, css=css)