Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
import nltk
|
| 5 |
import numpy as np
|
|
@@ -41,19 +41,20 @@ try:
|
|
| 41 |
except FileNotFoundError:
|
| 42 |
raise FileNotFoundError("Error: 'data.pickle' file not found. Ensure it exists and matches the model.")
|
| 43 |
|
| 44 |
-
# Build the model structure
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
net = tflearn.regression(net)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
model =
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Function to process user input into a bag-of-words format
|
| 59 |
def bag_of_words(s, words):
|
|
@@ -73,7 +74,7 @@ def chat(message, history):
|
|
| 73 |
|
| 74 |
try:
|
| 75 |
# Predict the tag
|
| 76 |
-
results = model.predict([bag_of_words(message, words)])
|
| 77 |
results_index = np.argmax(results)
|
| 78 |
tag = labels[results_index]
|
| 79 |
|
|
@@ -161,7 +162,7 @@ def scrape_website_for_contact_info(website):
|
|
| 161 |
response = requests.get(website, timeout=5)
|
| 162 |
soup = BeautifulSoup(response.content, 'html.parser')
|
| 163 |
|
| 164 |
-
phone_match = re.search(r'
|
| 165 |
if phone_match:
|
| 166 |
phone_number = phone_match.group()
|
| 167 |
|
|
@@ -242,7 +243,7 @@ def main():
|
|
| 242 |
# Gradio UI setup
|
| 243 |
with gr.Blocks() as demo:
|
| 244 |
# Load pre-trained model and tokenizer
|
| 245 |
-
@gr.
|
| 246 |
def load_model():
|
| 247 |
tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 248 |
model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
|
@@ -277,7 +278,7 @@ with gr.Blocks() as demo:
|
|
| 277 |
|
| 278 |
try:
|
| 279 |
# Predict the tag
|
| 280 |
-
results = model.predict([bag_of_words(message, words)])
|
| 281 |
results_index = np.argmax(results)
|
| 282 |
tag = labels[results_index]
|
| 283 |
|
|
@@ -314,4 +315,4 @@ with gr.Blocks() as demo:
|
|
| 314 |
fetch_button.click(fetch_data, inputs=None, outputs=data_output)
|
| 315 |
|
| 316 |
# Launch Gradio interface
|
| 317 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
import torch
|
| 4 |
import nltk
|
| 5 |
import numpy as np
|
|
|
|
| 41 |
except FileNotFoundError:
|
| 42 |
raise FileNotFoundError("Error: 'data.pickle' file not found. Ensure it exists and matches the model.")
|
| 43 |
|
| 44 |
+
# Build the model structure using Keras
|
| 45 |
+
from tensorflow.keras.models import Sequential
|
| 46 |
+
from tensorflow.keras.layers import Dense
|
| 47 |
+
from tensorflow.keras.optimizers import Adam
|
| 48 |
+
from tensorflow.keras.losses import CategoricalCrossentropy
|
|
|
|
| 49 |
|
| 50 |
+
model = Sequential()
|
| 51 |
+
model.add(Dense(8, input_shape=(len(training[0]),), activation='relu'))
|
| 52 |
+
model.add(Dense(8, activation='relu'))
|
| 53 |
+
model.add(Dense(len(output[0]), activation='softmax'))
|
| 54 |
+
model.compile(optimizer=Adam(), loss=CategoricalCrossentropy(), metrics=['accuracy'])
|
| 55 |
+
|
| 56 |
+
# Load the trained model weights
|
| 57 |
+
model.load_weights("MentalHealthChatBotmodel.tflearn")
|
| 58 |
|
| 59 |
# Function to process user input into a bag-of-words format
|
| 60 |
def bag_of_words(s, words):
|
|
|
|
| 74 |
|
| 75 |
try:
|
| 76 |
# Predict the tag
|
| 77 |
+
results = model.predict(np.array([bag_of_words(message, words)]))
|
| 78 |
results_index = np.argmax(results)
|
| 79 |
tag = labels[results_index]
|
| 80 |
|
|
|
|
| 162 |
response = requests.get(website, timeout=5)
|
| 163 |
soup = BeautifulSoup(response.content, 'html.parser')
|
| 164 |
|
| 165 |
+
phone_match = re.search(r'$$?\+?[0-9]*$$?[0-9_\- $$$$]*', soup.get_text())
|
| 166 |
if phone_match:
|
| 167 |
phone_number = phone_match.group()
|
| 168 |
|
|
|
|
| 243 |
# Gradio UI setup
|
| 244 |
with gr.Blocks() as demo:
|
| 245 |
# Load pre-trained model and tokenizer
|
| 246 |
+
@gr.cache
|
| 247 |
def load_model():
|
| 248 |
tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
| 249 |
model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
|
|
|
|
| 278 |
|
| 279 |
try:
|
| 280 |
# Predict the tag
|
| 281 |
+
results = model.predict(np.array([bag_of_words(message, words)]))
|
| 282 |
results_index = np.argmax(results)
|
| 283 |
tag = labels[results_index]
|
| 284 |
|
|
|
|
| 315 |
fetch_button.click(fetch_data, inputs=None, outputs=data_output)
|
| 316 |
|
| 317 |
# Launch Gradio interface
|
| 318 |
+
demo.launch()
|