baconnier commited on
Commit
8d4adac
Β·
verified Β·
1 Parent(s): c6219ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -57
app.py CHANGED
@@ -3,15 +3,8 @@ import openai
3
  import gradio as gr
4
  import json
5
  import plotly.graph_objects as go
6
- from typing import Dict, Any
7
- import os
8
- import openai
9
- import gradio as gr
10
- import json
11
- import plotly.graph_objects as go
12
  from variables import CONTEXTUAL_ZOOM_PROMPT, CONTEXTUAL_ZOOM_default_response
13
 
14
-
15
  class ArtExplorer:
16
  def __init__(self):
17
  self.client = openai.OpenAI(
@@ -122,9 +115,59 @@ class ArtExplorer:
122
  style_explanation = gr.Markdown()
123
  style_zoom = gr.Button("πŸ” Zoom Styles")
124
 
125
- # Set up event handlers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  search_btn.click(
127
- fn=self._handle_search,
128
  inputs=[query],
129
  outputs=[
130
  time_slider,
@@ -137,69 +180,25 @@ class ArtExplorer:
137
  )
138
 
139
  time_zoom.click(
140
- fn=lambda q, v: self._handle_zoom("temporal", q, v),
141
  inputs=[query, time_slider],
142
  outputs=[time_slider, time_explanation]
143
  )
144
 
145
  geo_zoom.click(
146
- fn=lambda q, v: self._handle_zoom("geographical", q, v),
147
  inputs=[query, map_plot],
148
  outputs=[map_plot, geo_explanation]
149
  )
150
 
151
  style_zoom.click(
152
- fn=lambda q, v: self._handle_zoom("style", q, v),
153
  inputs=[query, style_select],
154
  outputs=[style_select, style_explanation]
155
  )
156
 
157
  return demo
158
 
159
- def _handle_search(self, query: str) -> Dict[str, Any]:
160
- """Handle the initial search query"""
161
- config = self.get_llm_response(query)
162
- temporal_config = config["axis_configurations"]["temporal"]["current_zoom"]
163
- geographical_config = config["axis_configurations"]["geographical"]["current_zoom"]
164
- style_config = config["axis_configurations"]["style"]["current_zoom"]
165
-
166
- map_fig = self.create_map(geographical_config["locations"])
167
-
168
- return {
169
- "time_slider": temporal_config["range"],
170
- "map_plot": map_fig,
171
- "style_select": gr.Dropdown(choices=style_config["options"]),
172
- "time_explanation": temporal_config["explanation"],
173
- "geo_explanation": geographical_config.get("explanation", ""),
174
- "style_explanation": style_config["explanation"]
175
- }
176
-
177
- def _handle_zoom(self, axis_name: str, query: str, current_value: Any) -> Dict[str, Any]:
178
- """Handle zoom events for any axis"""
179
- self.current_state["zoom_level"] += 1
180
- config = self.get_llm_response(
181
- query,
182
- zoom_context={axis_name: current_value}
183
- )
184
- axis_config = config["axis_configurations"][axis_name]["current_zoom"]
185
-
186
- if axis_name == "temporal":
187
- return {
188
- "time_slider": axis_config["range"],
189
- "time_explanation": axis_config["explanation"]
190
- }
191
- elif axis_name == "geographical":
192
- map_fig = self.create_map(axis_config["locations"])
193
- return {
194
- "map_plot": map_fig,
195
- "geo_explanation": axis_config.get("explanation", "")
196
- }
197
- else: # style
198
- return {
199
- "style_select": gr.Dropdown(choices=axis_config["options"]),
200
- "style_explanation": axis_config["explanation"]
201
- }
202
-
203
  def main():
204
  print("Starting initialization...")
205
  explorer = ArtExplorer()
@@ -209,7 +208,7 @@ def main():
209
  demo.launch(
210
  server_name="0.0.0.0",
211
  server_port=7860,
212
- share=False
213
  )
214
 
215
  if __name__ == "__main__":
 
3
  import gradio as gr
4
  import json
5
  import plotly.graph_objects as go
 
 
 
 
 
 
6
  from variables import CONTEXTUAL_ZOOM_PROMPT, CONTEXTUAL_ZOOM_default_response
7
 
 
8
  class ArtExplorer:
9
  def __init__(self):
10
  self.client = openai.OpenAI(
 
115
  style_explanation = gr.Markdown()
116
  style_zoom = gr.Button("πŸ” Zoom Styles")
117
 
118
+ def initial_search(query):
119
+ """
120
+ Handle the initial search query and return properly formatted outputs for Gradio
121
+ Returns individual values instead of a dictionary for Gradio components
122
+ """
123
+ config = self.get_llm_response(query)
124
+ temporal_config = config["axis_configurations"]["temporal"]["current_zoom"]
125
+ geographical_config = config["axis_configurations"]["geographical"]["current_zoom"]
126
+ style_config = config["axis_configurations"]["style"]["current_zoom"]
127
+
128
+ map_fig = self.create_map(geographical_config["locations"])
129
+
130
+ # Return individual values in the order expected by Gradio
131
+ return (
132
+ temporal_config["range"], # time_slider
133
+ map_fig, # map_plot
134
+ style_config["options"], # style_select
135
+ temporal_config["explanation"], # time_explanation
136
+ geographical_config.get("explanation", ""), # geo_explanation
137
+ style_config["explanation"] # style_explanation
138
+ )
139
+
140
+ def zoom_axis(query, axis_name, current_value):
141
+ """
142
+ Handle zoom events for any axis with proper return formatting
143
+ """
144
+ self.current_state["zoom_level"] += 1
145
+ config = self.get_llm_response(
146
+ query,
147
+ zoom_context={axis_name: current_value}
148
+ )
149
+ axis_config = config["axis_configurations"][axis_name]["current_zoom"]
150
+
151
+ if axis_name == "temporal":
152
+ return (
153
+ axis_config["range"],
154
+ axis_config["explanation"]
155
+ )
156
+ elif axis_name == "geographical":
157
+ map_fig = self.create_map(axis_config["locations"])
158
+ return (
159
+ map_fig,
160
+ axis_config.get("explanation", "")
161
+ )
162
+ else: # style
163
+ return (
164
+ axis_config["options"],
165
+ axis_config["explanation"]
166
+ )
167
+
168
+ # Connect event handlers
169
  search_btn.click(
170
+ fn=initial_search,
171
  inputs=[query],
172
  outputs=[
173
  time_slider,
 
180
  )
181
 
182
  time_zoom.click(
183
+ fn=lambda q, v: zoom_axis(q, "temporal", v),
184
  inputs=[query, time_slider],
185
  outputs=[time_slider, time_explanation]
186
  )
187
 
188
  geo_zoom.click(
189
+ fn=lambda q, v: zoom_axis(q, "geographical", v),
190
  inputs=[query, map_plot],
191
  outputs=[map_plot, geo_explanation]
192
  )
193
 
194
  style_zoom.click(
195
+ fn=lambda q, v: zoom_axis(q, "style", v),
196
  inputs=[query, style_select],
197
  outputs=[style_select, style_explanation]
198
  )
199
 
200
  return demo
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  def main():
203
  print("Starting initialization...")
204
  explorer = ArtExplorer()
 
208
  demo.launch(
209
  server_name="0.0.0.0",
210
  server_port=7860,
211
+ share=True # Set to True to create a public link
212
  )
213
 
214
  if __name__ == "__main__":