Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,105 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
import mysql.connector
|
|
@@ -101,4 +201,4 @@ with gr.Blocks(js = js) as demo:
|
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
+
import json
|
| 4 |
+
import mysql.connector
|
| 5 |
+
|
| 6 |
+
def fetch_personalized_data(session_index):
|
| 7 |
+
# Connect to the database
|
| 8 |
+
conn = mysql.connector.connect(
|
| 9 |
+
host="18.153.94.89",
|
| 10 |
+
user="root",
|
| 11 |
+
password="N12RXMKtKxRj",
|
| 12 |
+
database="lionessdb"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Create a cursor object
|
| 16 |
+
cursor = conn.cursor()
|
| 17 |
+
|
| 18 |
+
# Replace the placeholders with your actual database and table names
|
| 19 |
+
core_table = "e5390g37096_core"
|
| 20 |
+
decisions_table = "e5390g37096_decisions"
|
| 21 |
+
|
| 22 |
+
# Query to fetch relevant data from both tables based on session_index
|
| 23 |
+
query = f"""
|
| 24 |
+
SELECT e5390g37096_core.playerNr,
|
| 25 |
+
e5390g37096_core.groupNr,
|
| 26 |
+
e5390g37096_core.subjectNr
|
| 27 |
+
FROM e5390g37096_core
|
| 28 |
+
JOIN e5390g37096_decisions ON
|
| 29 |
+
e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
|
| 30 |
+
WHERE e5390g37096_decisions.session_index = '{session_index}'
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
cursor.execute(query)
|
| 35 |
+
|
| 36 |
+
# Fetch all rows as lists of tuples
|
| 37 |
+
rows = cursor.fetchall()
|
| 38 |
+
|
| 39 |
+
# Close the database connection
|
| 40 |
+
conn.close()
|
| 41 |
+
|
| 42 |
+
# Convert the rows to a list of dictionaries
|
| 43 |
+
data = [{'playerNr': row[0], 'groupNr': row[1], 'subjectNr': row[2]} for row in rows]
|
| 44 |
+
return data
|
| 45 |
+
|
| 46 |
+
except mysql.connector.Error as err:
|
| 47 |
+
print(f"Error: {err}")
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
js = """
|
| 51 |
+
function() {
|
| 52 |
+
const params = new URLSearchParams(window.location.search);
|
| 53 |
+
const url_params = Object.fromEntries(params);
|
| 54 |
+
return url_params;
|
| 55 |
+
}
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(js=js) as demo:
|
| 59 |
+
gr.Markdown("""## Gradio send queryparam to chatbot
|
| 60 |
+
Type `hi`
|
| 61 |
+
""")
|
| 62 |
+
url_params = gr.JSON({}, visible=False, label="URL Params")
|
| 63 |
+
chatbot = gr.Chatbot().style(height=500)
|
| 64 |
+
msg = gr.Textbox()
|
| 65 |
+
clear = gr.Button("Clear")
|
| 66 |
+
|
| 67 |
+
def user(user_message, url_params, history):
|
| 68 |
+
return "", history + [[user_message, None]]
|
| 69 |
+
|
| 70 |
+
def bot(history, url_params):
|
| 71 |
+
if "hi" in history[-1][0]:
|
| 72 |
+
session_index = url_params.get('session_index')
|
| 73 |
+
if session_index: # Check if session_index is not None or empty
|
| 74 |
+
bot_message = f"""
|
| 75 |
+
Here are your URL params:
|
| 76 |
+
{json.dumps(url_params, indent=4)}
|
| 77 |
+
"""
|
| 78 |
+
# Fetch personalized data
|
| 79 |
+
personalized_data = fetch_personalized_data(session_index)
|
| 80 |
+
if personalized_data:
|
| 81 |
+
bot_message += f"\n\nHere are your personalized data:\n{personalized_data}"
|
| 82 |
+
else:
|
| 83 |
+
bot_message += "\n\nNo personalized data found for the given session index."
|
| 84 |
+
else:
|
| 85 |
+
bot_message = "Session index is missing."
|
| 86 |
+
else:
|
| 87 |
+
bot_message = "There was an error. Please try again."
|
| 88 |
+
history[-1][1] = bot_message
|
| 89 |
+
time.sleep(1)
|
| 90 |
+
return history
|
| 91 |
+
|
| 92 |
+
msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 93 |
+
fn=bot, inputs=[chatbot, url_params], outputs=[chatbot]
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 97 |
+
|
| 98 |
+
demo.launch()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
'''import gradio as gr
|
| 102 |
+
import time
|
| 103 |
import random
|
| 104 |
import json
|
| 105 |
import mysql.connector
|
|
|
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
+
demo.launch()'''
|