Spaces:
Runtime error
Runtime error
Srinivasulu kethanaboina
commited on
Commit
•
f941775
1
Parent(s):
4dc9ddf
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,6 @@ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
|
7 |
from simple_salesforce import Salesforce, SalesforceLogin
|
8 |
import random
|
9 |
import datetime
|
10 |
-
import uuid
|
11 |
-
import json
|
12 |
|
13 |
# Load environment variables
|
14 |
load_dotenv()
|
@@ -34,8 +32,8 @@ PDF_DIRECTORY = 'data'
|
|
34 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
35 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
36 |
|
37 |
-
# Variable to store current chat conversation
|
38 |
-
current_chat_history =
|
39 |
kkk = random.choice(['Clara', 'Lily'])
|
40 |
|
41 |
def data_ingestion_from_directory():
|
@@ -65,7 +63,7 @@ def handle_query(query):
|
|
65 |
|
66 |
# Use chat history to enhance response
|
67 |
context_str = ""
|
68 |
-
for past_query, response in reversed(current_chat_history):
|
69 |
if past_query.strip():
|
70 |
context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
71 |
|
@@ -79,38 +77,33 @@ def handle_query(query):
|
|
79 |
else:
|
80 |
response = "Sorry, I couldn't find an answer."
|
81 |
|
82 |
-
# Update current chat history
|
83 |
-
|
|
|
84 |
|
85 |
return response
|
86 |
|
87 |
-
def
|
88 |
-
# Save the chat history to a local file or Firebase
|
89 |
-
session_id = str(uuid.uuid4())
|
90 |
-
chat_history_path = f"chat_history_{session_id}.json"
|
91 |
-
|
92 |
-
with open(chat_history_path, 'w') as f:
|
93 |
-
json.dump(history, f)
|
94 |
-
print(f"Chat history saved as {chat_history_path}")
|
95 |
-
|
96 |
-
# Save to Salesforce
|
97 |
-
save_to_salesforce(current_chat_history)
|
98 |
-
|
99 |
-
def save_to_salesforce(history):
|
100 |
username =os.getenv("username")
|
101 |
password =os.getenv("password")
|
102 |
security_token =os.getenv("security_token")
|
103 |
-
domain = 'test'
|
104 |
|
|
|
105 |
session_id, sf_instance = SalesforceLogin(username=username, password=password, security_token=security_token, domain=domain)
|
|
|
|
|
106 |
sf = Salesforce(instance=sf_instance, session_id=session_id)
|
107 |
-
|
|
|
|
|
108 |
data = {
|
109 |
'Name': 'Chat with user',
|
110 |
-
'Bot_Message__c':
|
111 |
-
'User_Message__c':
|
112 |
'Date__c': str(datetime.datetime.now().date())
|
113 |
}
|
|
|
114 |
sf.Chat_History__c.create(data)
|
115 |
|
116 |
# Define the function to handle predictions
|
@@ -123,9 +116,7 @@ def predict(message, history):
|
|
123 |
response = handle_query(message)
|
124 |
response_with_logo = f'<div class="response-with-logo">{logo_html}<div class="response-text">{response}</div></div>'
|
125 |
|
126 |
-
#
|
127 |
-
save_chat_history(current_chat_history)
|
128 |
-
|
129 |
return response_with_logo
|
130 |
|
131 |
# Define your Gradio chat interface function
|
@@ -134,9 +125,6 @@ def chat_interface(message, history):
|
|
134 |
# Process the user message and generate a response
|
135 |
response = handle_query(message)
|
136 |
|
137 |
-
# Update the history and save it
|
138 |
-
save_chat_history(current_chat_history)
|
139 |
-
|
140 |
# Return the bot response
|
141 |
return response
|
142 |
except Exception as e:
|
@@ -172,14 +160,13 @@ div.svelte-rk35yg {display: none;}
|
|
172 |
div.progress-text.svelte-z7cif2.meta-text {display: none;}
|
173 |
'''
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
gr.Button("Close Chat").click(fn=save_chat_history)
|
183 |
|
184 |
-
# Launch the interface
|
185 |
demo.launch()
|
|
|
7 |
from simple_salesforce import Salesforce, SalesforceLogin
|
8 |
import random
|
9 |
import datetime
|
|
|
|
|
10 |
|
11 |
# Load environment variables
|
12 |
load_dotenv()
|
|
|
32 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
33 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
34 |
|
35 |
+
# Variable to store current chat conversation in a dictionary
|
36 |
+
current_chat_history = {}
|
37 |
kkk = random.choice(['Clara', 'Lily'])
|
38 |
|
39 |
def data_ingestion_from_directory():
|
|
|
63 |
|
64 |
# Use chat history to enhance response
|
65 |
context_str = ""
|
66 |
+
for past_query, response in reversed(current_chat_history.values()):
|
67 |
if past_query.strip():
|
68 |
context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
69 |
|
|
|
77 |
else:
|
78 |
response = "Sorry, I couldn't find an answer."
|
79 |
|
80 |
+
# Update current chat history dictionary (use unique ID as key)
|
81 |
+
chat_id = str(datetime.datetime.now().timestamp())
|
82 |
+
current_chat_history[chat_id] = (query, response)
|
83 |
|
84 |
return response
|
85 |
|
86 |
+
def save_chat_history_to_salesforce():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
username =os.getenv("username")
|
88 |
password =os.getenv("password")
|
89 |
security_token =os.getenv("security_token")
|
90 |
+
domain = 'test'
|
91 |
|
92 |
+
# Log in to Salesforce
|
93 |
session_id, sf_instance = SalesforceLogin(username=username, password=password, security_token=security_token, domain=domain)
|
94 |
+
|
95 |
+
# Create Salesforce object
|
96 |
sf = Salesforce(instance=sf_instance, session_id=session_id)
|
97 |
+
|
98 |
+
# Iterate over chat history dictionary and push to Salesforce
|
99 |
+
for chat_id, (user_message, bot_response) in current_chat_history.items():
|
100 |
data = {
|
101 |
'Name': 'Chat with user',
|
102 |
+
'Bot_Message__c': bot_response,
|
103 |
+
'User_Message__c': user_message,
|
104 |
'Date__c': str(datetime.datetime.now().date())
|
105 |
}
|
106 |
+
# Insert into the custom object (replace 'Chat_History__c' with your custom object's API name)
|
107 |
sf.Chat_History__c.create(data)
|
108 |
|
109 |
# Define the function to handle predictions
|
|
|
116 |
response = handle_query(message)
|
117 |
response_with_logo = f'<div class="response-with-logo">{logo_html}<div class="response-text">{response}</div></div>'
|
118 |
|
119 |
+
# Return the response with the logo
|
|
|
|
|
120 |
return response_with_logo
|
121 |
|
122 |
# Define your Gradio chat interface function
|
|
|
125 |
# Process the user message and generate a response
|
126 |
response = handle_query(message)
|
127 |
|
|
|
|
|
|
|
128 |
# Return the bot response
|
129 |
return response
|
130 |
except Exception as e:
|
|
|
160 |
div.progress-text.svelte-z7cif2.meta-text {display: none;}
|
161 |
'''
|
162 |
|
163 |
+
# Use Gradio Blocks to wrap components
|
164 |
+
with gr.Blocks() as demo:
|
165 |
+
chat = gr.ChatInterface(chat_interface, css=css, description="Lily", clear_btn=None, undo_btn=None, retry_btn=None)
|
166 |
+
|
167 |
+
# Add a button to save chat history
|
168 |
+
save_button = gr.Button("Save History")
|
169 |
+
save_button.click(fn=save_chat_history_to_salesforce)
|
|
|
170 |
|
171 |
+
# Launch the Gradio interface
|
172 |
demo.launch()
|