botsi commited on
Commit
07f740f
·
verified ·
1 Parent(s): 490c713

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -95
app.py CHANGED
@@ -4,52 +4,8 @@ import random
4
  import json
5
  import mysql.connector
6
  import json
7
- import requests
8
- import urllib.parse
9
- import urllib.request
10
 
11
- # fetch data from the decisions database
12
- def fetch_data_as_json():
13
- # Connect to the database
14
- conn = mysql.connector.connect(
15
- host="18.153.94.89",
16
- user="root",
17
- password="N12RXMKtKxRj",
18
- database="lionessdb"
19
- )
20
-
21
- # Create a cursor object
22
- cursor = conn.cursor()
23
-
24
- # Execute the SQL query
25
- query = "SELECT playerNr, subjectNr, initialCredit, transfer1, tripledAmount1, keptForSelf1, returned1, " \
26
- "newCreditRound2, transfer2, tripledAmount2, keptForSelf2, returned2, results2rounds, " \
27
- "newCreditRound3, transfer3, tripledAmount3, keptForSelf3, returned3, results3rounds FROM e5390g37096_decisions"
28
- cursor.execute(query)
29
-
30
- # Fetch all rows and convert to a list of dictionaries
31
- rows = cursor.fetchall()
32
- result = []
33
- for row in rows:
34
- d = {}
35
- for i, col in enumerate(cursor.description):
36
- d[col[0]] = row[i]
37
- result.append(d)
38
-
39
- # Convert the list of dictionaries to JSON
40
- json_result = json.dumps(result)
41
-
42
- # Close the database connection
43
- conn.close()
44
-
45
- # Return the JSON result
46
- return json_result
47
-
48
- # Call the function to fetch data as JSON
49
- json_result = fetch_data_as_json()
50
-
51
- # Print or use the json_result variable as needed
52
- print(json_result)
53
 
54
  get_window_url_params = """
55
  function() {
@@ -69,6 +25,49 @@ get_window_session_index = """
69
 
70
  # get url params out of JavaScript function as variables and pass the session_index to fetch_personalized_data() function
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  def fetch_personalized_data(session_index):
74
  # Connect to the database
@@ -131,51 +130,6 @@ def fetch_personalized_data(session_index):
131
  # Return the JSON result
132
  return personalized_result
133
 
134
-
135
- with gr.Blocks() as demo:
136
- gr.Markdown("""## Gradio send queryparam to chatbot
137
- type `read query`
138
- """)
139
- url_params = gr.JSON({}, visible=False, label="URL Params")
140
- chatbot = gr.Chatbot().style(height=500)
141
- msg = gr.Textbox()
142
- clear = gr.Button("Clear")
143
-
144
- def user(user_message, url_params, history):
145
- return "", history + [[user_message, None]]
146
-
147
-
148
- def bot(history, url_params):
149
- if "read query" in history[-1][0]:
150
- session_index = url_params.get('session_index')
151
- personalized_data = fetch_personalized_data(session_index)
152
- bot_message = f"""
153
- here your URL params:
154
- {json.dumps(url_params, indent=4)}
155
- """
156
- print(url_params)
157
- else:
158
- bot_message = random.choice(["Yes", "No"])
159
- history[-1][1] = bot_message
160
- time.sleep(1)
161
- return history
162
-
163
- msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
164
- fn=bot, inputs=[chatbot, url_params], outputs=[chatbot]
165
- )
166
- clear.click(lambda: None, None, chatbot, queue=False)
167
- demo.load(
168
- fn=lambda x: x,
169
- inputs=[url_params],
170
- outputs=[url_params],
171
- _js=get_window_url_params,
172
- queue=False
173
- )
174
-
175
- demo.launch()
176
-
177
-
178
-
179
  # Call the function to fetch data as JSON
180
  personalized_data = fetch_personalized_data()
181
 
@@ -183,11 +137,6 @@ personalized_data = fetch_personalized_data()
183
  print(personalized_data)
184
 
185
 
186
-
187
-
188
-
189
-
190
-
191
  '''
192
  WHERE {decisions_table}.session_index = '{session_index}'
193
 
 
4
  import json
5
  import mysql.connector
6
  import json
 
 
 
7
 
8
+ session_index = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  get_window_url_params = """
11
  function() {
 
25
 
26
  # get url params out of JavaScript function as variables and pass the session_index to fetch_personalized_data() function
27
 
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("""## Gradio send queryparam to chatbot
30
+ type `read query`
31
+ """)
32
+ url_params = gr.JSON({}, visible=False, label="URL Params")
33
+ chatbot = gr.Chatbot().style(height=500)
34
+ msg = gr.Textbox()
35
+ clear = gr.Button("Clear")
36
+
37
+ def user(user_message, url_params, history):
38
+ return "", history + [[user_message, None]]
39
+
40
+
41
+ def bot(history, url_params):
42
+ global session_index
43
+ if "read query" in history[-1][0]:
44
+ session_index = url_params.get('session_index')
45
+ personalized_data = fetch_personalized_data(session_index)
46
+ bot_message = f"""
47
+ here your URL params:
48
+ {json.dumps(url_params, indent=4)}
49
+ """
50
+ print(url_params)
51
+ else:
52
+ bot_message = random.choice(["Yes", "No"])
53
+ history[-1][1] = bot_message
54
+ time.sleep(1)
55
+ return history
56
+
57
+ msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
58
+ fn=bot, inputs=[chatbot, url_params], outputs=[chatbot]
59
+ )
60
+ clear.click(lambda: None, None, chatbot, queue=False)
61
+ demo.load(
62
+ fn=lambda x: x,
63
+ inputs=[url_params],
64
+ outputs=[url_params],
65
+ _js=get_window_url_params,
66
+ queue=False
67
+ )
68
+
69
+ demo.launch()
70
+
71
 
72
  def fetch_personalized_data(session_index):
73
  # Connect to the database
 
130
  # Return the JSON result
131
  return personalized_result
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  # Call the function to fetch data as JSON
134
  personalized_data = fetch_personalized_data()
135
 
 
137
  print(personalized_data)
138
 
139
 
 
 
 
 
 
140
  '''
141
  WHERE {decisions_table}.session_index = '{session_index}'
142