DreamStream-1 commited on
Commit
0bbb821
·
verified ·
1 Parent(s): 53595f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
3
  import torch
4
  import nltk
5
  import numpy as np
@@ -41,20 +41,21 @@ 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 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,7 +75,7 @@ def chat(message, history):
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,7 +163,7 @@ def scrape_website_for_contact_info(website):
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,7 +244,7 @@ def main():
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,7 +279,7 @@ with gr.Blocks() as demo:
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
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
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
45
+ net = tflearn.input_data(shape=[None, len(training[0])])
46
+ net = tflearn.fully_connected(net, 8)
47
+ net = tflearn.fully_connected(net, 8)
48
+ net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
49
+ net = tflearn.regression(net)
50
 
51
+ # Create a new model instance
52
+ model = tflearn.DNN(net)
 
 
 
53
 
54
+ # Create a checkpoint object
55
+ checkpoint = tf.train.Checkpoint(model=model)
56
+
57
+ # Load the model weights
58
+ checkpoint.restore("path/to/save/MentalHealthChatBotmodel")
59
 
60
  # Function to process user input into a bag-of-words format
61
  def bag_of_words(s, words):
 
75
 
76
  try:
77
  # Predict the tag
78
+ results = model.predict([bag_of_words(message, words)])
79
  results_index = np.argmax(results)
80
  tag = labels[results_index]
81
 
 
163
  response = requests.get(website, timeout=5)
164
  soup = BeautifulSoup(response.content, 'html.parser')
165
 
166
+ phone_match = re.search(r'\(?\+?[0-9]*\)?[0-9_\- \(\)]*', soup.get_text())
167
  if phone_match:
168
  phone_number = phone_match.group()
169
 
 
244
  # Gradio UI setup
245
  with gr.Blocks() as demo:
246
  # Load pre-trained model and tokenizer
247
+ @gr.cache_resource
248
  def load_model():
249
  tokenizer = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
250
  model = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
 
279
 
280
  try:
281
  # Predict the tag
282
+ results = model.predict([bag_of_words(message, words)])
283
  results_index = np.argmax(results)
284
  tag = labels[results_index]
285