Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,27 +3,49 @@ import time
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
import mysql.connector
|
| 6 |
-
import os
|
| 7 |
-
import csv
|
| 8 |
-
from datetime import datetime
|
| 9 |
-
from huggingface_hub import Repository, hf_hub_download
|
| 10 |
-
|
| 11 |
-
get_window_url_params = """
|
| 12 |
-
function() {
|
| 13 |
-
const params = new URLSearchParams(window.location.search);
|
| 14 |
-
const url_params = Object.fromEntries(params);
|
| 15 |
-
return url_params;
|
| 16 |
-
}
|
| 17 |
-
"""
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
with gr.Blocks() as demo:
|
| 28 |
gr.Markdown("""## Gradio send queryparam to chatbot
|
| 29 |
type `read query`
|
|
@@ -44,63 +66,32 @@ with gr.Blocks() as demo:
|
|
| 44 |
here your URL params:
|
| 45 |
{json.dumps(url_params, indent=4)}
|
| 46 |
"""
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Create a cursor object
|
| 56 |
-
cursor = conn.cursor(dictionary=True) # Use dictionary cursor for easier data manipulation
|
| 57 |
-
|
| 58 |
-
# Replace the placeholders with your actual database and table names
|
| 59 |
-
core_table = "e5390g37096_core"
|
| 60 |
-
decisions_table = "e5390g37096_decisions"
|
| 61 |
-
|
| 62 |
-
# Query to fetch relevant data from both tables based on session_index
|
| 63 |
-
query = f"""
|
| 64 |
-
SELECT e5390g37096_core.playerNr,
|
| 65 |
-
e5390g37096_core.groupNr,
|
| 66 |
-
e5390g37096_core.subjectNr
|
| 67 |
-
FROM e5390g37096_core
|
| 68 |
-
JOIN e5390g37096_decisions ON
|
| 69 |
-
e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
|
| 70 |
-
WHERE e5390g37096_decisions.session_index = '{session_index}'
|
| 71 |
-
"""
|
| 72 |
-
|
| 73 |
-
try:
|
| 74 |
-
cursor.execute(query)
|
| 75 |
-
|
| 76 |
-
# Fetch all rows as lists of tuples
|
| 77 |
-
rows = cursor.fetchall()
|
| 78 |
-
|
| 79 |
-
# Close the database connection
|
| 80 |
-
conn.close()
|
| 81 |
-
|
| 82 |
-
return [list(row) for row in rows] # Convert each row to a list
|
| 83 |
-
|
| 84 |
-
except mysql.connector.Error as err:
|
| 85 |
-
print(f"Error: {err}")
|
| 86 |
-
return None
|
| 87 |
|
| 88 |
elif "print data" in history[-1][0]:
|
| 89 |
personalized_data = fetch_personalized_data()
|
| 90 |
print(personalized_data)
|
| 91 |
bot_message = "Data printed."
|
|
|
|
| 92 |
else:
|
| 93 |
bot_message = random.choice(["Yes", "No"])
|
| 94 |
history[-1][1] = bot_message
|
| 95 |
time.sleep(1)
|
| 96 |
-
return history
|
| 97 |
|
| 98 |
msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 99 |
fn=bot, inputs=[chatbot, url_params], outputs=[chatbot]
|
| 100 |
)
|
| 101 |
-
|
| 102 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 103 |
-
|
| 104 |
demo.load(
|
| 105 |
fn=lambda x: x,
|
| 106 |
inputs=[url_params],
|
|
@@ -108,5 +99,5 @@ with gr.Blocks() as demo:
|
|
| 108 |
_js=get_window_url_params,
|
| 109 |
queue=False
|
| 110 |
)
|
| 111 |
-
|
| 112 |
-
demo.launch()
|
|
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
import mysql.connector
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def fetch_personalized_data(session_index):
|
| 8 |
+
# Connect to the database
|
| 9 |
+
conn = mysql.connector.connect(
|
| 10 |
+
host="18.153.94.89",
|
| 11 |
+
user="root",
|
| 12 |
+
password="N12RXMKtKxRj",
|
| 13 |
+
database="lionessdb"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Create a cursor object
|
| 17 |
+
cursor = conn.cursor()
|
| 18 |
+
|
| 19 |
+
# Replace the placeholders with your actual database and table names
|
| 20 |
+
core_table = "e5390g37096_core"
|
| 21 |
+
decisions_table = "e5390g37096_decisions"
|
| 22 |
+
|
| 23 |
+
# Query to fetch relevant data from both tables based on session_index
|
| 24 |
+
query = f"""
|
| 25 |
+
SELECT e5390g37096_core.playerNr,
|
| 26 |
+
e5390g37096_core.groupNr,
|
| 27 |
+
e5390g37096_core.subjectNr
|
| 28 |
+
FROM e5390g37096_core
|
| 29 |
+
JOIN e5390g37096_decisions ON
|
| 30 |
+
e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
|
| 31 |
+
WHERE e5390g37096_decisions.session_index = '{session_index}'
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
try:
|
| 35 |
+
cursor.execute(query)
|
| 36 |
+
|
| 37 |
+
# Fetch all rows as lists of tuples
|
| 38 |
+
rows = cursor.fetchall()
|
| 39 |
+
|
| 40 |
+
# Close the database connection
|
| 41 |
+
conn.close()
|
| 42 |
+
|
| 43 |
+
return [list(row) for row in rows] # Convert each row to a list
|
| 44 |
+
|
| 45 |
+
except mysql.connector.Error as err:
|
| 46 |
+
print(f"Error: {err}")
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
gr.Markdown("""## Gradio send queryparam to chatbot
|
| 51 |
type `read query`
|
|
|
|
| 66 |
here your URL params:
|
| 67 |
{json.dumps(url_params, indent=4)}
|
| 68 |
"""
|
| 69 |
+
# Fetch personalized data
|
| 70 |
+
personalized_data = fetch_personalized_data(session_index)
|
| 71 |
+
if personalized_data:
|
| 72 |
+
bot_message = "Personalized data fetched successfully."
|
| 73 |
+
return [[bot_message, personalized_data]]
|
| 74 |
+
else:
|
| 75 |
+
bot_message = "Error fetching personalized data."
|
| 76 |
+
return [[bot_message, None]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
elif "print data" in history[-1][0]:
|
| 79 |
personalized_data = fetch_personalized_data()
|
| 80 |
print(personalized_data)
|
| 81 |
bot_message = "Data printed."
|
| 82 |
+
return [[bot_message, None]]
|
| 83 |
else:
|
| 84 |
bot_message = random.choice(["Yes", "No"])
|
| 85 |
history[-1][1] = bot_message
|
| 86 |
time.sleep(1)
|
| 87 |
+
return history
|
| 88 |
|
| 89 |
msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 90 |
fn=bot, inputs=[chatbot, url_params], outputs=[chatbot]
|
| 91 |
)
|
| 92 |
+
|
| 93 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 94 |
+
|
| 95 |
demo.load(
|
| 96 |
fn=lambda x: x,
|
| 97 |
inputs=[url_params],
|
|
|
|
| 99 |
_js=get_window_url_params,
|
| 100 |
queue=False
|
| 101 |
)
|
| 102 |
+
|
| 103 |
+
demo.launch()
|