Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pdfplumber
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from llama_index.core import StorageContext, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
| 5 |
+
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
| 6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
+
from llama_index import VectorStoreIndex
|
| 8 |
+
|
| 9 |
+
# Configure Llama index settings
|
| 10 |
+
Settings.llm = HuggingFaceInferenceAPI(
|
| 11 |
+
model_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 12 |
+
tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 13 |
+
context_window=3000,
|
| 14 |
+
token=os.getenv("HF_TOKEN"),
|
| 15 |
+
max_new_tokens=512,
|
| 16 |
+
generate_kwargs={"temperature": 0.1},
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
| 20 |
+
model_name="BAAI/bge-small-en-v1.5"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Define directories for persistent storage and PDF data
|
| 24 |
+
PERSIST_DIR = "db"
|
| 25 |
+
PDF_DIRECTORY = 'data' # Directory containing PDFs
|
| 26 |
+
|
| 27 |
+
# Ensure directories exist
|
| 28 |
+
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
| 29 |
+
os.makedirs(PERSIST_DIR, exist_ok=True)
|
| 30 |
+
|
| 31 |
+
# Variable to store current chat conversation
|
| 32 |
+
current_chat_history = []
|
| 33 |
+
|
| 34 |
+
def data_ingestion_from_directory():
|
| 35 |
+
# Use SimpleDirectoryReader on the directory containing the PDF files
|
| 36 |
+
documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
|
| 37 |
+
storage_context = StorageContext.from_defaults()
|
| 38 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 39 |
+
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
| 40 |
+
|
| 41 |
+
# Load the PDF documents into the index
|
| 42 |
+
data_ingestion_from_directory()
|
| 43 |
+
|
| 44 |
+
# Function to handle user queries
|
| 45 |
+
def handle_query(query):
|
| 46 |
+
global current_chat_history
|
| 47 |
+
chat_text_qa_msgs = [
|
| 48 |
+
(
|
| 49 |
+
"user",
|
| 50 |
+
"""
|
| 51 |
+
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.
|
| 52 |
+
{context_str}
|
| 53 |
+
Question:
|
| 54 |
+
{query_str}
|
| 55 |
+
"""
|
| 56 |
+
)
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
| 60 |
+
|
| 61 |
+
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
| 62 |
+
index = VectorStoreIndex.load_from_storage(storage_context)
|
| 63 |
+
|
| 64 |
+
context_str = "\n".join([f"User asked: '{past_query}'\nBot answered: '{response}'"
|
| 65 |
+
for past_query, response in reversed(current_chat_history) if past_query.strip()])
|
| 66 |
+
|
| 67 |
+
query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
| 68 |
+
print(f"Query: {query}")
|
| 69 |
+
|
| 70 |
+
answer = query_engine.query(query)
|
| 71 |
+
|
| 72 |
+
response = getattr(answer, 'response', answer.get('response', "Sorry, I couldn't find an answer."))
|
| 73 |
+
current_chat_history.append((query, response))
|
| 74 |
+
return response
|
| 75 |
+
|
| 76 |
+
# Create Gradio interface
|
| 77 |
+
def gradio_chatbot(user_input):
|
| 78 |
+
response = handle_query(user_input)
|
| 79 |
+
return response
|
| 80 |
+
|
| 81 |
+
# Set up Gradio app interface
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=gradio_chatbot,
|
| 84 |
+
inputs=gr.inputs.Textbox(label="Ask a question about the hotel"),
|
| 85 |
+
outputs="text",
|
| 86 |
+
title="Hotel Chatbot",
|
| 87 |
+
description="Ask any questions related to the hotel."
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Launch the Gradio app
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
iface.launch()
|