Ujeshhh commited on
Commit
bb45a6c
·
verified ·
1 Parent(s): 4879b8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -117
app.py CHANGED
@@ -1,137 +1,54 @@
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
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
+ import google.generativeai as genai
4
  import logging
 
5
 
6
+ # Suppress unnecessary logs
7
+ logging.getLogger().setLevel(logging.ERROR)
 
8
 
9
+ # Load the Gemini API key from Hugging Face secrets
10
+ GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
11
 
12
+ # Configure Gemini API
13
+ if GEMINI_API_KEY:
14
+ genai.configure(api_key=GEMINI_API_KEY)
15
+ else:
16
+ print("Error: Gemini API key not found. Please set GEMINI_API_KEY in Hugging Face secrets.")
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Initialize Gemini model
19
+ model = None
20
+ try:
21
+ model = genai.GenerativeModel('gemini-1.5-flash')
22
+ except Exception as e:
23
+ print(f"Error initializing Gemini model: {e}")
 
 
 
 
 
 
 
 
 
24
 
25
+ # Chatbot function using Gemini
26
+ def gradio_chatbot(user_query):
27
+ if not user_query.strip():
28
+ return "Please enter a valid query."
29
+
30
+ if not model:
31
+ return "Error: Gemini model not initialized. Check API key and try again."
 
 
 
 
 
 
 
32
 
33
+ # Prompt to restrict Gemini to restaurant-related responses
34
+ prompt = (
35
+ "You are a restaurant information assistant. You can only respond to questions about restaurants, their features (e.g., online ordering, table booking), ratings, costs, cuisine types, or other restaurant-related details. "
36
+ "Do not answer questions unrelated to restaurants. If the query is not restaurant-related, respond with: 'Sorry, I can only answer questions about restaurants.' "
37
+ f"User query: {user_query}\nAnswer:"
38
+ )
39
 
 
 
 
 
40
  try:
41
+ response = model.generate_content(prompt)
42
+ return response.text.strip()
 
 
 
 
 
 
43
  except Exception as e:
44
+ return f"Error generating response: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  # Create Gradio interface
47
  iface = gr.Interface(
48
  fn=gradio_chatbot,
49
+ inputs=gr.Textbox(lines=2, placeholder="Ask about a restaurant (e.g., 'What are the features of Cafe Delight?')"),
50
  outputs="text",
51
+ title="Restaurant Chatbot",
52
  description="Ask questions about restaurants, such as online ordering, table booking, ratings, costs, or cuisine types."
53
  )
54