Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,8 +1,5 @@
|
|
1 |
import os
|
2 |
-
import shutil
|
3 |
from flask import Flask, render_template, request, jsonify
|
4 |
-
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
5 |
-
from sentence_transformers import SentenceTransformer # Fallback for embeddings
|
6 |
from deep_translator import GoogleTranslator
|
7 |
import google.generativeai as genai
|
8 |
|
@@ -15,83 +12,40 @@ if not GOOGLE_API_KEY:
|
|
15 |
genai.configure(api_key=GOOGLE_API_KEY)
|
16 |
gemini_model = genai.GenerativeModel('gemini-flash-1.0')
|
17 |
|
18 |
-
#
|
19 |
-
class CustomEmbedding:
|
20 |
-
def __init__(self, model_name):
|
21 |
-
self.model = SentenceTransformer(model_name)
|
22 |
-
|
23 |
-
def get_text_embedding(self, text):
|
24 |
-
return self.model.encode(text).tolist()
|
25 |
-
|
26 |
-
# Configure Llama index settings with Custom Embedding
|
27 |
-
Settings.embed_model = CustomEmbedding("sentence-transformers/paraphrase-multilingual-mpnet-base-v2")
|
28 |
-
|
29 |
-
PERSIST_DIR = "db"
|
30 |
PDF_DIRECTORY = 'data'
|
31 |
|
32 |
-
#
|
33 |
-
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
34 |
-
os.makedirs(PERSIST_DIR, exist_ok=True)
|
35 |
chat_history = []
|
36 |
-
current_chat_history = []
|
37 |
-
|
38 |
-
def data_ingestion_from_directory():
|
39 |
-
# Clear previous data by removing the persist directory
|
40 |
-
if os.path.exists(PERSIST_DIR):
|
41 |
-
shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# Create a new index with the new documents
|
50 |
-
index = VectorStoreIndex.from_documents(new_documents)
|
51 |
-
|
52 |
-
# Persist the new index
|
53 |
-
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
54 |
|
55 |
def handle_query(query):
|
56 |
-
|
57 |
-
|
58 |
-
"user",
|
59 |
-
"""
|
60 |
-
You are the Hotel voice chatbot and your name is hotel helper. Your goal is to provide accurate, professional, and helpful answers to user queries based on the hotel's data. Always ensure your responses are clear and concise. Give response within 10-15 words only. You need to give an answer in the same language used by the user.
|
61 |
-
{context_str}
|
62 |
-
Question:
|
63 |
-
{query_str}
|
64 |
-
"""
|
65 |
-
)
|
66 |
-
]
|
67 |
-
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
68 |
-
|
69 |
-
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
70 |
-
index = load_index_from_storage(storage_context)
|
71 |
-
context_str = ""
|
72 |
-
for past_query, response in reversed(current_chat_history):
|
73 |
-
if past_query.strip():
|
74 |
-
context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
75 |
-
|
76 |
-
query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
77 |
-
print(query)
|
78 |
-
|
79 |
-
# Use Gemini for generating the response
|
80 |
prompt = f"""
|
81 |
-
Context: {
|
82 |
Question: {query}
|
|
|
83 |
"""
|
|
|
84 |
gemini_response = gemini_model.generate_content(prompt)
|
85 |
-
response = gemini_response.text
|
86 |
-
|
87 |
-
current_chat_history.append((query, response))
|
88 |
return response
|
89 |
|
90 |
app = Flask(__name__)
|
91 |
|
92 |
-
# Data ingestion
|
93 |
-
data_ingestion_from_directory()
|
94 |
-
|
95 |
# Generate Response
|
96 |
def generate_response(query, language):
|
97 |
try:
|
|
|
1 |
import os
|
|
|
2 |
from flask import Flask, render_template, request, jsonify
|
|
|
|
|
3 |
from deep_translator import GoogleTranslator
|
4 |
import google.generativeai as genai
|
5 |
|
|
|
12 |
genai.configure(api_key=GOOGLE_API_KEY)
|
13 |
gemini_model = genai.GenerativeModel('gemini-flash-1.0')
|
14 |
|
15 |
+
# Directory for storing PDFs (optional if you want to load raw text data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
PDF_DIRECTORY = 'data'
|
17 |
|
18 |
+
# Chat history
|
|
|
|
|
19 |
chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
def load_data_from_directory():
|
22 |
+
"""Load raw text data from the directory."""
|
23 |
+
data = ""
|
24 |
+
for filename in os.listdir(PDF_DIRECTORY):
|
25 |
+
file_path = os.path.join(PDF_DIRECTORY, filename)
|
26 |
+
if os.path.isfile(file_path):
|
27 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
28 |
+
data += file.read() + "\n"
|
29 |
+
return data
|
30 |
|
31 |
+
# Load hotel data (context) from the directory
|
32 |
+
hotel_data = load_data_from_directory()
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def handle_query(query):
|
35 |
+
"""Handle user queries and generate responses using Gemini."""
|
36 |
+
# Prepare the prompt with context and query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
prompt = f"""
|
38 |
+
Context: {hotel_data}
|
39 |
Question: {query}
|
40 |
+
Answer the question based on the context above. If the answer is not found, say "I'm sorry, I couldn't find an answer."
|
41 |
"""
|
42 |
+
# Generate response using Gemini
|
43 |
gemini_response = gemini_model.generate_content(prompt)
|
44 |
+
response = gemini_response.text.strip()
|
|
|
|
|
45 |
return response
|
46 |
|
47 |
app = Flask(__name__)
|
48 |
|
|
|
|
|
|
|
49 |
# Generate Response
|
50 |
def generate_response(query, language):
|
51 |
try:
|