Ujeshhh commited on
Commit
bbf58b2
·
verified ·
1 Parent(s): 0b195fa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ from sklearn.model_selection import train_test_split
6
+ from tensorflow.keras.preprocessing.text import Tokenizer
7
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
8
+ from tensorflow.keras.models import load_model
9
+ import nltk
10
+ import gradio as gr
11
+ import os
12
+ import logging
13
+ import tensorflow as tf
14
+
15
+ # Suppress TensorFlow warnings and set logging level
16
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Suppress TensorFlow info/warning logs
17
+ logging.getLogger('tensorflow').setLevel(logging.ERROR)
18
+
19
+ # Download NLTK data
20
+ nltk.download('punkt', quiet=True)
21
+
22
+ # Load the dataset
23
+ def load_data():
24
+ file_path = "zomato_data.csv"
25
+ try:
26
+ zomato_data = pd.read_csv(file_path)
27
+ except FileNotFoundError:
28
+ print("Warning: zomato_data.csv not found. Using dummy dataset.")
29
+ zomato_data = pd.DataFrame({
30
+ 'name': ['Sample Restaurant'],
31
+ 'online_order': ['Yes'],
32
+ 'book_table': ['No'],
33
+ 'rate': [4.0],
34
+ 'approx_cost': [1000.0],
35
+ 'listed_in(type)': ['Dining']
36
+ })
37
+ return zomato_data
38
+
39
+ # Preprocess data
40
+ def preprocess_data(zomato_data):
41
+ zomato_data.rename(columns={'approx_cost(for two people)': 'approx_cost'}, inplace=True)
42
+ zomato_data['approx_cost'] = zomato_data['approx_cost'].astype(str).str.replace(',', '', regex=True).astype(float)
43
+ zomato_data['rate'] = zomato_data['rate'].astype(str).str.split('/').str[0].replace(['NEW', '-', 'nan'], None).astype(float)
44
+ zomato_data.fillna("Unknown", inplace=True)
45
+ zomato_data['search_text'] = (
46
+ "Name: " + zomato_data['name'] + " | " +
47
+ "Online Order: " + zomato_data['online_order'] + " | " +
48
+ "Book Table: " + zomato_data['book_table'] + " | " +
49
+ "Rate: " + zomato_data['rate'].astype(str) + " | " +
50
+ "Cost for Two: ₹" + zomato_data['approx_cost'].astype(str) + " | " +
51
+ "Cuisine Type: " + zomato_data['listed_in(type)']
52
+ )
53
+ return zomato_data
54
+
55
+ # Generate Q&A pairs
56
+ def generate_qa_pairs(zomato_data):
57
+ qa_pairs = []
58
+ for _, row in zomato_data.iterrows():
59
+ qa_pairs.append((f"Can I order online at {row['name']}?",
60
+ f"{'Yes' if row['online_order'] == 'Yes' else 'No'}, online ordering is {'available' if row['online_order'] == 'Yes' else 'not available'} at {row['name']}."))
61
+ qa_pairs.append((f"Can I book a table at {row['name']}?",
62
+ f"{'Yes' if row['book_table'] == 'Yes' else 'No'}, table booking is {'available' if row['book_table'] == 'Yes' else 'not available'} at {row['name']}."))
63
+ qa_pairs.append((f"What is the rating of {row['name']}?", f"The rating of {row['name']} is {row['rate']}/5."))
64
+ qa_pairs.append((f"What is the cost for two people at {row['name']}?",
65
+ f"The approximate cost for two people at {row['name']} is ₹{row['approx_cost']}."))
66
+ qa_pairs.append((f"What type of cuisine does {row['name']} serve?",
67
+ f"{row['name']} is listed in {row['listed_in(type)']}."))
68
+ return qa_pairs
69
+
70
+ # Initialize vectorizer
71
+ def init_vectorizer(zomato_data):
72
+ vectorizer = TfidfVectorizer()
73
+ vectorized_data = vectorizer.fit_transform(zomato_data['search_text'])
74
+ return vectorizer, vectorized_data
75
+
76
+ # Load tokenizer and model
77
+ def load_model_and_tokenizer(zomato_data):
78
+ model = None
79
+ tokenizer = None
80
+ try:
81
+ model = load_model("chatbot_model.h5")
82
+ tokenizer = Tokenizer()
83
+ qa_pairs = generate_qa_pairs(zomato_data)
84
+ questions, answers = zip(*qa_pairs)
85
+ tokenizer.fit_on_texts(questions + answers)
86
+ print("Successfully loaded model and initialized tokenizer.")
87
+ except FileNotFoundError:
88
+ print("Warning: chatbot_model.h5 not found. Falling back to TF-IDF-based responses.")
89
+ except Exception as e:
90
+ print(f"Error loading model: {e}. Falling back to TF-IDF-based responses.")
91
+ return model, tokenizer
92
+
93
+ # Chatbot query using TF-IDF
94
+ def chatbot_query(query, vectorizer, vectorized_data, zomato_data):
95
+ query_vector = vectorizer.transform([query])
96
+ similarities = cosine_similarity(query_vector, vectorized_data)
97
+ most_similar_idx = np.argmax(similarities)
98
+ return zomato_data['search_text'].iloc[most_similar_idx]
99
+
100
+ # Chatbot response using LSTM model
101
+ def chatbot_response(input_text, model, tokenizer, vectorizer, vectorized_data, zomato_data, max_len=20):
102
+ if model and tokenizer:
103
+ input_seq = tokenizer.texts_to_sequences([input_text])
104
+ input_seq = pad_sequences(input_seq, maxlen=max_len, padding='post')
105
+ try:
106
+ pred = model.predict([input_seq, input_seq], verbose=0)
107
+ pred_idx = np.argmax(pred[0], axis=1)
108
+ response = " ".join([tokenizer.index_word.get(idx, "") for idx in pred_idx if idx > 0])
109
+ if response.strip():
110
+ return response
111
+ except Exception as e:
112
+ print(f"Model prediction failed: {e}")
113
+ # Fallback to TF-IDF
114
+ return chatbot_query(input_text, vectorizer, vectorized_data, zomato_data)
115
+
116
+ # Initialize everything
117
+ zomato_data = load_data()
118
+ zomato_data = preprocess_data(zomato_data)
119
+ vectorizer, vectorized_data = init_vectorizer(zomato_data)
120
+ model, tokenizer = load_model_and_tokenizer(zomato_data)
121
+
122
+ # Gradio interface
123
+ def gradio_chatbot(user_query):
124
+ if not user_query.strip():
125
+ return "Please enter a valid query."
126
+ response = chatbot_response(user_query, model, tokenizer, vectorizer, vectorized_data, zomato_data)
127
+ return response
128
+
129
+ # Create Gradio interface
130
+ iface = gr.Interface(
131
+ fn=gradio_chatbot,
132
+ inputs=gr.Textbox(lines=2, placeholder="Ask about a restaurant (e.g., 'Can I order online at Cafe XYZ?')"),
133
+ outputs="text",
134
+ title="Zomato Restaurant Chatbot",
135
+ description="Ask questions about restaurants, such as online ordering, table booking, ratings, costs, or cuisine types."
136
+ )
137
+
138
+ # Launch the app
139
+ if __name__ == "__main__":
140
+ iface.launch()