botsi commited on
Commit
5361910
·
verified ·
1 Parent(s): 00a079d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -96
app.py CHANGED
@@ -1,98 +1,5 @@
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]
81
- )
82
-
83
- clear.click(lambda: None, None, chatbot, queue=False)
84
- demo.load(
85
- fn=lambda x: x,
86
- inputs=[url_params],
87
- outputs=[url_params],
88
- _js=get_window_url_params,
89
- queue=False
90
- )
91
-
92
- demo.launch()
93
-
94
- '''import gradio as gr
95
- import time
96
  import random
97
  import json
98
 
@@ -177,7 +84,7 @@ with gr.Blocks() as demo:
177
  {json.dumps(url_params, indent=4)}
178
  """
179
  else:
180
- bot_message = "You did not ask for the URL Params :) But here is what I read from the database" + {json_result}
181
  history[-1][1] = bot_message
182
  time.sleep(1)
183
  return history
@@ -194,5 +101,4 @@ with gr.Blocks() as demo:
194
  queue=False
195
  )
196
 
197
- demo.launch()
198
- '''
 
1
  import gradio as gr
2
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import random
4
  import json
5
 
 
84
  {json.dumps(url_params, indent=4)}
85
  """
86
  else:
87
+ bot_message = "You did not ask for the URL Params :) But here is what I read from the database"
88
  history[-1][1] = bot_message
89
  time.sleep(1)
90
  return history
 
101
  queue=False
102
  )
103
 
104
+ demo.launch()