Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
16 |
-
|
17 |
-
logging.getLogger('tensorflow').setLevel(logging.ERROR)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
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 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
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 |
-
#
|
56 |
-
def
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
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 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
75 |
|
76 |
-
# Load tokenizer and model
|
77 |
-
def load_model_and_tokenizer(zomato_data):
|
78 |
-
model = None
|
79 |
-
tokenizer = None
|
80 |
try:
|
81 |
-
|
82 |
-
|
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 |
-
|
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., '
|
133 |
outputs="text",
|
134 |
-
title="
|
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 |
|