Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from langchain.embeddings.openai import OpenAIEmbeddings
|
|
| 12 |
|
| 13 |
|
| 14 |
from langchain.vectorstores import Chroma
|
| 15 |
-
|
| 16 |
|
| 17 |
chat = ChatOpenAI(model="gpt-4o-mini")
|
| 18 |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
@@ -20,7 +20,62 @@ OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
| 20 |
|
| 21 |
embedding_function = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY, model="text-embedding-3-large")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
|
|
@@ -66,7 +121,7 @@ def answer_query(message, chat_history):
|
|
| 66 |
|
| 67 |
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
-
gr.HTML("<h1 align = 'center'>ChatGPT augmented with
|
| 70 |
gr.Markdown("""## 1. Input: 🔍
|
| 71 |
**Define Your Workshop Objective.**
|
| 72 |
Choose a topic that is timely and fills a skill gap relevant to your consulting firm’s strategic goals.
|
|
@@ -84,9 +139,9 @@ with gr.Blocks() as demo:
|
|
| 84 |
|
| 85 |
chatbot = gr.Chatbot()
|
| 86 |
msg = gr.Textbox(label = "Enter your question here")
|
| 87 |
-
msg.submit(answer_query,[msg,chatbot],[msg,chatbot])
|
| 88 |
submitChat = gr.Button("Submit message")
|
| 89 |
-
submitChat.click(answer_query,[msg,chatbot],[msg,chatbot])
|
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
from langchain.vectorstores import Chroma
|
| 15 |
+
user_id = None
|
| 16 |
|
| 17 |
chat = ChatOpenAI(model="gpt-4o-mini")
|
| 18 |
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
|
|
| 20 |
|
| 21 |
embedding_function = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY, model="text-embedding-3-large")
|
| 22 |
|
| 23 |
+
def track_user_interaction(user_input, action):
|
| 24 |
+
global user_id
|
| 25 |
+
print(user_input)
|
| 26 |
+
|
| 27 |
+
user_id = str(user_id)
|
| 28 |
+
|
| 29 |
|
| 30 |
+
# Construct connection string
|
| 31 |
+
from mysql.connector import errorcode
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
connection = mysql.connector.connect(user=os.getenv("MYSQLUSER"), password= os.getenv("MYSQLPASSWORD"), host=os.getenv("DBHOST"), port=3306, database="user_interact")
|
| 35 |
+
print("Connection established")
|
| 36 |
+
except mysql.connector.Error as err:
|
| 37 |
+
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
| 38 |
+
print("Something is wrong with the user name or password")
|
| 39 |
+
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
| 40 |
+
print("Database does not exist")
|
| 41 |
+
else:
|
| 42 |
+
print(err)
|
| 43 |
+
|
| 44 |
+
cursor = connection.cursor()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# Create table if it doesn't exist
|
| 48 |
+
cursor.execute('''
|
| 49 |
+
CREATE TABLE IF NOT EXISTS user_interactions (
|
| 50 |
+
user_input LONGTEXT NOT NULL,
|
| 51 |
+
action TEXT NOT NULL,
|
| 52 |
+
timestamp TEXT NOT NULL,
|
| 53 |
+
user_id TEXT NOT NULL
|
| 54 |
+
);
|
| 55 |
+
''')
|
| 56 |
+
# Prepare data
|
| 57 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
+
user_input_str = str(user_input)
|
| 59 |
+
action_str = str(action)
|
| 60 |
+
|
| 61 |
+
# Use a parameterized query to insert data
|
| 62 |
+
insert_query = """
|
| 63 |
+
INSERT INTO user_interactions (user_input, action, timestamp, user_id)
|
| 64 |
+
VALUES (%s, %s, %s, %s)
|
| 65 |
+
"""
|
| 66 |
+
cursor.execute(insert_query, (user_input_str, action_str, timestamp, user_id))
|
| 67 |
+
# Commit changes and close connection
|
| 68 |
+
connection.commit()
|
| 69 |
+
connection.close()
|
| 70 |
+
|
| 71 |
+
def profile_user(username, password):
|
| 72 |
+
global user_id
|
| 73 |
+
user_id = username
|
| 74 |
+
track_user_interaction("", "login")
|
| 75 |
+
if password == os.getenv("APP_PASSWORD"):
|
| 76 |
+
return True
|
| 77 |
+
else:
|
| 78 |
+
return False
|
| 79 |
|
| 80 |
|
| 81 |
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
with gr.Blocks() as demo:
|
| 124 |
+
gr.HTML("<h1 align = 'center'>ChatGPT augmented with Cyber Security Knowledge</h1>")
|
| 125 |
gr.Markdown("""## 1. Input: 🔍
|
| 126 |
**Define Your Workshop Objective.**
|
| 127 |
Choose a topic that is timely and fills a skill gap relevant to your consulting firm’s strategic goals.
|
|
|
|
| 139 |
|
| 140 |
chatbot = gr.Chatbot()
|
| 141 |
msg = gr.Textbox(label = "Enter your question here")
|
| 142 |
+
msg.submit(answer_query,[msg,chatbot],[msg,chatbot]).then(track_user_interaction, inputs=[msg, gr.Textbox("promptChatGPT", visible=False)].then(track_user_interaction, inputs=[chatbot, gr.Textbox("ChatGPTreply", visible=False)]
|
| 143 |
submitChat = gr.Button("Submit message")
|
| 144 |
+
submitChat.click(answer_query,[msg,chatbot],[msg,chatbot]).then(track_user_interaction, inputs=[msg, gr.Textbox("promptChatGPT", visible=False)].then(track_user_interaction, inputs=[chatbot, gr.Textbox("ChatGPTreply", visible=False)]
|
| 145 |
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|