Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import random
|
| 4 |
import json
|
| 5 |
|
|
@@ -102,3 +194,4 @@ with gr.Blocks() as demo:
|
|
| 102 |
)
|
| 103 |
|
| 104 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
+
import json
|
| 4 |
+
import mysql.connector
|
| 5 |
+
|
| 6 |
+
# fetch data from the decisions database
|
| 7 |
+
def fetch_data_as_json():
|
| 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 |
+
# Execute the SQL query
|
| 20 |
+
query = "SELECT playerNr, subjectNr, initialCredit, transfer1, tripledAmount1, keptForSelf1, returned1, " \
|
| 21 |
+
"newCreditRound2, transfer2, tripledAmount2, keptForSelf2, returned2, results2rounds, " \
|
| 22 |
+
"newCreditRound3, transfer3, tripledAmount3, keptForSelf3, returned3, results3rounds FROM e5390g37096_decisions"
|
| 23 |
+
cursor.execute(query)
|
| 24 |
+
|
| 25 |
+
# Fetch all rows and convert to a list of dictionaries
|
| 26 |
+
rows = cursor.fetchall()
|
| 27 |
+
result = []
|
| 28 |
+
for row in rows:
|
| 29 |
+
d = {}
|
| 30 |
+
for i, col in enumerate(cursor.description):
|
| 31 |
+
d[col[0]] = row[i]
|
| 32 |
+
result.append(d)
|
| 33 |
+
|
| 34 |
+
# Convert the list of dictionaries to JSON
|
| 35 |
+
json_result = json.dumps(result)
|
| 36 |
+
|
| 37 |
+
# Close the database connection
|
| 38 |
+
conn.close()
|
| 39 |
+
|
| 40 |
+
# Return the JSON result
|
| 41 |
+
return json_result
|
| 42 |
+
|
| 43 |
+
# Call the function to fetch data as JSON
|
| 44 |
+
json_result = fetch_data_as_json()
|
| 45 |
+
|
| 46 |
+
# Print or use the json_result variable as needed
|
| 47 |
+
print(json_result)
|
| 48 |
+
|
| 49 |
+
get_window_url_params = """
|
| 50 |
+
function() {
|
| 51 |
+
const params = new URLSearchParams(window.location.search);
|
| 52 |
+
const url_params = Object.fromEntries(params);
|
| 53 |
+
return url_params;
|
| 54 |
+
}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("""## Please insert 'read query' in order to get the URL Parameters from the chatbot""")
|
| 59 |
+
url_params = gr.JSON({}, visible=False, label="URL Params")
|
| 60 |
+
chatbot = gr.Chatbot().style(height=500)
|
| 61 |
+
msg = gr.Textbox()
|
| 62 |
+
clear = gr.Button("Clear")
|
| 63 |
+
|
| 64 |
+
def user(user_message, url_params, history):
|
| 65 |
+
return "", history + [[user_message, None]]
|
| 66 |
+
|
| 67 |
+
def bot(history, url_params, json_result):
|
| 68 |
+
if "read query" in history[-1][0]:
|
| 69 |
+
bot_message = f"""
|
| 70 |
+
Here are your URL params:
|
| 71 |
+
{json.dumps(url_params, indent=4)}
|
| 72 |
+
"""
|
| 73 |
+
else:
|
| 74 |
+
bot_message = "You did not ask for the URL Params :) But here is what I read from the database" + json_result
|
| 75 |
+
history[-1][1] = bot_message
|
| 76 |
+
time.sleep(1)
|
| 77 |
+
return history, url_params # Returning url_params to match the expected arguments
|
| 78 |
+
|
| 79 |
+
msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
|
| 80 |
+
fn=bot, inputs=[chatbot, url_params, json_result], outputs=[chatbot, url_params]
|
| 81 |
+
)
|
| 82 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 83 |
+
demo.load(
|
| 84 |
+
fn=lambda x: x,
|
| 85 |
+
inputs=[url_params],
|
| 86 |
+
outputs=[url_params],
|
| 87 |
+
_js=get_window_url_params,
|
| 88 |
+
queue=False
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
demo.launch()
|
| 92 |
+
|
| 93 |
+
'''import gradio as gr
|
| 94 |
+
import time
|
| 95 |
import random
|
| 96 |
import json
|
| 97 |
|
|
|
|
| 194 |
)
|
| 195 |
|
| 196 |
demo.launch()
|
| 197 |
+
'''
|