botsi commited on
Commit
64bf518
·
verified ·
1 Parent(s): 87a5ed5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py CHANGED
@@ -55,6 +55,110 @@ js = """
55
  }
56
  """
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  with gr.Blocks(js=js) as demo:
59
  gr.Markdown("""## Gradio send queryparam to chatbot
60
  Type `hi`
@@ -103,6 +207,12 @@ with gr.Blocks(js=js) as demo:
103
  demo.launch()
104
 
105
 
 
 
 
 
 
 
106
  '''import gradio as gr
107
  import time
108
  import random
 
55
  }
56
  """
57
 
58
+ def get_session_index(url_params, chatbot):
59
+ if url_params:
60
+ print(f"""
61
+ {json.dumps(url_params)}
62
+ """)
63
+ session_index = url_params.get('session_index')
64
+ if session_index: # Check if session_index is not None or empty
65
+ print(session_index)
66
+ # Fetch personalized data
67
+ personalized_data = fetch_personalized_data(session_index)
68
+ print(personalized_data)
69
+ else:
70
+ print("Session index is missing.")
71
+ else:
72
+ print("There are no URL parameters available.")
73
+ time.sleep(1)
74
+ return session_index
75
+
76
+ with gr.Blocks(js=js) as demo:
77
+ gr.Markdown("""## Gradio send queryparam to chatbot
78
+ Type `hi`
79
+ """)
80
+ url_params = gr.JSON({}, visible=False, label="URL Params")
81
+ chatbot = gr.Chatbot().style(height=500)
82
+ msg = gr.Textbox()
83
+ clear = gr.Button("Clear")
84
+
85
+ def user(user_message, url_params, history):
86
+ return "", history + [[user_message, None]]
87
+
88
+ msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
89
+ fn=get_session_index, inputs=[url_params, chatbot], outputs=[chatbot]
90
+ )
91
+
92
+ clear.click(lambda: None, None, chatbot, queue=False)
93
+
94
+ demo.load(
95
+ fn=lambda x: x,
96
+ inputs=[url_params],
97
+ outputs=[url_params],
98
+ _js=js,
99
+ queue=False
100
+ )
101
+
102
+ demo.launch()
103
+
104
+
105
+ '''import gradio as gr
106
+ import time
107
+ import json
108
+ import mysql.connector
109
+
110
+ def fetch_personalized_data(session_index):
111
+ # Connect to the database
112
+ conn = mysql.connector.connect(
113
+ host="18.153.94.89",
114
+ user="root",
115
+ password="N12RXMKtKxRj",
116
+ database="lionessdb"
117
+ )
118
+
119
+ # Create a cursor object
120
+ cursor = conn.cursor()
121
+
122
+ # Replace the placeholders with your actual database and table names
123
+ core_table = "e5390g37096_core"
124
+ decisions_table = "e5390g37096_decisions"
125
+
126
+ # Query to fetch relevant data from both tables based on session_index
127
+ query = f"""
128
+ SELECT e5390g37096_core.playerNr,
129
+ e5390g37096_core.groupNr,
130
+ e5390g37096_core.subjectNr
131
+ FROM e5390g37096_core
132
+ JOIN e5390g37096_decisions ON
133
+ e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
134
+ WHERE e5390g37096_decisions.session_index = '{session_index}'
135
+ """
136
+
137
+ try:
138
+ cursor.execute(query)
139
+
140
+ # Fetch all rows as lists of tuples
141
+ rows = cursor.fetchall()
142
+
143
+ # Close the database connection
144
+ conn.close()
145
+
146
+ # Convert the rows to a list of dictionaries
147
+ data = [{'playerNr': row[0], 'groupNr': row[1], 'subjectNr': row[2]} for row in rows]
148
+ return data
149
+
150
+ except mysql.connector.Error as err:
151
+ print(f"Error: {err}")
152
+ return None
153
+
154
+ js = """
155
+ function() {
156
+ const params = new URLSearchParams(window.location.search);
157
+ const url_params = Object.fromEntries(params);
158
+ return url_params;
159
+ }
160
+ """
161
+
162
  with gr.Blocks(js=js) as demo:
163
  gr.Markdown("""## Gradio send queryparam to chatbot
164
  Type `hi`
 
207
  demo.launch()
208
 
209
 
210
+
211
+
212
+
213
+
214
+
215
+ '''
216
  '''import gradio as gr
217
  import time
218
  import random