botsi commited on
Commit
d93c3e9
·
verified ·
1 Parent(s): 72537bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -127
app.py CHANGED
@@ -1,130 +1,5 @@
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 get_session_index(url_params, chatbot=None):
71
- if url_params:
72
- print(f"""
73
- {json.dumps(url_params)}
74
- """)
75
- session_index = url_params.get('session_index')
76
- if session_index: # Check if session_index is not None or empty
77
- print(session_index)
78
- # Fetch personalized data
79
- personalized_data = fetch_personalized_data(session_index)
80
- print(personalized_data)
81
- else:
82
- print("Session index is missing.")
83
- else:
84
- print("There are no URL parameters available.")
85
- time.sleep(1)
86
- return session_index
87
-
88
-
89
- '''def get_session_index(url_params):
90
- if url_params:
91
- print(f"""
92
- {json.dumps(url_params)}
93
- """)
94
- session_index = url_params.get('session_index')
95
- if session_index: # Check if session_index is not None or empty
96
- print(session_index)
97
- # Fetch personalized data
98
- personalized_data = fetch_personalized_data(session_index)
99
- print(personalized_data)
100
- else:
101
- print("Session index is missing.")
102
- else:
103
- print("There are no URL parameters available.")
104
- time.sleep(1)
105
- return session_index
106
- '''
107
-
108
- msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
109
- fn=get_session_index, inputs=[chatbot, url_params], outputs=[chatbot]
110
- )
111
-
112
- clear.click(lambda: None, None, chatbot, queue=False)
113
-
114
- demo.load(
115
- fn=lambda x: x,
116
- inputs=[url_params],
117
- outputs=[url_params],
118
- _js=js,
119
- #_js=get_window_url_params(),
120
- queue=False
121
- )
122
-
123
- demo.launch()
124
-
125
-
126
- '''import gradio as gr
127
- import time
128
  import random
129
  import json
130
  import mysql.connector
@@ -224,6 +99,13 @@ with gr.Blocks(js = js) as demo:
224
 
225
  clear.click(lambda: None, None, chatbot, queue=False)
226
 
 
 
 
 
 
 
 
 
227
 
228
-
229
- demo.launch()'''
 
1
  import gradio as gr
2
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import random
4
  import json
5
  import mysql.connector
 
99
 
100
  clear.click(lambda: None, None, chatbot, queue=False)
101
 
102
+ demo.load(
103
+ fn=lambda x: x,
104
+ inputs=[url_params],
105
+ outputs=[url_params],
106
+ _js=js,
107
+ #_js=get_window_url_params(),
108
+ queue=False
109
+ )
110
 
111
+ demo.launch()