Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| import os | |
| os.system('pip install transformers') | |
| import os | |
| os.system('pip install torch') | |
| import os | |
| os.system('pip install tensorflow') | |
| import os | |
| os.system('pip install soundfile') | |
| import soundfile as sf | |
| from transformers import VitsModel, AutoTokenizer | |
| import numpy as np | |
| import io | |
| import torch | |
| print(torch.__version__) | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.metrics import classification_report, confusion_matrix | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import nltk | |
| nltk.download('punkt') | |
| nltk.download('averaged_perceptron_tagger') | |
| from wordcloud import WordCloud | |
| import zipfile | |
| import os | |
| model = VitsModel.from_pretrained("facebook/mms-tts-eng") | |
| tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng") | |
| def generate_speech(text): | |
| df = pd.read_csv("sample_data/Restaurant_Reviews.tsv", sep='\t') | |
| df=df.head(200) | |
| df.shape | |
| df.head() | |
| sns.countplot(x='Liked', data=df) | |
| plt.title('Sentiment Distribution (0 = Negative, 1 = Positive)') | |
| plt.show() | |
| example=df['Review'][56] | |
| example | |
| vectorizer = TfidfVectorizer(max_features=5000) | |
| X = vectorizer.fit_transform(df['Review']) | |
| y = df['Liked'] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) | |
| model = LogisticRegression() | |
| model.fit(X_train, y_train) | |
| y_pred = model.predict(X_test) | |
| print(classification_report(y_test, y_pred)) | |
| cm = confusion_matrix(y_test, y_pred) | |
| sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=model.classes_, yticklabels=model.classes_) | |
| plt.xlabel('Predicted') | |
| plt.ylabel('Actual') | |
| plt.title('Confusion Matrix') | |
| plt.show() | |
| import ipywidgets as widgets | |
| from IPython.display import display | |
| def predict_sentiment(review): | |
| # Transform the input review using the vectorizer | |
| input_vector = vectorizer.transform([review]) | |
| # Predict sentiment | |
| prediction = model.predict(input_vector)[0] | |
| # Return the sentiment prediction | |
| return "Positive" if prediction == 1 else "Negative" | |
| # Text input widget | |
| review_input = widgets.Text( | |
| placeholder='Enter your review here...', | |
| description='Review:', | |
| disabled=False | |
| ) | |
| # Button widgets for sample reviews | |
| sample_reviews = [ | |
| "The food was delicious and the service was excellent!", | |
| "Terrible experience, food was cold and service was slow." | |
| ] | |
| sample_buttons = [widgets.Button(description=review) for review in sample_reviews] | |
| def on_button_click(b): | |
| review_input.value = b.description | |
| for button in sample_buttons: | |
| button.on_click(on_button_click) | |
| # Output widget for displaying predictions | |
| output = widgets.Output() | |
| def update_output(change): | |
| output.clear_output() | |
| with output: | |
| prediction = predict_sentiment(change['new']) | |
| print(f"Predicted Sentiment: {prediction}") | |
| review_input.observe(update_output, names='value') | |
| # Display widgets | |
| display(review_input) | |
| display(widgets.HBox(sample_buttons)) | |
| display(output) |