baconnier commited on
Commit
4c0ff10
Β·
verified Β·
1 Parent(s): b7d7689

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import openai
3
  import gradio as gr
4
  import json
 
5
 
6
  class ArtExplorer:
7
  def __init__(self):
@@ -14,6 +15,28 @@ class ArtExplorer:
14
  "selections": {}
15
  }
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def get_llm_response(self, query: str, zoom_context: dict = None) -> dict:
18
  system_prompt = """You are an art history expert. Generate a structured JSON configuration for an interactive art exploration interface."""
19
 
@@ -23,6 +46,11 @@ class ArtExplorer:
23
  Current Selections: {json.dumps(zoom_context) if zoom_context else 'None'}
24
 
25
  Return a JSON object with configurations for temporal, geographical, and style axes.
 
 
 
 
 
26
  """
27
 
28
  response = self.client.chat.completions.create(
@@ -41,7 +69,6 @@ class ArtExplorer:
41
  with gr.Blocks() as demo:
42
  gr.Markdown("# Art History Explorer")
43
 
44
- # Query and initial search
45
  with gr.Row():
46
  query = gr.Textbox(
47
  label="Enter your art history query",
@@ -49,7 +76,6 @@ class ArtExplorer:
49
  )
50
  search_btn = gr.Button("Explore")
51
 
52
- # Display areas for each axis
53
  with gr.Row():
54
  # Temporal axis
55
  with gr.Column():
@@ -64,14 +90,10 @@ class ArtExplorer:
64
 
65
  # Geographical axis
66
  with gr.Column():
67
- map_select = gr.Map(
68
- label="Geographic Location",
69
- interactive=True
70
- )
71
  geo_explanation = gr.Markdown()
72
  geo_zoom = gr.Button("πŸ” Zoom Geography")
73
 
74
- # Style selection
75
  with gr.Row():
76
  style_select = gr.Dropdown(
77
  multiselect=True,
@@ -85,9 +107,12 @@ class ArtExplorer:
85
  self.current_state["selections"] = {}
86
  self.current_state["zoom_level"] = 0
87
 
 
 
 
88
  return {
89
  time_slider: config["temporal"]["range"],
90
- map_select: config["geographical"]["locations"],
91
  style_select: gr.Dropdown(choices=config["style"]["options"]),
92
  time_explanation: config["temporal"]["explanation"],
93
  geo_explanation: config["geographical"]["explanation"],
@@ -110,8 +135,9 @@ class ArtExplorer:
110
  time_explanation: config["temporal"]["explanation"]
111
  })
112
  elif axis_name == "geographical":
 
113
  updates.update({
114
- map_select: config["geographical"]["locations"],
115
  geo_explanation: config["geographical"]["explanation"]
116
  })
117
  elif axis_name == "style":
@@ -125,7 +151,7 @@ class ArtExplorer:
125
  search_btn.click(
126
  fn=initial_search,
127
  inputs=[query],
128
- outputs=[time_slider, map_select, style_select,
129
  time_explanation, geo_explanation, style_explanation]
130
  )
131
 
@@ -137,19 +163,9 @@ class ArtExplorer:
137
 
138
  geo_zoom.click(
139
  fn=lambda q, v: zoom_axis(q, "geographical", v),
140
- inputs=[query, map_select],
141
- outputs=[map_select, geo_explanation]
142
  )
143
 
144
  style_zoom.click(
145
- fn=lambda q, v: zoom_axis(q, "style", v),
146
- inputs=[query, style_select],
147
- outputs=[style_select, style_explanation]
148
- )
149
-
150
- return demo
151
-
152
- if __name__ == "__main__":
153
- explorer = ArtExplorer()
154
- demo = explorer.create_interface()
155
- demo.launch()
 
2
  import openai
3
  import gradio as gr
4
  import json
5
+ import plotly.graph_objects as go
6
 
7
  class ArtExplorer:
8
  def __init__(self):
 
15
  "selections": {}
16
  }
17
 
18
+ def create_map(self, locations):
19
+ """Create a Plotly map figure from location data"""
20
+ fig = go.Figure(go.Scattermapbox(
21
+ lat=[loc.get('lat') for loc in locations],
22
+ lon=[loc.get('lon') for loc in locations],
23
+ mode='markers',
24
+ marker=go.scattermapbox.Marker(size=10),
25
+ text=[loc.get('name') for loc in locations],
26
+ hovertemplate='<b>%{text}</b><extra></extra>'
27
+ ))
28
+
29
+ fig.update_layout(
30
+ mapbox_style="open-street-map",
31
+ mapbox=dict(
32
+ center=dict(lat=48.8566, lon=2.3522), # Default to Paris
33
+ zoom=4
34
+ ),
35
+ margin=dict(r=0, t=0, l=0, b=0),
36
+ showlegend=False
37
+ )
38
+ return fig
39
+
40
  def get_llm_response(self, query: str, zoom_context: dict = None) -> dict:
41
  system_prompt = """You are an art history expert. Generate a structured JSON configuration for an interactive art exploration interface."""
42
 
 
46
  Current Selections: {json.dumps(zoom_context) if zoom_context else 'None'}
47
 
48
  Return a JSON object with configurations for temporal, geographical, and style axes.
49
+ Include exact coordinates for geographical locations in this format:
50
+ "locations": [
51
+ {{"name": "Paris", "lat": 48.8566, "lon": 2.3522, "description": "Center of French art"}},
52
+ {{"name": "Florence", "lat": 43.7696, "lon": 11.2558, "description": "Renaissance art hub"}}
53
+ ]
54
  """
