judebebo32 commited on
Commit
fd2040c
1 Parent(s): d906e7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -25
app.py CHANGED
@@ -4,19 +4,14 @@ from nltk.stem import PorterStemmer
4
  from nltk.tokenize import word_tokenize
5
  from sklearn.feature_extraction.text import TfidfVectorizer
6
  from sklearn.metrics.pairwise import cosine_similarity
7
- from flask import Flask, request, jsonify
8
- import os
9
-
10
- # Initialize the Flask app
11
- app = Flask(__name__)
12
 
13
  # Download necessary NLTK data
14
  nltk.download('punkt')
15
  nltk.download('stopwords')
16
 
17
  # Load customer inquiries dataset
18
- file_path = os.path.join(os.path.dirname(__file__), 'my_text_file.txt')
19
- with open(file_path, 'r') as f:
20
  data = f.readlines()
21
 
22
  # Preprocess data
@@ -34,26 +29,21 @@ tfidf_matrix = vectorizer.fit_transform(data)
34
 
35
  # Define chatbot logic
36
  def chatbot_response(user_input):
37
- preprocessed_input = preprocess_text(user_input)
38
  input_vector = vectorizer.transform([user_input])
39
  cosine_similarities = cosine_similarity(input_vector, tfidf_matrix)
40
  most_similar_index = cosine_similarities.argmax()
41
  return data[most_similar_index].strip()
42
 
43
- # Define routes
44
- @app.route('/')
45
- def home():
46
- return "Welcome to the Chatbot! Send a POST request to /chat with your message."
47
-
48
- @app.route('/chat', methods=['POST'])
49
- def chat():
50
- user_input = request.json.get('message')
51
- if user_input:
52
- response = chatbot_response(user_input)
53
- return jsonify({'response': response})
54
- else:
55
- return jsonify({'error': 'No message provided'}), 400
56
-
57
- # Main entry
58
- if __name__ == '__main__':
59
- app.run(host='0.0.0.0', port=8080)
 
4
  from nltk.tokenize import word_tokenize
5
  from sklearn.feature_extraction.text import TfidfVectorizer
6
  from sklearn.metrics.pairwise import cosine_similarity
7
+ import gradio as gr
 
 
 
 
8
 
9
  # Download necessary NLTK data
10
  nltk.download('punkt')
11
  nltk.download('stopwords')
12
 
13
  # Load customer inquiries dataset
14
+ with open('my_text_file.txt', 'r') as f:
 
15
  data = f.readlines()
16
 
17
  # Preprocess data
 
29
 
30
  # Define chatbot logic
31
  def chatbot_response(user_input):
 
32
  input_vector = vectorizer.transform([user_input])
33
  cosine_similarities = cosine_similarity(input_vector, tfidf_matrix)
34
  most_similar_index = cosine_similarities.argmax()
35
  return data[most_similar_index].strip()
36
 
37
+ # Create Gradio interface
38
+ def chatbot_interface(user_input):
39
+ response = chatbot_response(user_input)
40
+ return response
41
+
42
+ iface = gr.Interface(fn=chatbot_interface,
43
+ inputs="text",
44
+ outputs="text",
45
+ title="FAQ Chatbot",
46
+ description="Enter a question to get a response from the chatbot based on the preloaded FAQ data.")
47
+
48
+ if __name__ == "__main__":
49
+ iface.launch()