Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,43 +5,20 @@ from llama_index.core import StorageContext, load_index_from_storage, VectorStor
|
|
| 5 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
| 6 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Ensure HF_TOKEN is set
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
if not HF_TOKEN:
|
| 12 |
raise ValueError("HF_TOKEN environment variable not set.")
|
| 13 |
|
| 14 |
-
repo_id = "
|
| 15 |
llm_client = InferenceClient(
|
| 16 |
model=repo_id,
|
| 17 |
token=HF_TOKEN,
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Configure Llama index settings
|
| 21 |
-
# Settings.llm = HuggingFaceInferenceAPI(
|
| 22 |
-
# model_name=repo_id,
|
| 23 |
-
# tokenizer_name=repo_id,
|
| 24 |
-
# context_window=3000,
|
| 25 |
-
# token=HF_TOKEN,
|
| 26 |
-
# max_new_tokens=512,
|
| 27 |
-
# generate_kwargs={"temperature": 0.1},
|
| 28 |
-
# )
|
| 29 |
-
# Settings.embed_model = HuggingFaceEmbedding(
|
| 30 |
-
# model_name="BAAI/bge-small-en-v1.5"
|
| 31 |
-
# )
|
| 32 |
-
# # Configure Llama index settings
|
| 33 |
-
# Settings.llm = HuggingFaceInferenceAPI(
|
| 34 |
-
# model_name="xlm-roberta-base",
|
| 35 |
-
# tokenizer_name="xlm-roberta-base",
|
| 36 |
-
# context_window=3000,
|
| 37 |
-
# token=HF_TOKEN,
|
| 38 |
-
# max_new_tokens=512,
|
| 39 |
-
# generate_kwargs={"temperature": 0.1},
|
| 40 |
-
# )
|
| 41 |
-
|
| 42 |
-
# Settings.embed_model = HuggingFaceEmbedding(
|
| 43 |
-
# model_name="sentence-transformers/paraphrase-xlm-r-100langs-v1"
|
| 44 |
-
# )
|
| 45 |
# Configure Llama index settings
|
| 46 |
Settings.llm = HuggingFaceInferenceAPI(
|
| 47 |
model_name=repo_id,
|
|
@@ -51,11 +28,18 @@ Settings.llm = HuggingFaceInferenceAPI(
|
|
| 51 |
max_new_tokens=512,
|
| 52 |
generate_kwargs={"temperature": 0.1},
|
| 53 |
)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
PERSIST_DIR = "db"
|
| 60 |
PDF_DIRECTORY = 'data'
|
| 61 |
|
|
@@ -83,42 +67,29 @@ def data_ingestion_from_directory():
|
|
| 83 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
| 84 |
|
| 85 |
def handle_query(query):
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# """
|
| 96 |
-
# )
|
| 97 |
-
# ]
|
| 98 |
-
chat_text_qa_msgs = [
|
| 99 |
-
(
|
| 100 |
-
"user",
|
| 101 |
-
"""
|
| 102 |
-
You are the Taj Hotel chatbot, known as Taj Hotel Helper.
|
| 103 |
-
Your goal is to provide accurate and professional answers to
|
| 104 |
-
user queries about the Taj Hotel in the language they use:
|
| 105 |
-
English, Telugu, or Hindi. Always respond clearly and concisely,
|
| 106 |
-
ideally within 10-15 words. If you don't know the answer, say so politely.
|
| 107 |
|
| 108 |
-
Context:
|
| 109 |
-
{context_str}
|
| 110 |
|
| 111 |
-
User's Question:
|
| 112 |
-
{query_str}
|
| 113 |
|
| 114 |
-
Language-Specific Guidance:
|
| 115 |
-
- For English: Respond in English.
|
| 116 |
-
- For Telugu: తెలుగు లో సమాధానం ఇవ్వండి.
|
| 117 |
-
- For Hindi: हिंदी में उत्तर दें.
|
| 118 |
-
"""
|
| 119 |
-
)
|
| 120 |
]
|
| 121 |
-
|
| 122 |
|
| 123 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
| 124 |
|
|
|
|
| 5 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
| 6 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
+
from transformers import AutoTokenizer, AutoModel
|
| 9 |
+
|
| 10 |
|
| 11 |
# Ensure HF_TOKEN is set
|
| 12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
if not HF_TOKEN:
|
| 14 |
raise ValueError("HF_TOKEN environment variable not set.")
|
| 15 |
|
| 16 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 17 |
llm_client = InferenceClient(
|
| 18 |
model=repo_id,
|
| 19 |
token=HF_TOKEN,
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Configure Llama index settings
|
| 23 |
Settings.llm = HuggingFaceInferenceAPI(
|
| 24 |
model_name=repo_id,
|
|
|
|
| 28 |
max_new_tokens=512,
|
| 29 |
generate_kwargs={"temperature": 0.1},
|
| 30 |
)
|
| 31 |
+
# Settings.embed_model = HuggingFaceEmbedding(
|
| 32 |
+
# model_name="BAAI/bge-small-en-v1.5"
|
| 33 |
+
# )
|
| 34 |
+
# Replace the embedding model with XLM-R
|
| 35 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
| 36 |
+
model_name="xlm-roberta-base" # XLM-RoBERTa model for multilingual support
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Configure tokenizer and model if required
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
|
| 41 |
+
model = AutoModel.from_pretrained("xlm-roberta-base")
|
| 42 |
+
|
| 43 |
PERSIST_DIR = "db"
|
| 44 |
PDF_DIRECTORY = 'data'
|
| 45 |
|
|
|
|
| 67 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
| 68 |
|
| 69 |
def handle_query(query):
|
| 70 |
+
chat_text_qa_msgs = [
|
| 71 |
+
(
|
| 72 |
+
"user",
|
| 73 |
+
"""
|
| 74 |
+
You are the Taj Hotel chatbot, known as Taj Hotel Helper.
|
| 75 |
+
Your goal is to provide accurate and professional answers to
|
| 76 |
+
user queries about the Taj Hotel in the language they use:
|
| 77 |
+
English, Telugu, or Hindi. Always respond clearly and concisely,
|
| 78 |
+
ideally within 10-15 words. If you don't know the answer, say so politely.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
Context:
|
| 81 |
+
{context_str}
|
| 82 |
|
| 83 |
+
User's Question:
|
| 84 |
+
{query_str}
|
| 85 |
|
| 86 |
+
Language-Specific Guidance:
|
| 87 |
+
- For English: Respond in English.
|
| 88 |
+
- For Telugu: తెలుగు లో సమాధానం ఇవ్వండి.
|
| 89 |
+
- For Hindi: हिंदी में उत्तर दें.
|
| 90 |
+
"""
|
| 91 |
+
)
|
| 92 |
]
|
|
|
|
| 93 |
|
| 94 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
| 95 |
|