Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ from fastapi.templating import Jinja2Templates
|
|
| 15 |
from huggingface_hub import InferenceClient
|
| 16 |
import json
|
| 17 |
import re
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# Define Pydantic model for incoming request body
|
|
@@ -55,8 +56,19 @@ def summarize_conversation(inference_client: InferenceClient, history: list):
|
|
| 55 |
|
| 56 |
|
| 57 |
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
app = FastAPI()
|
| 59 |
|
|
|
|
| 60 |
@app.middleware("http")
|
| 61 |
async def add_security_headers(request: Request, call_next):
|
| 62 |
response = await call_next(request)
|
|
@@ -116,6 +128,22 @@ def initialize():
|
|
| 116 |
start_time = time.time()
|
| 117 |
data_ingestion_from_directory() # Process PDF ingestion at startup
|
| 118 |
print(f"Data ingestion time: {time.time() - start_time} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
initialize() # Run initialization tasks
|
| 121 |
|
|
@@ -166,6 +194,7 @@ async def save_chat_history(history: dict):
|
|
| 166 |
print("Received history:", history['history']) # Debugging line
|
| 167 |
cleaned_summary = summarize_conversation(llm_client, history['history'])
|
| 168 |
print("Cleaned summary:", cleaned_summary) # Debugging line
|
|
|
|
| 169 |
return {"summary": cleaned_summary, "message": "Chat history saved"}
|
| 170 |
else:
|
| 171 |
return JSONResponse(status_code=400, content={"message": "Invalid history format"})
|
|
@@ -173,6 +202,16 @@ async def save_chat_history(history: dict):
|
|
| 173 |
async def receive_form_data(request: Request):
|
| 174 |
form_data = await request.json()
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
# Generate a unique ID (for tracking user)
|
| 177 |
unique_id = str(uuid.uuid4())
|
| 178 |
|
|
@@ -180,7 +219,7 @@ async def receive_form_data(request: Request):
|
|
| 180 |
print("Received form data:", form_data)
|
| 181 |
|
| 182 |
# Send back the unique id to the frontend
|
| 183 |
-
return JSONResponse({"id":
|
| 184 |
|
| 185 |
@app.post("/chat/")
|
| 186 |
async def chat(request: MessageRequest):
|
|
|
|
| 15 |
from huggingface_hub import InferenceClient
|
| 16 |
import json
|
| 17 |
import re
|
| 18 |
+
from simple_salesforce import Salesforce, SalesforceLogin
|
| 19 |
|
| 20 |
|
| 21 |
# Define Pydantic model for incoming request body
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
| 59 |
+
username = os.getenv("username")
|
| 60 |
+
password = os.getenv("password")
|
| 61 |
+
security_token = os.getenv("security_token")
|
| 62 |
+
domain = os.getenv("domain")# Using sandbox environment
|
| 63 |
+
|
| 64 |
+
# Log in to Salesforce
|
| 65 |
+
session_id, sf_instance = SalesforceLogin(username=username, password=password, security_token=security_token, domain=domain)
|
| 66 |
+
|
| 67 |
+
# Create Salesforce object
|
| 68 |
+
sf = Salesforce(instance=sf_instance, session_id=session_id)
|
| 69 |
app = FastAPI()
|
| 70 |
|
| 71 |
+
|
| 72 |
@app.middleware("http")
|
| 73 |
async def add_security_headers(request: Request, call_next):
|
| 74 |
response = await call_next(request)
|
|
|
|
| 128 |
start_time = time.time()
|
| 129 |
data_ingestion_from_directory() # Process PDF ingestion at startup
|
| 130 |
print(f"Data ingestion time: {time.time() - start_time} seconds")
|
| 131 |
+
def split_name(full_name):
|
| 132 |
+
# Split the name by spaces
|
| 133 |
+
words = full_name.strip().split()
|
| 134 |
+
|
| 135 |
+
# Logic for determining first name and last name
|
| 136 |
+
if len(words) == 1:
|
| 137 |
+
first_name = ''
|
| 138 |
+
last_name = words[0]
|
| 139 |
+
elif len(words) == 2:
|
| 140 |
+
first_name = words[0]
|
| 141 |
+
last_name = words[1]
|
| 142 |
+
else:
|
| 143 |
+
first_name = words[0]
|
| 144 |
+
last_name = ' '.join(words[1:])
|
| 145 |
+
|
| 146 |
+
return first_name, last_name
|
| 147 |
|
| 148 |
initialize() # Run initialization tasks
|
| 149 |
|
|
|
|
| 194 |
print("Received history:", history['history']) # Debugging line
|
| 195 |
cleaned_summary = summarize_conversation(llm_client, history['history'])
|
| 196 |
print("Cleaned summary:", cleaned_summary) # Debugging line
|
| 197 |
+
sf.Lead.update(user_id,{'Description': cleaned_summary})
|
| 198 |
return {"summary": cleaned_summary, "message": "Chat history saved"}
|
| 199 |
else:
|
| 200 |
return JSONResponse(status_code=400, content={"message": "Invalid history format"})
|
|
|
|
| 202 |
async def receive_form_data(request: Request):
|
| 203 |
form_data = await request.json()
|
| 204 |
|
| 205 |
+
first_name, last_name = split_name(form_data['name'])
|
| 206 |
+
data = {
|
| 207 |
+
'FirstName': first_name,
|
| 208 |
+
'LastName': last_name,
|
| 209 |
+
'Description': 'hii', # Static description
|
| 210 |
+
'Company': form_data['company'], # Assuming company is available in form_data
|
| 211 |
+
'Phone': form_data['phone'].strip(), # Phone from form data
|
| 212 |
+
'Email': form_data['email'], # Email from form data
|
| 213 |
+
}
|
| 214 |
+
a=sf.Lead.create(data)
|
| 215 |
# Generate a unique ID (for tracking user)
|
| 216 |
unique_id = str(uuid.uuid4())
|
| 217 |
|
|
|
|
| 219 |
print("Received form data:", form_data)
|
| 220 |
|
| 221 |
# Send back the unique id to the frontend
|
| 222 |
+
return JSONResponse({"id": a})
|
| 223 |
|
| 224 |
@app.post("/chat/")
|
| 225 |
async def chat(request: MessageRequest):
|