Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,8 +76,6 @@ with gr.Blocks() as demo:
|
|
| 76 |
chatbot = gr.Chatbot().style(height=500)
|
| 77 |
msg = gr.Textbox()
|
| 78 |
clear = gr.Button("Clear")
|
| 79 |
-
session_index = url_params.get('session_index')
|
| 80 |
-
print(session_index)
|
| 81 |
|
| 82 |
def user(user_message, url_params, history):
|
| 83 |
return "", history + [[user_message, None]]
|
|
@@ -90,6 +88,7 @@ with gr.Blocks() as demo:
|
|
| 90 |
here your URL params:
|
| 91 |
{json.dumps(url_params, indent=4)}
|
| 92 |
"""
|
|
|
|
| 93 |
elif "print data" in history[-1][0]:
|
| 94 |
personalized_data = fetch_personalized_data()
|
| 95 |
print(personalized_data)
|
|
@@ -99,8 +98,19 @@ with gr.Blocks() as demo:
|
|
| 99 |
history[-1][1] = bot_message
|
| 100 |
time.sleep(1)
|
| 101 |
return history
|
| 102 |
-
|
| 103 |
def fetch_personalized_data():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# Replace the placeholders with your actual database and table names
|
| 105 |
core_table = "e5390g37096_core"
|
| 106 |
decisions_table = "e5390g37096_decisions"
|
|
@@ -108,6 +118,81 @@ with gr.Blocks() as demo:
|
|
| 108 |
print(session_index)
|
| 109 |
playerNr = "1"
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
# Connect to the database
|
| 112 |
conn = mysql.connector.connect(
|
| 113 |
host="18.153.94.89",
|
|
@@ -118,7 +203,12 @@ with gr.Blocks() as demo:
|
|
| 118 |
|
| 119 |
# Create a cursor object
|
| 120 |
cursor = conn.cursor()
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Query to fetch relevant data from both tables based on session_index
|
| 123 |
query = f"""
|
| 124 |
SELECT
|
|
@@ -169,6 +259,7 @@ with gr.Blocks() as demo:
|
|
| 169 |
|
| 170 |
# Print or use the json_result variable as needed
|
| 171 |
# print(personalized_data)
|
|
|
|
| 172 |
|
| 173 |
''' def bot(history, url_params):
|
| 174 |
global session_index
|
|
@@ -187,19 +278,4 @@ with gr.Blocks() as demo:
|
|
| 187 |
history[-1][1] = bot_message
|
| 188 |
time.sleep(1)
|
| 189 |
return history
|
| 190 |
-
'''
|
| 191 |
-
|
| 192 |
-
msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 193 |
-
fn=bot, inputs=[chatbot, url_params], outputs=[chatbot]
|
| 194 |
-
)
|
| 195 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 196 |
-
|
| 197 |
-
demo.load(
|
| 198 |
-
fn=lambda x: x,
|
| 199 |
-
inputs=[url_params],
|
| 200 |
-
outputs=[url_params],
|
| 201 |
-
_js=get_window_url_params,
|
| 202 |
-
queue=False
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
demo.launch()
|
|
|
|
| 76 |
chatbot = gr.Chatbot().style(height=500)
|
| 77 |
msg = gr.Textbox()
|
| 78 |
clear = gr.Button("Clear")
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def user(user_message, url_params, history):
|
| 81 |
return "", history + [[user_message, None]]
|
|
|
|
| 88 |
here your URL params:
|
| 89 |
{json.dumps(url_params, indent=4)}
|
| 90 |
"""
|
| 91 |
+
|
| 92 |
elif "print data" in history[-1][0]:
|
| 93 |
personalized_data = fetch_personalized_data()
|
| 94 |
print(personalized_data)
|
|
|
|
| 98 |
history[-1][1] = bot_message
|
| 99 |
time.sleep(1)
|
| 100 |
return history
|
| 101 |
+
|
| 102 |
def fetch_personalized_data():
|
| 103 |
+
# Connect to the database
|
| 104 |
+
conn = mysql.connector.connect(
|
| 105 |
+
host="18.153.94.89",
|
| 106 |
+
user="root",
|
| 107 |
+
password="N12RXMKtKxRj",
|
| 108 |
+
database="lionessdb"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Create a cursor object
|
| 112 |
+
cursor = conn.cursor()
|
| 113 |
+
|
| 114 |
# Replace the placeholders with your actual database and table names
|
| 115 |
core_table = "e5390g37096_core"
|
| 116 |
decisions_table = "e5390g37096_decisions"
|
|
|
|
| 118 |
print(session_index)
|
| 119 |
playerNr = "1"
|
| 120 |
|
| 121 |
+
# Query to fetch relevant data from both tables based on session_index
|
| 122 |
+
query = f"""
|
| 123 |
+
SELECT
|
| 124 |
+
{core_table}.playerNr,
|
| 125 |
+
{core_table}.subjectNr,
|
| 126 |
+
{core_table}.onPage,
|
| 127 |
+
{decisions_table}.transfer1,
|
| 128 |
+
{decisions_table}.tripledAmount1,
|
| 129 |
+
{decisions_table}.keptForSelf1,
|
| 130 |
+
{decisions_table}.returned1,
|
| 131 |
+
{decisions_table}.newCreditRound2,
|
| 132 |
+
{decisions_table}.transfer2,
|
| 133 |
+
{decisions_table}.tripledAmount2,
|
| 134 |
+
{decisions_table}.keptForSelf2,
|
| 135 |
+
{decisions_table}.returned2,
|
| 136 |
+
{decisions_table}.resultsWithoutAI
|
| 137 |
+
FROM {core_table}
|
| 138 |
+
JOIN {decisions_table} ON
|
| 139 |
+
{core_table}.playerNr = {decisions_table}.playerNr AND
|
| 140 |
+
{core_table}.groupNr = {decisions_table}.groupNr AND
|
| 141 |
+
{core_table}.subjectNr = {decisions_table}.subjectNr
|
| 142 |
+
WHERE {decisions_table}.session_index = '{session_index}'
|
| 143 |
+
"""
|
| 144 |
+
cursor.execute(query)
|
| 145 |
+
|
| 146 |
+
# Fetch all rows and convert to a list of dictionaries
|
| 147 |
+
rows = cursor.fetchall()
|
| 148 |
+
result = []
|
| 149 |
+
for row in rows:
|
| 150 |
+
d = {}
|
| 151 |
+
for i, col in enumerate(cursor.description):
|
| 152 |
+
d[col[0]] = row[i]
|
| 153 |
+
result.append(d)
|
| 154 |
+
|
| 155 |
+
# Convert the list of dictionaries to JSON
|
| 156 |
+
personalized_result = json.dumps(result)
|
| 157 |
+
|
| 158 |
+
# Close the database connection
|
| 159 |
+
conn.close()
|
| 160 |
+
|
| 161 |
+
print(personalized_result)
|
| 162 |
+
|
| 163 |
+
# Return the JSON result
|
| 164 |
+
return personalized_result
|
| 165 |
+
|
| 166 |
+
# Call the function to fetch data as JSON
|
| 167 |
+
# personalized_data = fetch_personalized_data()
|
| 168 |
+
|
| 169 |
+
# Print or use the json_result variable as needed
|
| 170 |
+
# print(personalized_data)
|
| 171 |
+
|
| 172 |
+
msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 173 |
+
fn=bot, inputs=[chatbot, url_params], outputs=[chatbot]
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 177 |
+
|
| 178 |
+
demo.load(
|
| 179 |
+
fn=lambda x: x,
|
| 180 |
+
inputs=[url_params],
|
| 181 |
+
outputs=[url_params],
|
| 182 |
+
_js=get_window_url_params,
|
| 183 |
+
queue=False
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
demo.launch()
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
'''def fetch_personalized_data():
|
| 196 |
# Connect to the database
|
| 197 |
conn = mysql.connector.connect(
|
| 198 |
host="18.153.94.89",
|
|
|
|
| 203 |
|
| 204 |
# Create a cursor object
|
| 205 |
cursor = conn.cursor()
|
| 206 |
+
|
| 207 |
+
# Replace the placeholders with your actual database and table names
|
| 208 |
+
core_table = "e5390g37096_core"
|
| 209 |
+
decisions_table = "e5390g37096_decisions"
|
| 210 |
+
playerNr = "1"
|
| 211 |
+
|
| 212 |
# Query to fetch relevant data from both tables based on session_index
|
| 213 |
query = f"""
|
| 214 |
SELECT
|
|
|
|
| 259 |
|
| 260 |
# Print or use the json_result variable as needed
|
| 261 |
# print(personalized_data)
|
| 262 |
+
'''
|
| 263 |
|
| 264 |
''' def bot(history, url_params):
|
| 265 |
global session_index
|
|
|
|
| 278 |
history[-1][1] = bot_message
|
| 279 |
time.sleep(1)
|
| 280 |
return history
|
| 281 |
+
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|