Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from gtts import gTTS
|
|
3 |
import time
|
4 |
from fastapi import FastAPI, Request
|
5 |
from fastapi.responses import HTMLResponse
|
|
|
6 |
from fastapi.staticfiles import StaticFiles
|
7 |
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
8 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
@@ -17,15 +18,18 @@ from huggingface_hub import InferenceClient
|
|
17 |
import json
|
18 |
import re
|
19 |
from deep_translator import GoogleTranslator
|
|
|
|
|
|
|
20 |
|
21 |
|
22 |
-
|
23 |
# Define Pydantic model for incoming request body
|
24 |
class MessageRequest(BaseModel):
|
25 |
message: str
|
26 |
language: str
|
27 |
|
28 |
-
repo_id = "
|
29 |
llm_client = InferenceClient(
|
30 |
model=repo_id,
|
31 |
token=os.getenv("HF_TOKEN"),
|
@@ -61,8 +65,8 @@ templates = Jinja2Templates(directory="static")
|
|
61 |
|
62 |
# Configure Llama index settings
|
63 |
Settings.llm = HuggingFaceInferenceAPI(
|
64 |
-
model_name="
|
65 |
-
tokenizer_name="
|
66 |
context_window=3000,
|
67 |
token=os.getenv("HF_TOKEN"),
|
68 |
max_new_tokens=512,
|
@@ -86,7 +90,17 @@ def data_ingestion_from_directory():
|
|
86 |
storage_context = StorageContext.from_defaults()
|
87 |
index = VectorStoreIndex.from_documents(documents)
|
88 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
|
|
|
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
def initialize():
|
91 |
start_time = time.time()
|
92 |
data_ingestion_from_directory() # Process PDF ingestion at startup
|
@@ -110,13 +124,20 @@ def split_name(full_name):
|
|
110 |
return first_name, last_name
|
111 |
|
112 |
initialize() # Run initialization tasks
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
def handle_query(query):
|
115 |
chat_text_qa_msgs = [
|
116 |
(
|
117 |
"user",
|
118 |
"""
|
119 |
-
You are
|
120 |
{context_str}
|
121 |
Question:
|
122 |
{query_str}
|
@@ -144,7 +165,8 @@ def handle_query(query):
|
|
144 |
response = "Sorry, I couldn't find an answer."
|
145 |
current_chat_history.append((query, response))
|
146 |
return response
|
147 |
-
|
|
|
148 |
@app.get("/ch/{id}", response_class=HTMLResponse)
|
149 |
async def load_chat(request: Request, id: str):
|
150 |
return templates.TemplateResponse("index.html", {"request": request, "user_id": id})
|
@@ -152,7 +174,13 @@ async def load_chat(request: Request, id: str):
|
|
152 |
async def load_chat(request: Request, id: str):
|
153 |
return templates.TemplateResponse("voice.html", {"request": request, "user_id": id})
|
154 |
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
@app.post("/chat/")
|
158 |
async def chat(request: MessageRequest):
|
@@ -182,12 +210,14 @@ async def chat(request: MessageRequest):
|
|
182 |
}
|
183 |
chat_history.append(message_data)
|
184 |
tts = gTTS(text=response1, lang=language_code)
|
185 |
-
audio_path = "
|
186 |
tts.save(audio_path)
|
187 |
return {"response": response1,
|
188 |
-
"audioUrl": f"
|
|
|
189 |
|
190 |
@app.get("/")
|
191 |
def read_root(request: Request):
|
192 |
return templates.TemplateResponse("home.html", {"request": request})
|
193 |
|
|
|
|
3 |
import time
|
4 |
from fastapi import FastAPI, Request
|
5 |
from fastapi.responses import HTMLResponse
|
6 |
+
from fastapi.responses import FileResponse
|
7 |
from fastapi.staticfiles import StaticFiles
|
8 |
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
9 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
|
|
18 |
import json
|
19 |
import re
|
20 |
from deep_translator import GoogleTranslator
|
21 |
+
from dotenv import load_dotenv
|
22 |
+
import random
|
23 |
+
import string
|
24 |
|
25 |
|
26 |
+
load_dotenv()
|
27 |
# Define Pydantic model for incoming request body
|
28 |
class MessageRequest(BaseModel):
|
29 |
message: str
|
30 |
language: str
|
31 |
|
32 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
33 |
llm_client = InferenceClient(
|
34 |
model=repo_id,
|
35 |
token=os.getenv("HF_TOKEN"),
|
|
|
65 |
|
66 |
# Configure Llama index settings
|
67 |
Settings.llm = HuggingFaceInferenceAPI(
|
68 |
+
model_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
69 |
+
tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
70 |
context_window=3000,
|
71 |
token=os.getenv("HF_TOKEN"),
|
72 |
max_new_tokens=512,
|
|
|
90 |
storage_context = StorageContext.from_defaults()
|
91 |
index = VectorStoreIndex.from_documents(documents)
|
92 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
93 |
+
def generate_unique_filename(extension="txt"):
|
94 |
+
# Current timestamp
|
95 |
|
96 |
+
timestamp = time.strftime("%Y%m%d%H%M%S")
|
97 |
+
|
98 |
+
# Generate a random string of 6 characters
|
99 |
+
random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
|
100 |
+
|
101 |
+
# Combine timestamp and random string
|
102 |
+
unique_filename = f"{timestamp}_{random_str}.{extension}"
|
103 |
+
return unique_filename
|
104 |
def initialize():
|
105 |
start_time = time.time()
|
106 |
data_ingestion_from_directory() # Process PDF ingestion at startup
|
|
|
124 |
return first_name, last_name
|
125 |
|
126 |
initialize() # Run initialization tasks
|
127 |
+
#You are the ITC GrandChola Hotel voice chatbot and your name is hotel helper.
|
128 |
+
# Your goal is to provide accurate, professional, and helpful answers to user queries
|
129 |
+
# based on the Taj hotel data. Always ensure your responses are clear and concise.
|
130 |
+
# Give response within 10-15 words only. If you don't know the answer,
|
131 |
+
# you can say 'I don't know'. If you need more information, you can ask the user for clarification.
|
132 |
+
# You can also ask the user if they need help with anything else. Remember to be polite and professional at all times. If you are ready to start, you can say 'I am ready'. If you need to take a break, you can say 'I need a break'.
|
133 |
+
# If you need to end the conversation, you can say 'Goodbye'.
|
134 |
+
|
135 |
def handle_query(query):
|
136 |
chat_text_qa_msgs = [
|
137 |
(
|
138 |
"user",
|
139 |
"""
|
140 |
+
You are an assistant designed to help customers with inquiries about ITC Grand Chola Hotel. Your goal is to provide accurate, professional, and helpful answers based on available hotel information. If the user greets or doesn't ask a specific question, respond politely by encouraging them to ask about the hotel. Answer questions in a single sentence, within 5-10 words, while maintaining politeness and professionalism at all times.
|
141 |
{context_str}
|
142 |
Question:
|
143 |
{query_str}
|
|
|
165 |
response = "Sorry, I couldn't find an answer."
|
166 |
current_chat_history.append((query, response))
|
167 |
return response
|
168 |
+
def generate_unique_audio_filename():
|
169 |
+
return f"audio/response_{uuid.uuid4().hex}.mp3"
|
170 |
@app.get("/ch/{id}", response_class=HTMLResponse)
|
171 |
async def load_chat(request: Request, id: str):
|
172 |
return templates.TemplateResponse("index.html", {"request": request, "user_id": id})
|
|
|
174 |
async def load_chat(request: Request, id: str):
|
175 |
return templates.TemplateResponse("voice.html", {"request": request, "user_id": id})
|
176 |
|
177 |
+
@app.get("/audio/{filename}")
|
178 |
+
async def get_audio(filename: str):
|
179 |
+
audio_path = os.path.join(os.getcwd(), filename) # Ensure correct file path
|
180 |
+
if os.path.exists(audio_path):
|
181 |
+
return FileResponse(audio_path)
|
182 |
+
else:
|
183 |
+
raise HTTPException(status_code=404, detail="Audio file not found.")
|
184 |
|
185 |
@app.post("/chat/")
|
186 |
async def chat(request: MessageRequest):
|
|
|
210 |
}
|
211 |
chat_history.append(message_data)
|
212 |
tts = gTTS(text=response1, lang=language_code)
|
213 |
+
audio_path = generate_unique_filename("mp3")
|
214 |
tts.save(audio_path)
|
215 |
return {"response": response1,
|
216 |
+
"audioUrl": f"http://localhost:8000/audio/{audio_path}"}
|
217 |
+
|
218 |
|
219 |
@app.get("/")
|
220 |
def read_root(request: Request):
|
221 |
return templates.TemplateResponse("home.html", {"request": request})
|
222 |
|
223 |
+
|