botsi commited on
Commit
6e2b5b2
·
verified ·
1 Parent(s): 42cfece

Update app.py

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