Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Configure Llama index settings
|
| 10 |
Settings.llm = HuggingFaceInferenceAPI(
|
|
@@ -15,35 +19,33 @@ Settings.llm = HuggingFaceInferenceAPI(
|
|
| 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'
|
| 26 |
|
| 27 |
# Ensure directories exist
|
| 28 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
| 29 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
| 30 |
|
| 31 |
-
#
|
| 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 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
chat_text_qa_msgs = [
|
| 48 |
(
|
| 49 |
"user",
|
|
@@ -55,36 +57,49 @@ def handle_query(query):
|
|
| 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 =
|
| 63 |
-
|
| 64 |
-
context_str = "
|
| 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 |
-
#
|
| 77 |
-
def
|
| 78 |
-
response = handle_query(
|
| 79 |
return response
|
| 80 |
|
| 81 |
-
#
|
| 82 |
iface = gr.Interface(
|
| 83 |
-
fn=
|
| 84 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 85 |
outputs="text",
|
| 86 |
title="Hotel Chatbot",
|
| 87 |
-
description="Ask
|
| 88 |
)
|
| 89 |
|
| 90 |
# Launch the Gradio app
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
import datetime
|
| 4 |
import gradio as gr
|
| 5 |
+
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
| 6 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
| 7 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 8 |
+
from deep_translator import GoogleTranslator
|
| 9 |
+
|
| 10 |
+
# Initialize Hugging Face token
|
| 11 |
+
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
| 12 |
|
| 13 |
# Configure Llama index settings
|
| 14 |
Settings.llm = HuggingFaceInferenceAPI(
|
|
|
|
| 19 |
max_new_tokens=512,
|
| 20 |
generate_kwargs={"temperature": 0.1},
|
| 21 |
)
|
|
|
|
| 22 |
Settings.embed_model = HuggingFaceEmbedding(
|
| 23 |
model_name="BAAI/bge-small-en-v1.5"
|
| 24 |
)
|
| 25 |
|
|
|
|
| 26 |
PERSIST_DIR = "db"
|
| 27 |
+
PDF_DIRECTORY = 'data'
|
| 28 |
|
| 29 |
# Ensure directories exist
|
| 30 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
| 31 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
| 32 |
|
| 33 |
+
# Load and initialize data
|
|
|
|
|
|
|
| 34 |
def data_ingestion_from_directory():
|
|
|
|
| 35 |
documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
|
| 36 |
storage_context = StorageContext.from_defaults()
|
| 37 |
index = VectorStoreIndex.from_documents(documents)
|
| 38 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
| 39 |
|
| 40 |
+
def initialize():
|
| 41 |
+
start_time = time.time()
|
| 42 |
+
data_ingestion_from_directory() # Process PDF ingestion at startup
|
| 43 |
+
print(f"Data ingestion time: {time.time() - start_time} seconds")
|
| 44 |
|
| 45 |
+
initialize() # Run initialization tasks
|
| 46 |
+
|
| 47 |
+
# Handle user queries
|
| 48 |
+
def handle_query(query, language):
|
| 49 |
chat_text_qa_msgs = [
|
| 50 |
(
|
| 51 |
"user",
|
|
|
|
| 57 |
"""
|
| 58 |
)
|
| 59 |
]
|
|
|
|
| 60 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
| 61 |
|
| 62 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
| 63 |
+
index = load_index_from_storage(storage_context)
|
| 64 |
+
|
| 65 |
+
context_str = ""
|
|
|
|
|
|
|
| 66 |
query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
|
|
|
| 67 |
|
| 68 |
answer = query_engine.query(query)
|
| 69 |
+
|
| 70 |
+
if hasattr(answer, 'response'):
|
| 71 |
+
response = answer.response
|
| 72 |
+
elif isinstance(answer, dict) and 'response' in answer:
|
| 73 |
+
response = answer['response']
|
| 74 |
+
else:
|
| 75 |
+
response = "Sorry, I couldn't find an answer."
|
| 76 |
+
|
| 77 |
+
# Translate response if needed
|
| 78 |
+
if language:
|
| 79 |
+
try:
|
| 80 |
+
translator = GoogleTranslator(target=language.split('-')[0]) # Translate to the specified language
|
| 81 |
+
response = translator.translate(response)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Translation error: {e}")
|
| 84 |
+
response = "Sorry, I couldn't translate the response."
|
| 85 |
|
|
|
|
|
|
|
| 86 |
return response
|
| 87 |
|
| 88 |
+
# Gradio interface
|
| 89 |
+
def chatbot_interface(message, language):
|
| 90 |
+
response = handle_query(message, language)
|
| 91 |
return response
|
| 92 |
|
| 93 |
+
# Create Gradio app
|
| 94 |
iface = gr.Interface(
|
| 95 |
+
fn=chatbot_interface,
|
| 96 |
+
inputs=[
|
| 97 |
+
gr.inputs.Textbox(label="Your Message"),
|
| 98 |
+
gr.inputs.Textbox(label="Language (e.g., en, fr, es)", default="en")
|
| 99 |
+
],
|
| 100 |
outputs="text",
|
| 101 |
title="Hotel Chatbot",
|
| 102 |
+
description="Ask questions about the hotel and get responses."
|
| 103 |
)
|
| 104 |
|
| 105 |
# Launch the Gradio app
|