Spaces:
Sleeping
Sleeping
File size: 3,285 Bytes
5114235 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
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) |