alyzbane commited on
Commit
be44902
·
1 Parent(s): 8fe0bec

added app.py

Browse files
Files changed (12) hide show
  1. .dockerignore +96 -0
  2. .gitignore +173 -0
  3. Dockerfile +19 -0
  4. app.py +114 -0
  5. lr/model.pkl +3 -0
  6. lr/vectorizer.pkl +3 -0
  7. mnb/model.pkl +3 -0
  8. mnb/vectorizer.pkl +3 -0
  9. rf/model.pkl +3 -0
  10. rf/vectorizer.pkl +3 -0
  11. svm/model.pkl +3 -0
  12. svm/vectorizer.pkl +3 -0
.dockerignore ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Git
2
+ .git
3
+ .gitignore
4
+
5
+ # CI
6
+ .codeclimate.yml
7
+ .travis.yml
8
+ .taskcluster.yml
9
+
10
+ # Docker
11
+ docker-compose.yml
12
+ .docker
13
+
14
+ # Byte-compiled / optimized / DLL files
15
+ __pycache__/
16
+ */__pycache__/
17
+ */*/__pycache__/
18
+ */*/*/__pycache__/
19
+ *.py[cod]
20
+ */*.py[cod]
21
+ */*/*.py[cod]
22
+ */*/*/*.py[cod]
23
+
24
+ # C extensions
25
+ *.so
26
+
27
+ # Distribution / packaging
28
+ .Python
29
+ env/
30
+ build/
31
+ develop-eggs/
32
+ dist/
33
+ downloads/
34
+ eggs/
35
+ lib/
36
+ lib64/
37
+ parts/
38
+ sdist/
39
+ var/
40
+ *.egg-info/
41
+ .installed.cfg
42
+ *.egg
43
+
44
+ # PyInstaller
45
+ # Usually these files are written by a python script from a template
46
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
47
+ *.manifest
48
+ *.spec
49
+
50
+ # Installer logs
51
+ pip-log.txt
52
+ pip-delete-this-directory.txt
53
+
54
+ # Unit test / coverage reports
55
+ htmlcov/
56
+ .tox/
57
+ .coverage
58
+ .cache
59
+ nosetests.xml
60
+ coverage.xml
61
+
62
+ # Translations
63
+ *.mo
64
+ *.pot
65
+
66
+ # Django stuff:
67
+ *.log
68
+
69
+ # Sphinx documentation
70
+ docs/_build/
71
+
72
+ # PyBuilder
73
+ target/
74
+
75
+ # Virtual environment
76
+ .env/
77
+ .venv/
78
+ venv/
79
+
80
+ # PyCharm
81
+ .idea
82
+
83
+ # Python mode for VIM
84
+ .ropeproject
85
+ */.ropeproject
86
+ */*/.ropeproject
87
+ */*/*/.ropeproject
88
+
89
+ # Vim swap files
90
+ *.swp
91
+ */*.swp
92
+ */*/*.swp
93
+ */*/*/*.swp
94
+
95
+ # Local Files
96
+ notebook/*
.gitignore ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # PyPI configuration file
171
+ .pypirc
172
+
173
+ .gradio/*
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.10-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /usr/src/app
6
+
7
+ # Copy the requirements file into the container
8
+ COPY . .
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Make port 7860 available to the world outside this container
14
+ EXPOSE 7860
15
+
16
+ ENV GRADIO_SERVER_NAME="0.0.0.0"
17
+
18
+ # Run app.py when the container launches
19
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import os
4
+ import re
5
+ import string
6
+
7
+ # Load the models and vectorizers
8
+ def load_model_and_vectorizer(path, model_filename='model.pkl', vectorizer_filename='vectorizer.pkl'):
9
+ model = joblib.load(os.path.join(path, model_filename))
10
+ vectorizer = joblib.load(os.path.join(path, vectorizer_filename))
11
+ return model, vectorizer
12
+
13
+ # Text preprocessing
14
+ def preprocess_text(text):
15
+ text = text.lower()
16
+ text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
17
+ text = text.translate(str.maketrans('', '', string.punctuation))
18
+ text = text.strip()
19
+ return text
20
+
21
+ # Load all models at startup
22
+ models = {
23
+ "Linear Regression": load_model_and_vectorizer(path=os.path.join('models', 'lr')),
24
+ "MultinomialNB": load_model_and_vectorizer(path=os.path.join('models', 'mnb')),
25
+ "SVM": load_model_and_vectorizer(path=os.path.join('models', 'svm')),
26
+ "Random Forest": load_model_and_vectorizer(path=os.path.join('models', 'rf'))
27
+ }
28
+
29
+ def predict_sentiment(message, model_name="MultinomialNB"):
30
+ if not message.strip():
31
+ return None
32
+
33
+ model, vectorizer = models[model_name]
34
+ preprocessed = preprocess_text(message)
35
+ vectorized = vectorizer.transform([preprocessed])
36
+ prediction = model.predict(vectorized)[0]
37
+ return prediction
38
+
39
+ def get_bot_response(message, chat_history, model_choice):
40
+ message = message["text"]
41
+ if not message.strip():
42
+ return "", chat_history
43
+
44
+ # Get sentiment prediction
45
+ sentiment = predict_sentiment(message, model_choice)
46
+
47
+ if sentiment is None:
48
+ bot_response = "Please share your thoughts about a game!"
49
+ chat_history.append((message, bot_response))
50
+ return "", chat_history
51
+
52
+ # Generate response based on sentiment
53
+ if sentiment == 1:
54
+ responses = [
55
+ f"This is a Positive review 😃"
56
+ ]
57
+ bot_response = responses[len(chat_history) % len(responses)]
58
+ else:
59
+ responses = [
60
+ f"This is a Negative review 🤬"
61
+ ]
62
+ bot_response = responses[len(chat_history) % len(responses)]
63
+
64
+ chat_history.append((message, bot_response))
65
+ return "", chat_history
66
+
67
+ # Create the Gradio interface
68
+ with gr.Blocks(theme=gr.themes.Default(), title="Gaming Sentiment Chatbot", css=".upload-button {display: none;} .centered-md {text-align: center}") as demo:
69
+ gr.Markdown("# 🎮 Steam Review Sentiment Analysis", elem_classes="centered-md")
70
+ gr.Markdown("✨ Enter a Steam review to analyze its sentiment. For more information, see the dataset used at [Kaggle Steam Reviews](https://www.kaggle.com/datasets/filipkin/steam-reviews)", elem_classes="centered-md")
71
+ gr.Markdown("[![GitHub](https://img.shields.io/badge/GitHub-Repository-blue?logo=github)](https://github.com/alyzbane/gradio-sentimental-analysis-ml)")
72
+
73
+ chatbot = gr.Chatbot(
74
+ label="History",
75
+ height=400
76
+ )
77
+
78
+ with gr.Row():
79
+ message = gr.MultimodalTextbox(
80
+ interactive=True,
81
+ placeholder="Enter message...",
82
+ show_label=False,
83
+ )
84
+
85
+ with gr.Row():
86
+ model_choice = gr.Dropdown(
87
+ choices=list(models.keys()),
88
+ value="MultinomialNB",
89
+ label="Select Model for Analysis",
90
+ )
91
+
92
+ # Example messages
93
+ gr.Markdown("## Example Messages")
94
+ examples = gr.Examples(
95
+ examples=[
96
+ "This game is absolutely fantastic! The graphics and gameplay are incredible!",
97
+ "I can't believe how buggy this game is. Constant crashes and poor optimization.",
98
+ "Decent game but nothing special. Might be worth it on sale.",
99
+ "Best game I've played this year! The story is amazing!",
100
+ "1/10"
101
+ ],
102
+ inputs=message,
103
+ label="Example Messages"
104
+ )
105
+
106
+ # Also allow Enter key to submit
107
+ message.submit(
108
+ fn=get_bot_response,
109
+ inputs=[message, chatbot, model_choice],
110
+ outputs=[message, chatbot]
111
+ )
112
+
113
+ if __name__ == "__main__":
114
+ demo.launch(debug=False)
lr/model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7573e0b54dc8bc8988d37a1713a380279dadae6bd8fe8ce46091be938c381f71
3
+ size 40863
lr/vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:378afbbef96e36dbc70fba3045b9fbe0fc148cae922d84e741af18358cd165e9
3
+ size 181651
mnb/model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7a84283035fa24df67bb627a4d57481a37ce8d0b06ceafade7e31a0fc0e1fe24
3
+ size 160791
mnb/vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:378afbbef96e36dbc70fba3045b9fbe0fc148cae922d84e741af18358cd165e9
3
+ size 181651
rf/model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9bc24cf969f575cf12e28f44a9a168c6c231ae5e76a6bbb04d6a2dbedf44a07a
3
+ size 268973145
rf/vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:378afbbef96e36dbc70fba3045b9fbe0fc148cae922d84e741af18358cd165e9
3
+ size 181651
svm/model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bccae25393cd2dd073df6210ce21a50a315ca3d61ac73889e0ccaea1be366c1e
3
+ size 7017067
svm/vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:378afbbef96e36dbc70fba3045b9fbe0fc148cae922d84e741af18358cd165e9
3
+ size 181651