Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,273 +1,47 @@
|
|
1 |
-
# import os
|
2 |
-
# import time
|
3 |
-
# from fastapi import FastAPI,Request
|
4 |
-
# from fastapi.responses import HTMLResponse
|
5 |
-
# from fastapi.staticfiles import StaticFiles
|
6 |
-
# from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
7 |
-
# from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
8 |
-
# from pydantic import BaseModel
|
9 |
-
# from fastapi.responses import JSONResponse
|
10 |
-
# import uuid # for generating unique IDs
|
11 |
-
# import datetime
|
12 |
-
# from fastapi.middleware.cors import CORSMiddleware
|
13 |
-
# from fastapi.templating import Jinja2Templates
|
14 |
-
# from huggingface_hub import InferenceClient
|
15 |
-
# import json
|
16 |
-
# import re
|
17 |
-
# from gradio_client import Client
|
18 |
-
# from simple_salesforce import Salesforce, SalesforceLogin
|
19 |
-
# from llama_index.llms.huggingface import HuggingFaceLLM
|
20 |
-
|
21 |
-
|
22 |
-
# # Define Pydantic model for incoming request body
|
23 |
-
# class MessageRequest(BaseModel):
|
24 |
-
# message: str
|
25 |
-
# # repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
26 |
-
# # llm_client = InferenceClient(
|
27 |
-
# # model=repo_id,
|
28 |
-
# # token=os.getenv("HF_TOKEN"),
|
29 |
-
# # )
|
30 |
-
|
31 |
-
|
32 |
-
# os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
33 |
-
# username = os.getenv("username")
|
34 |
-
# password = os.getenv("password")
|
35 |
-
# security_token = os.getenv("security_token")
|
36 |
-
# domain = os.getenv("domain")# Using sandbox environment
|
37 |
-
# session_id, sf_instance = SalesforceLogin(username=username, password=password, security_token=security_token, domain=domain)
|
38 |
-
|
39 |
-
# # Create Salesforce object
|
40 |
-
# sf = Salesforce(instance=sf_instance, session_id=session_id)
|
41 |
-
|
42 |
-
# app = FastAPI()
|
43 |
-
|
44 |
-
|
45 |
-
# @app.middleware("http")
|
46 |
-
# async def add_security_headers(request: Request, call_next):
|
47 |
-
# response = await call_next(request)
|
48 |
-
# response.headers["Content-Security-Policy"] = "frame-ancestors *; frame-src *; object-src *;"
|
49 |
-
# response.headers["X-Frame-Options"] = "ALLOWALL"
|
50 |
-
# return response
|
51 |
-
|
52 |
-
|
53 |
-
# # Allow CORS requests from any domain
|
54 |
-
# app.add_middleware(
|
55 |
-
# CORSMiddleware,
|
56 |
-
# allow_origins=["*"],
|
57 |
-
# allow_credentials=True,
|
58 |
-
# allow_methods=["*"],
|
59 |
-
# allow_headers=["*"],
|
60 |
-
# )
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
# @app.get("/favicon.ico")
|
66 |
-
# async def favicon():
|
67 |
-
# return HTMLResponse("") # or serve a real favicon if you have one
|
68 |
-
|
69 |
-
|
70 |
-
# app.mount("/static", StaticFiles(directory="static"), name="static")
|
71 |
-
|
72 |
-
# templates = Jinja2Templates(directory="static")
|
73 |
-
# # Configure Llama index settings
|
74 |
-
# # Settings.llm = HuggingFaceInferenceAPI(
|
75 |
-
# # model_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
76 |
-
# # tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
77 |
-
# # context_window=3000,
|
78 |
-
# # token=os.getenv("HF_TOKEN"),
|
79 |
-
# # max_new_tokens=512,
|
80 |
-
# # generate_kwargs={"temperature": 0.1},
|
81 |
-
# # )
|
82 |
-
# # Configure Llama index settings
|
83 |
-
# Settings.llm = HuggingFaceLLM(
|
84 |
-
# model_name="google/flan-t5-small",
|
85 |
-
# tokenizer_name="google/flan-t5-small",
|
86 |
-
# context_window=512, # flan-t5-small has a max context window of 512 tokens
|
87 |
-
# max_new_tokens=256,
|
88 |
-
# generate_kwargs={"temperature": 0.1, "do_sample": True},
|
89 |
-
# device_map="auto" # Automatically use GPU if available, else CPU
|
90 |
-
# )
|
91 |
-
# Settings.embed_model = HuggingFaceEmbedding(
|
92 |
-
# model_name="BAAI/bge-small-en-v1.5"
|
93 |
-
# )
|
94 |
-
|
95 |
-
# PERSIST_DIR = "db"
|
96 |
-
# PDF_DIRECTORY = 'data'
|
97 |
-
|
98 |
-
# # Ensure directories exist
|
99 |
-
# os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
100 |
-
# os.makedirs(PERSIST_DIR, exist_ok=True)
|
101 |
-
# chat_history = []
|
102 |
-
# current_chat_history = []
|
103 |
-
# def data_ingestion_from_directory():
|
104 |
-
# documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
|
105 |
-
# storage_context = StorageContext.from_defaults()
|
106 |
-
# index = VectorStoreIndex.from_documents(documents)
|
107 |
-
# index.storage_context.persist(persist_dir=PERSIST_DIR)
|
108 |
-
|
109 |
-
# def initialize():
|
110 |
-
# start_time = time.time()
|
111 |
-
# data_ingestion_from_directory() # Process PDF ingestion at startup
|
112 |
-
# print(f"Data ingestion time: {time.time() - start_time} seconds")
|
113 |
-
# def split_name(full_name):
|
114 |
-
# # Split the name by spaces
|
115 |
-
# words = full_name.strip().split()
|
116 |
-
|
117 |
-
# # Logic for determining first name and last name
|
118 |
-
# if len(words) == 1:
|
119 |
-
# first_name = ''
|
120 |
-
# last_name = words[0]
|
121 |
-
# elif len(words) == 2:
|
122 |
-
# first_name = words[0]
|
123 |
-
# last_name = words[1]
|
124 |
-
# else:
|
125 |
-
# first_name = words[0]
|
126 |
-
# last_name = ' '.join(words[1:])
|
127 |
-
|
128 |
-
# return first_name, last_name
|
129 |
-
|
130 |
-
# initialize() # Run initialization tasks
|
131 |
-
|
132 |
-
|
133 |
-
# def handle_query(query):
|
134 |
-
# chat_text_qa_msgs = [
|
135 |
-
# (
|
136 |
-
# "user",
|
137 |
-
# """
|
138 |
-
# You are the Clara Redfernstech chatbot. Your goal is to provide accurate, professional, and helpful answers to user queries based on the company's data. Always ensure your responses are clear and concise. Give response within 10-15 words only
|
139 |
-
# {context_str}
|
140 |
-
# Question:
|
141 |
-
# {query_str}
|
142 |
-
# """
|
143 |
-
# )
|
144 |
-
# ]
|
145 |
-
# text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
146 |
-
|
147 |
-
# storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
148 |
-
# index = load_index_from_storage(storage_context)
|
149 |
-
# context_str = ""
|
150 |
-
# for past_query, response in reversed(current_chat_history):
|
151 |
-
# if past_query.strip():
|
152 |
-
# context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
153 |
-
|
154 |
-
|
155 |
-
# query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
156 |
-
# answer = query_engine.query(query)
|
157 |
-
|
158 |
-
# if hasattr(answer, 'response'):
|
159 |
-
# response=answer.response
|
160 |
-
# elif isinstance(answer, dict) and 'response' in answer:
|
161 |
-
# response =answer['response']
|
162 |
-
# else:
|
163 |
-
# response ="Sorry, I couldn't find an answer."
|
164 |
-
# current_chat_history.append((query, response))
|
165 |
-
# return response
|
166 |
-
# @app.get("/ch/{id}", response_class=HTMLResponse)
|
167 |
-
# async def load_chat(request: Request, id: str):
|
168 |
-
# return templates.TemplateResponse("index.html", {"request": request, "user_id": id})
|
169 |
-
# # Route to save chat history
|
170 |
-
# @app.post("/hist/")
|
171 |
-
# async def save_chat_history(history: dict):
|
172 |
-
# # Check if 'userId' is present in the incoming dictionary
|
173 |
-
# user_id = history.get('userId')
|
174 |
-
# print(user_id)
|
175 |
-
|
176 |
-
# # Ensure user_id is defined before proceeding
|
177 |
-
# if user_id is None:
|
178 |
-
# return {"error": "userId is required"}, 400
|
179 |
-
|
180 |
-
# # Construct the chat history string
|
181 |
-
# hist = ''.join([f"'{entry['sender']}: {entry['message']}'\n" for entry in history['history']])
|
182 |
-
# hist = "You are a Redfernstech summarize model. Your aim is to use this conversation to identify user interests solely based on that conversation: " + hist
|
183 |
-
# print(hist)
|
184 |
-
|
185 |
-
# # Get the summarized result from the client model
|
186 |
-
# result = hist
|
187 |
-
|
188 |
-
# try:
|
189 |
-
# sf.Lead.update(user_id, {'Description': result})
|
190 |
-
# except Exception as e:
|
191 |
-
# return {"error": f"Failed to update lead: {str(e)}"}, 500
|
192 |
-
|
193 |
-
# return {"summary": result, "message": "Chat history saved"}
|
194 |
-
# @app.post("/webhook")
|
195 |
-
# async def receive_form_data(request: Request):
|
196 |
-
# form_data = await request.json()
|
197 |
-
# # Log in to Salesforce
|
198 |
-
# first_name, last_name = split_name(form_data['name'])
|
199 |
-
# data = {
|
200 |
-
# 'FirstName': first_name,
|
201 |
-
# 'LastName': last_name,
|
202 |
-
# 'Description': 'hii', # Static description
|
203 |
-
# 'Company': form_data['company'], # Assuming company is available in form_data
|
204 |
-
# 'Phone': form_data['phone'].strip(), # Phone from form data
|
205 |
-
# 'Email': form_data['email'], # Email from form data
|
206 |
-
# }
|
207 |
-
# a=sf.Lead.create(data)
|
208 |
-
# # Generate a unique ID (for tracking user)
|
209 |
-
# unique_id = a['id']
|
210 |
-
|
211 |
-
# # Here you can do something with form_data like saving it to a database
|
212 |
-
# print("Received form data:", form_data)
|
213 |
-
|
214 |
-
# # Send back the unique id to the frontend
|
215 |
-
# return JSONResponse({"id": unique_id})
|
216 |
-
|
217 |
-
# @app.post("/chat/")
|
218 |
-
# async def chat(request: MessageRequest):
|
219 |
-
# message = request.message # Access the message from the request body
|
220 |
-
# response = handle_query(message) # Process the message
|
221 |
-
# message_data = {
|
222 |
-
# "sender": "User",
|
223 |
-
# "message": message,
|
224 |
-
# "response": response,
|
225 |
-
# "timestamp": datetime.datetime.now().isoformat()
|
226 |
-
# }
|
227 |
-
# chat_history.append(message_data)
|
228 |
-
# return {"response": response}
|
229 |
-
# @app.get("/")
|
230 |
-
# def read_root():
|
231 |
-
# return {"message": "Welcome to the API"}
|
232 |
-
|
233 |
import os
|
234 |
import time
|
235 |
-
import
|
236 |
-
from fastapi import FastAPI, Request
|
237 |
from fastapi.responses import HTMLResponse
|
238 |
from fastapi.staticfiles import StaticFiles
|
239 |
-
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader,
|
240 |
-
from llama_index.llms.huggingface import HuggingFaceLLM
|
241 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
242 |
from pydantic import BaseModel
|
243 |
from fastapi.responses import JSONResponse
|
|
|
244 |
import datetime
|
245 |
from fastapi.middleware.cors import CORSMiddleware
|
246 |
from fastapi.templating import Jinja2Templates
|
|
|
|
|
|
|
|
|
247 |
from simple_salesforce import Salesforce, SalesforceLogin
|
248 |
-
from
|
249 |
|
250 |
-
# Set up logging
|
251 |
-
logging.basicConfig(level=logging.INFO)
|
252 |
-
logger = logging.getLogger(__name__)
|
253 |
|
254 |
# Define Pydantic model for incoming request body
|
255 |
class MessageRequest(BaseModel):
|
256 |
message: str
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
|
258 |
-
|
259 |
-
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN", "") # Optional: Needed for model download if gated
|
260 |
username = os.getenv("username")
|
261 |
password = os.getenv("password")
|
262 |
security_token = os.getenv("security_token")
|
263 |
-
domain =
|
264 |
session_id, sf_instance = SalesforceLogin(username=username, password=password, security_token=security_token, domain=domain)
|
265 |
|
266 |
-
# Create Salesforce object
|
267 |
sf = Salesforce(instance=sf_instance, session_id=session_id)
|
268 |
|
269 |
app = FastAPI()
|
270 |
|
|
|
271 |
@app.middleware("http")
|
272 |
async def add_security_headers(request: Request, call_next):
|
273 |
response = await call_next(request)
|
@@ -275,6 +49,7 @@ async def add_security_headers(request: Request, call_next):
|
|
275 |
response.headers["X-Frame-Options"] = "ALLOWALL"
|
276 |
return response
|
277 |
|
|
|
278 |
# Allow CORS requests from any domain
|
279 |
app.add_middleware(
|
280 |
CORSMiddleware,
|
@@ -284,162 +59,157 @@ app.add_middleware(
|
|
284 |
allow_headers=["*"],
|
285 |
)
|
286 |
|
|
|
|
|
|
|
287 |
@app.get("/favicon.ico")
|
288 |
async def favicon():
|
289 |
return HTMLResponse("") # or serve a real favicon if you have one
|
290 |
|
|
|
291 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
292 |
|
293 |
templates = Jinja2Templates(directory="static")
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
generate_kwargs={"temperature": 0.3, "do_sample": True}, # Increased temperature for better responses
|
303 |
-
model=AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small"),
|
304 |
-
tokenizer=tokenizer,
|
305 |
-
device_map="auto" # Automatically use GPU if available, else CPU
|
306 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
Settings.embed_model = HuggingFaceEmbedding(
|
308 |
model_name="BAAI/bge-small-en-v1.5"
|
309 |
)
|
310 |
|
311 |
PERSIST_DIR = "db"
|
312 |
-
PDF_DIRECTORY =
|
313 |
|
314 |
# Ensure directories exist
|
315 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
316 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
317 |
chat_history = []
|
318 |
-
current_chat_history = [
|
319 |
-
("What is Redfernstech?", "Redfernstech is a technology company specializing in AI solutions."),
|
320 |
-
("What services does Redfernstech offer?", "Redfernstech offers AI-powered chatbots, data analytics, and automation tools.")
|
321 |
-
]
|
322 |
-
|
323 |
def data_ingestion_from_directory():
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
logger.warning(f"No documents found in {PDF_DIRECTORY}. Ensure PDF files are present.")
|
329 |
-
storage_context = StorageContext.from_defaults()
|
330 |
-
index = VectorStoreIndex.from_documents(documents)
|
331 |
-
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
332 |
-
logger.info(f"Index persisted to {PERSIST_DIR}")
|
333 |
-
except Exception as e:
|
334 |
-
logger.error(f"Error during data ingestion: {str(e)}")
|
335 |
-
raise
|
336 |
|
337 |
def initialize():
|
338 |
start_time = time.time()
|
339 |
data_ingestion_from_directory() # Process PDF ingestion at startup
|
340 |
-
|
341 |
-
|
342 |
def split_name(full_name):
|
343 |
# Split the name by spaces
|
344 |
words = full_name.strip().split()
|
345 |
|
346 |
# Logic for determining first name and last name
|
347 |
if len(words) == 1:
|
348 |
-
first_name =
|
349 |
last_name = words[0]
|
350 |
elif len(words) == 2:
|
351 |
first_name = words[0]
|
352 |
last_name = words[1]
|
353 |
else:
|
354 |
first_name = words[0]
|
355 |
-
last_name =
|
356 |
|
357 |
return first_name, last_name
|
358 |
|
359 |
initialize() # Run initialization tasks
|
360 |
|
|
|
361 |
def handle_query(query):
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
|
|
|
|
|
|
371 |
|
372 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
373 |
index = load_index_from_storage(storage_context)
|
|
|
|
|
|
|
|
|
|
|
374 |
|
375 |
-
|
376 |
-
context_str = "\n".join([f"User asked: '{past_query}'\nBot answered: '{response}'"
|
377 |
-
for past_query, response in reversed(current_chat_history)])
|
378 |
-
|
379 |
-
logger.info(f"Query: {query}")
|
380 |
-
logger.info(f"Context: {context_str}")
|
381 |
-
|
382 |
-
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
383 |
answer = query_engine.query(query)
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
response =
|
389 |
-
elif isinstance(answer, dict) and "response" in answer and answer["response"]:
|
390 |
-
response = answer["response"].strip()
|
391 |
else:
|
392 |
-
response =
|
393 |
-
|
394 |
-
logger.info(f"Processed response: {response}")
|
395 |
current_chat_history.append((query, response))
|
396 |
return response
|
397 |
-
|
398 |
@app.get("/ch/{id}", response_class=HTMLResponse)
|
399 |
async def load_chat(request: Request, id: str):
|
400 |
return templates.TemplateResponse("index.html", {"request": request, "user_id": id})
|
401 |
-
|
402 |
@app.post("/hist/")
|
403 |
async def save_chat_history(history: dict):
|
404 |
# Check if 'userId' is present in the incoming dictionary
|
405 |
-
user_id = history.get(
|
406 |
-
|
407 |
|
408 |
# Ensure user_id is defined before proceeding
|
409 |
if user_id is None:
|
410 |
return {"error": "userId is required"}, 400
|
411 |
|
412 |
# Construct the chat history string
|
413 |
-
hist =
|
414 |
hist = "You are a Redfernstech summarize model. Your aim is to use this conversation to identify user interests solely based on that conversation: " + hist
|
415 |
-
|
|
|
|
|
|
|
416 |
|
417 |
try:
|
418 |
-
sf.Lead.update(user_id, {
|
419 |
except Exception as e:
|
420 |
-
logger.error(f"Failed to update lead: {str(e)}")
|
421 |
return {"error": f"Failed to update lead: {str(e)}"}, 500
|
422 |
|
423 |
-
return {"summary":
|
424 |
-
|
425 |
@app.post("/webhook")
|
426 |
async def receive_form_data(request: Request):
|
427 |
form_data = await request.json()
|
428 |
# Log in to Salesforce
|
429 |
-
first_name, last_name = split_name(form_data[
|
430 |
data = {
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
}
|
438 |
-
a
|
439 |
# Generate a unique ID (for tracking user)
|
440 |
-
unique_id = a[
|
441 |
|
442 |
-
|
|
|
443 |
|
444 |
# Send back the unique id to the frontend
|
445 |
return JSONResponse({"id": unique_id})
|
@@ -447,7 +217,6 @@ async def receive_form_data(request: Request):
|
|
447 |
@app.post("/chat/")
|
448 |
async def chat(request: MessageRequest):
|
449 |
message = request.message # Access the message from the request body
|
450 |
-
logger.info(f"Received chat message: {message}")
|
451 |
response = handle_query(message) # Process the message
|
452 |
message_data = {
|
453 |
"sender": "User",
|
@@ -456,9 +225,7 @@ async def chat(request: MessageRequest):
|
|
456 |
"timestamp": datetime.datetime.now().isoformat()
|
457 |
}
|
458 |
chat_history.append(message_data)
|
459 |
-
logger.info(f"Chat response: {response}")
|
460 |
return {"response": response}
|
461 |
-
|
462 |
@app.get("/")
|
463 |
def read_root():
|
464 |
return {"message": "Welcome to the API"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import time
|
3 |
+
from fastapi import FastAPI,Request
|
|
|
4 |
from fastapi.responses import HTMLResponse
|
5 |
from fastapi.staticfiles import StaticFiles
|
6 |
+
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
|
|
7 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
8 |
from pydantic import BaseModel
|
9 |
from fastapi.responses import JSONResponse
|
10 |
+
import uuid # for generating unique IDs
|
11 |
import datetime
|
12 |
from fastapi.middleware.cors import CORSMiddleware
|
13 |
from fastapi.templating import Jinja2Templates
|
14 |
+
from huggingface_hub import InferenceClient
|
15 |
+
import json
|
16 |
+
import re
|
17 |
+
from gradio_client import Client
|
18 |
from simple_salesforce import Salesforce, SalesforceLogin
|
19 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
20 |
|
|
|
|
|
|
|
21 |
|
22 |
# Define Pydantic model for incoming request body
|
23 |
class MessageRequest(BaseModel):
|
24 |
message: str
|
25 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
26 |
+
llm_client = InferenceClient(
|
27 |
+
model=repo_id,
|
28 |
+
token=os.getenv("HF_TOKEN"),
|
29 |
+
)
|
30 |
+
|
31 |
|
32 |
+
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
|
|
33 |
username = os.getenv("username")
|
34 |
password = os.getenv("password")
|
35 |
security_token = os.getenv("security_token")
|
36 |
+
domain = os.getenv("domain")# Using sandbox environment
|
37 |
session_id, sf_instance = SalesforceLogin(username=username, password=password, security_token=security_token, domain=domain)
|
38 |
|
39 |
+
# Create Salesforce object
|
40 |
sf = Salesforce(instance=sf_instance, session_id=session_id)
|
41 |
|
42 |
app = FastAPI()
|
43 |
|
44 |
+
|
45 |
@app.middleware("http")
|
46 |
async def add_security_headers(request: Request, call_next):
|
47 |
response = await call_next(request)
|
|
|
49 |
response.headers["X-Frame-Options"] = "ALLOWALL"
|
50 |
return response
|
51 |
|
52 |
+
|
53 |
# Allow CORS requests from any domain
|
54 |
app.add_middleware(
|
55 |
CORSMiddleware,
|
|
|
59 |
allow_headers=["*"],
|
60 |
)
|
61 |
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
@app.get("/favicon.ico")
|
66 |
async def favicon():
|
67 |
return HTMLResponse("") # or serve a real favicon if you have one
|
68 |
|
69 |
+
|
70 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
71 |
|
72 |
templates = Jinja2Templates(directory="static")
|
73 |
+
Configure Llama index settings
|
74 |
+
Settings.llm = HuggingFaceInferenceAPI(
|
75 |
+
model_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
76 |
+
tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
77 |
+
context_window=3000,
|
78 |
+
token=os.getenv("HF_TOKEN"),
|
79 |
+
max_new_tokens=512,
|
80 |
+
generate_kwargs={"temperature": 0.1},
|
|
|
|
|
|
|
|
|
81 |
)
|
82 |
+
# Configure Llama index settings
|
83 |
+
# Settings.llm = HuggingFaceLLM(
|
84 |
+
# model_name="google/flan-t5-small",
|
85 |
+
# tokenizer_name="google/flan-t5-small",
|
86 |
+
# context_window=512, # flan-t5-small has a max context window of 512 tokens
|
87 |
+
# max_new_tokens=256,
|
88 |
+
# generate_kwargs={"temperature": 0.1, "do_sample": True},
|
89 |
+
# device_map="auto" # Automatically use GPU if available, else CPU
|
90 |
+
# )
|
91 |
Settings.embed_model = HuggingFaceEmbedding(
|
92 |
model_name="BAAI/bge-small-en-v1.5"
|
93 |
)
|
94 |
|
95 |
PERSIST_DIR = "db"
|
96 |
+
PDF_DIRECTORY = 'data'
|
97 |
|
98 |
# Ensure directories exist
|
99 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
100 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
101 |
chat_history = []
|
102 |
+
current_chat_history = []
|
|
|
|
|
|
|
|
|
103 |
def data_ingestion_from_directory():
|
104 |
+
documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
|
105 |
+
storage_context = StorageContext.from_defaults()
|
106 |
+
index = VectorStoreIndex.from_documents(documents)
|
107 |
+
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
def initialize():
|
110 |
start_time = time.time()
|
111 |
data_ingestion_from_directory() # Process PDF ingestion at startup
|
112 |
+
print(f"Data ingestion time: {time.time() - start_time} seconds")
|
|
|
113 |
def split_name(full_name):
|
114 |
# Split the name by spaces
|
115 |
words = full_name.strip().split()
|
116 |
|
117 |
# Logic for determining first name and last name
|
118 |
if len(words) == 1:
|
119 |
+
first_name = ''
|
120 |
last_name = words[0]
|
121 |
elif len(words) == 2:
|
122 |
first_name = words[0]
|
123 |
last_name = words[1]
|
124 |
else:
|
125 |
first_name = words[0]
|
126 |
+
last_name = ' '.join(words[1:])
|
127 |
|
128 |
return first_name, last_name
|
129 |
|
130 |
initialize() # Run initialization tasks
|
131 |
|
132 |
+
|
133 |
def handle_query(query):
|
134 |
+
chat_text_qa_msgs = [
|
135 |
+
(
|
136 |
+
"user",
|
137 |
+
"""
|
138 |
+
You are the Clara Redfernstech chatbot. Your goal is to provide accurate, professional, and helpful answers to user queries based on the company's data. Always ensure your responses are clear and concise. Give response within 10-15 words only
|
139 |
+
{context_str}
|
140 |
+
Question:
|
141 |
+
{query_str}
|
142 |
+
"""
|
143 |
+
)
|
144 |
+
]
|
145 |
+
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
146 |
|
147 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
148 |
index = load_index_from_storage(storage_context)
|
149 |
+
context_str = ""
|
150 |
+
for past_query, response in reversed(current_chat_history):
|
151 |
+
if past_query.strip():
|
152 |
+
context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
153 |
+
|
154 |
|
155 |
+
query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
answer = query_engine.query(query)
|
157 |
+
|
158 |
+
if hasattr(answer, 'response'):
|
159 |
+
response=answer.response
|
160 |
+
elif isinstance(answer, dict) and 'response' in answer:
|
161 |
+
response =answer['response']
|
|
|
|
|
162 |
else:
|
163 |
+
response ="Sorry, I couldn't find an answer."
|
|
|
|
|
164 |
current_chat_history.append((query, response))
|
165 |
return response
|
|
|
166 |
@app.get("/ch/{id}", response_class=HTMLResponse)
|
167 |
async def load_chat(request: Request, id: str):
|
168 |
return templates.TemplateResponse("index.html", {"request": request, "user_id": id})
|
169 |
+
# Route to save chat history
|
170 |
@app.post("/hist/")
|
171 |
async def save_chat_history(history: dict):
|
172 |
# Check if 'userId' is present in the incoming dictionary
|
173 |
+
user_id = history.get('userId')
|
174 |
+
print(user_id)
|
175 |
|
176 |
# Ensure user_id is defined before proceeding
|
177 |
if user_id is None:
|
178 |
return {"error": "userId is required"}, 400
|
179 |
|
180 |
# Construct the chat history string
|
181 |
+
hist = ''.join([f"'{entry['sender']}: {entry['message']}'\n" for entry in history['history']])
|
182 |
hist = "You are a Redfernstech summarize model. Your aim is to use this conversation to identify user interests solely based on that conversation: " + hist
|
183 |
+
print(hist)
|
184 |
+
|
185 |
+
# Get the summarized result from the client model
|
186 |
+
result = hist
|
187 |
|
188 |
try:
|
189 |
+
sf.Lead.update(user_id, {'Description': result})
|
190 |
except Exception as e:
|
|
|
191 |
return {"error": f"Failed to update lead: {str(e)}"}, 500
|
192 |
|
193 |
+
return {"summary": result, "message": "Chat history saved"}
|
|
|
194 |
@app.post("/webhook")
|
195 |
async def receive_form_data(request: Request):
|
196 |
form_data = await request.json()
|
197 |
# Log in to Salesforce
|
198 |
+
first_name, last_name = split_name(form_data['name'])
|
199 |
data = {
|
200 |
+
'FirstName': first_name,
|
201 |
+
'LastName': last_name,
|
202 |
+
'Description': 'hii', # Static description
|
203 |
+
'Company': form_data['company'], # Assuming company is available in form_data
|
204 |
+
'Phone': form_data['phone'].strip(), # Phone from form data
|
205 |
+
'Email': form_data['email'], # Email from form data
|
206 |
}
|
207 |
+
a=sf.Lead.create(data)
|
208 |
# Generate a unique ID (for tracking user)
|
209 |
+
unique_id = a['id']
|
210 |
|
211 |
+
# Here you can do something with form_data like saving it to a database
|
212 |
+
print("Received form data:", form_data)
|
213 |
|
214 |
# Send back the unique id to the frontend
|
215 |
return JSONResponse({"id": unique_id})
|
|
|
217 |
@app.post("/chat/")
|
218 |
async def chat(request: MessageRequest):
|
219 |
message = request.message # Access the message from the request body
|
|
|
220 |
response = handle_query(message) # Process the message
|
221 |
message_data = {
|
222 |
"sender": "User",
|
|
|
225 |
"timestamp": datetime.datetime.now().isoformat()
|
226 |
}
|
227 |
chat_history.append(message_data)
|
|
|
228 |
return {"response": response}
|
|
|
229 |
@app.get("/")
|
230 |
def read_root():
|
231 |
return {"message": "Welcome to the API"}
|