redfernstech commited on
Commit
60a19b7
·
verified ·
1 Parent(s): b25e3e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +273 -61
app.py CHANGED
@@ -1,47 +1,268 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,7 +270,6 @@ async def add_security_headers(request: Request, call_next):
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,26 +279,14 @@ app.add_middleware(
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",
@@ -86,6 +294,7 @@ Settings.llm = HuggingFaceLLM(
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(
@@ -93,13 +302,14 @@ Settings.embed_model = HuggingFaceEmbedding(
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()
@@ -110,35 +320,35 @@ 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
  ]
@@ -150,27 +360,27 @@ def handle_query(query):
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
@@ -178,35 +388,36 @@ async def save_chat_history(history: dict):
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)
@@ -226,6 +437,7 @@ async def chat(request: MessageRequest):
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"}
 
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
+ from fastapi import FastAPI, Request
236
  from fastapi.responses import HTMLResponse
237
  from fastapi.staticfiles import StaticFiles
238
  from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
239
+ from llama_index.llms.huggingface import HuggingFaceLLM
240
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
241
  from pydantic import BaseModel
242
  from fastapi.responses import JSONResponse
 
243
  import datetime
244
  from fastapi.middleware.cors import CORSMiddleware
245
  from fastapi.templating import Jinja2Templates
 
 
 
 
246
  from simple_salesforce import Salesforce, SalesforceLogin
247
+ from transformers import AutoModelForSeq2SeqLM
 
248
 
249
  # Define Pydantic model for incoming request body
250
  class MessageRequest(BaseModel):
251
  message: str
 
 
 
 
 
 
252
 
253
+ # Environment variables for Salesforce
254
+ os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN") # Optional: Needed for model download if gated
255
  username = os.getenv("username")
256
  password = os.getenv("password")
257
  security_token = os.getenv("security_token")
258
+ domain = os.getenv("domain") # Using sandbox environment
259
  session_id, sf_instance = SalesforceLogin(username=username, password=password, security_token=security_token, domain=domain)
260
 
261
+ # Create Salesforce object
262
  sf = Salesforce(instance=sf_instance, session_id=session_id)
263
 
264
  app = FastAPI()
265
 
 
266
  @app.middleware("http")
267
  async def add_security_headers(request: Request, call_next):
268
  response = await call_next(request)
 
270
  response.headers["X-Frame-Options"] = "ALLOWALL"
271
  return response
272
 
 
273
  # Allow CORS requests from any domain
274
  app.add_middleware(
275
  CORSMiddleware,
 
279
  allow_headers=["*"],
280
  )
281
 
 
 
 
282
  @app.get("/favicon.ico")
283
  async def favicon():
284
  return HTMLResponse("") # or serve a real favicon if you have one
285
 
 
286
  app.mount("/static", StaticFiles(directory="static"), name="static")
287
 
288
  templates = Jinja2Templates(directory="static")
289
+
 
 
 
 
 
 
 
 
290
  # Configure Llama index settings
291
  Settings.llm = HuggingFaceLLM(
292
  model_name="google/flan-t5-small",
 
294
  context_window=512, # flan-t5-small has a max context window of 512 tokens
295
  max_new_tokens=256,
296
  generate_kwargs={"temperature": 0.1, "do_sample": True},
297
+ model=AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small"),
298
  device_map="auto" # Automatically use GPU if available, else CPU
299
  )
300
  Settings.embed_model = HuggingFaceEmbedding(
 
302
  )
303
 
304
  PERSIST_DIR = "db"
305
+ PDF_DIRECTORY = "data"
306
 
307
  # Ensure directories exist
308
  os.makedirs(PDF_DIRECTORY, exist_ok=True)
309
  os.makedirs(PERSIST_DIR, exist_ok=True)
310
  chat_history = []
311
  current_chat_history = []
312
+
313
  def data_ingestion_from_directory():
314
  documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
315
  storage_context = StorageContext.from_defaults()
 
320
  start_time = time.time()
321
  data_ingestion_from_directory() # Process PDF ingestion at startup
322
  print(f"Data ingestion time: {time.time() - start_time} seconds")
323
+
324
  def split_name(full_name):
325
  # Split the name by spaces
326
  words = full_name.strip().split()
327
 
328
  # Logic for determining first name and last name
329
  if len(words) == 1:
330
+ first_name = ""
331
  last_name = words[0]
332
  elif len(words) == 2:
333
  first_name = words[0]
334
  last_name = words[1]
335
  else:
336
  first_name = words[0]
337
+ last_name = " ".join(words[1:])
338
 
339
  return first_name, last_name
340
 
341
  initialize() # Run initialization tasks
342
 
 
343
  def handle_query(query):
344
  chat_text_qa_msgs = [
345
  (
346
  "user",
347
  """
348
+ You are Clara, a Redfernstech chatbot. Provide accurate, concise answers (10-15 words) based on company data.
349
+ Context: {context_str}
350
+ Question: {query_str}
351
+ Answer:
352
  """
353
  )
354
  ]
 
360
  for past_query, response in reversed(current_chat_history):
361
  if past_query.strip():
362
  context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
 
363
 
364
  query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
365
  answer = query_engine.query(query)
366
 
367
+ if hasattr(answer, "response"):
368
+ response = answer.response
369
+ elif isinstance(answer, dict) and "response" in answer:
370
+ response = answer["response"]
371
  else:
372
+ response = "Sorry, I couldn't find an answer."
373
  current_chat_history.append((query, response))
374
  return response
375
+
376
  @app.get("/ch/{id}", response_class=HTMLResponse)
377
  async def load_chat(request: Request, id: str):
378
  return templates.TemplateResponse("index.html", {"request": request, "user_id": id})
379
+
380
  @app.post("/hist/")
381
  async def save_chat_history(history: dict):
382
  # Check if 'userId' is present in the incoming dictionary
383
+ user_id = history.get("userId")
384
  print(user_id)
385
 
386
  # Ensure user_id is defined before proceeding
 
388
  return {"error": "userId is required"}, 400
389
 
390
  # Construct the chat history string
391
+ hist = "".join([f"'{entry['sender']}: {entry['message']}'\n" for entry in history["history"]])
392
  hist = "You are a Redfernstech summarize model. Your aim is to use this conversation to identify user interests solely based on that conversation: " + hist
393
  print(hist)
394
 
395
+ # Get the summarized result
396
  result = hist
397
 
398
  try:
399
+ sf.Lead.update(user_id, {"Description": result})
400
  except Exception as e:
401
  return {"error": f"Failed to update lead: {str(e)}"}, 500
402
 
403
  return {"summary": result, "message": "Chat history saved"}
404
+
405
  @app.post("/webhook")
406
  async def receive_form_data(request: Request):
407
  form_data = await request.json()
408
  # Log in to Salesforce
409
+ first_name, last_name = split_name(form_data["name"])
410
  data = {
411
+ "FirstName": first_name,
412
+ "LastName": last_name,
413
+ "Description": "hii", # Static description
414
+ "Company": form_data["company"], # Assuming company is available in form_data
415
+ "Phone": form_data["phone"].strip(), # Phone from form data
416
+ "Email": form_data["email"], # Email from form data
417
  }
418
+ a = sf.Lead.create(data)
419
  # Generate a unique ID (for tracking user)
420
+ unique_id = a["id"]
421
 
422
  # Here you can do something with form_data like saving it to a database
423
  print("Received form data:", form_data)
 
437
  }
438
  chat_history.append(message_data)
439
  return {"response": response}
440
+
441
  @app.get("/")
442
  def read_root():
443
+ return {"message": "Welcome to the API"}