55
 
56
  response = self.client.chat.completions.create(
 
69
  with gr.Blocks() as demo:
70
  gr.Markdown("# Art History Explorer")
71
 
 
72
  with gr.Row():
73
  query = gr.Textbox(
74
  label="Enter your art history query",
 
76
  )
77
  search_btn = gr.Button("Explore")
78
 
 
79
  with gr.Row():
80
  # Temporal axis
81
  with gr.Column():
 
90
 
91
  # Geographical axis
92
  with gr.Column():
93
+ map_plot = gr.Plot(label="Geographic Location")
 
 
 
94
  geo_explanation = gr.Markdown()
95
  geo_zoom = gr.Button("πŸ” Zoom Geography")
96
 
 
97
  with gr.Row():
98
  style_select = gr.Dropdown(
99
  multiselect=True,
 
107
  self.current_state["selections"] = {}
108
  self.current_state["zoom_level"] = 0
109
 
110
+ # Create map figure from locations
111
+ map_fig = self.create_map(config["geographical"]["locations"])
112
+
113
  return {
114
  time_slider: config["temporal"]["range"],
115
+ map_plot: map_fig,
116
  style_select: gr.Dropdown(choices=config["style"]["options"]),
117
  time_explanation: config["temporal"]["explanation"],
118
  geo_explanation: config["geographical"]["explanation"],
 
135
  time_explanation: config["temporal"]["explanation"]
136
  })
137
  elif axis_name == "geographical":
138
+ map_fig = self.create_map(config["geographical"]["locations"])
139
  updates.update({
140
+ map_plot: map_fig,
141
  geo_explanation: config["geographical"]["explanation"]
142
  })
143
  elif axis_name == "style":
 
151
  search_btn.click(
152
  fn=initial_search,
153
  inputs=[query],
154
+ outputs=[time_slider, map_plot, style_select,
155
  time_explanation, geo_explanation, style_explanation]
156
  )
157
 
 
163
 
164
  geo_zoom.click(
165
  fn=lambda q, v: zoom_axis(q, "geographical", v),
166
+ inputs=[query, map_plot],
167
+ outputs=[map_plot, geo_explanation]
168
  )
169
 
170
  style_zoom.click(
171
+ fn=lambda q, v: zoom_axis