Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import gradio as gr
|
|
4 |
import json
|
5 |
import plotly.graph_objects as go
|
6 |
|
7 |
-
|
8 |
CONTEXTUAL_ZOOM_PROMPT = """
|
9 |
You are an expert art historian specializing in interactive exploration. Analyze the query and generate contextually aware zoom configurations with explanations.
|
10 |
|
@@ -98,12 +97,12 @@ Current Zoom States: {
|
|
98 |
"current_zoom": {
|
99 |
"level": "period",
|
100 |
"range": [1799, 1815],
|
101 |
-
"explanation": "Napoleon's reign as First Consul and Emperor"
|
102 |
},
|
103 |
"available_zooms": {
|
104 |
"in": {
|
105 |
"range": [1812, 1815],
|
106 |
-
"explanation": "Focus on final campaigns and artistic responses"
|
107 |
}
|
108 |
}
|
109 |
},
|
@@ -114,11 +113,11 @@ Current Zoom States: {
|
|
114 |
"locations": [
|
115 |
{
|
116 |
"name": "France",
|
117 |
-
"relevance": "Center of Napoleonic art production"
|
118 |
},
|
119 |
{
|
120 |
"name": "Spain",
|
121 |
-
"relevance": "Goya's war paintings perspective"
|
122 |
}
|
123 |
]
|
124 |
}
|
@@ -135,8 +134,6 @@ Current Zoom States: {
|
|
135 |
|
136 |
Generate only the JSON response, maintaining strict JSON format."""
|
137 |
|
138 |
-
|
139 |
-
|
140 |
class ArtExplorer:
|
141 |
def __init__(self):
|
142 |
self.client = openai.OpenAI(
|
@@ -152,7 +149,7 @@ class ArtExplorer:
|
|
152 |
"""Create a Plotly map figure from location data"""
|
153 |
if not locations:
|
154 |
locations = [{"name": "Paris", "lat": 48.8566, "lon": 2.3522}]
|
155 |
-
|
156 |
fig = go.Figure(go.Scattermapbox(
|
157 |
lat=[loc.get('lat') for loc in locations],
|
158 |
lon=[loc.get('lon') for loc in locations],
|
@@ -179,18 +176,20 @@ class ArtExplorer:
|
|
179 |
"style": {"level": self.current_state["zoom_level"], "selection": ""},
|
180 |
"subject": {"level": self.current_state["zoom_level"], "selection": ""}
|
181 |
}
|
182 |
-
|
183 |
if zoom_context:
|
184 |
for key, value in zoom_context.items():
|
185 |
if key in current_zoom_states:
|
186 |
current_zoom_states[key]["selection"] = value
|
|
|
187 |
messages=[
|
188 |
{"role": "system", "content": "You are an expert art historian specializing in interactive exploration."},
|
189 |
{"role": "user", "content": CONTEXTUAL_ZOOM_PROMPT.format(
|
190 |
user_query=query,
|
191 |
current_zoom_states=json.dumps(current_zoom_states, indent=2)
|
192 |
)}
|
193 |
-
]
|
|
|
194 |
print(messages)
|
195 |
response = self.client.chat.completions.create(
|
196 |
model="mixtral-8x7b-32768",
|
@@ -201,7 +200,6 @@ class ArtExplorer:
|
|
201 |
print(response)
|
202 |
result = json.loads(response.choices[0].message.content)
|
203 |
return result
|
204 |
-
|
205 |
except Exception as e:
|
206 |
print(f"Error in LLM response: {str(e)}")
|
207 |
return self.get_default_response()
|
@@ -293,13 +291,10 @@ class ArtExplorer:
|
|
293 |
|
294 |
def initial_search(query):
|
295 |
config = self.get_llm_response(query)
|
296 |
-
|
297 |
-
# Access the correct nested structure
|
298 |
temporal_config = config["axis_configurations"]["temporal"]["current_zoom"]
|
299 |
geographical_config = config["axis_configurations"]["geographical"]["current_zoom"]
|
300 |
style_config = config["axis_configurations"]["style"]["current_zoom"]
|
301 |
|
302 |
-
# Create map figure
|
303 |
map_fig = self.create_map(geographical_config["locations"])
|
304 |
|
305 |
return {
|
@@ -317,7 +312,6 @@ class ArtExplorer:
|
|
317 |
query,
|
318 |
zoom_context={axis_name: current_value}
|
319 |
)
|
320 |
-
|
321 |
axis_config = config["axis_configurations"][axis_name]["current_zoom"]
|
322 |
|
323 |
if axis_name == "temporal":
|
@@ -342,11 +336,11 @@ class ArtExplorer:
|
|
342 |
fn=initial_search,
|
343 |
inputs=[query],
|
344 |
outputs=[
|
345 |
-
time_slider,
|
346 |
-
map_plot,
|
347 |
style_select,
|
348 |
-
time_explanation,
|
349 |
-
geo_explanation,
|
350 |
style_explanation
|
351 |
]
|
352 |
)
|
|
|
4 |
import json
|
5 |
import plotly.graph_objects as go
|
6 |
|
|
|
7 |
CONTEXTUAL_ZOOM_PROMPT = """
|
8 |
You are an expert art historian specializing in interactive exploration. Analyze the query and generate contextually aware zoom configurations with explanations.
|
9 |
|
|
|
97 |
"current_zoom": {
|
98 |
"level": "period",
|
99 |
"range": [1799, 1815],
|
100 |
+
"explanation": "Napoleon's reign as First Consul and Emperor"
|
101 |
},
|
102 |
"available_zooms": {
|
103 |
"in": {
|
104 |
"range": [1812, 1815],
|
105 |
+
"explanation": "Focus on final campaigns and artistic responses"
|
106 |
}
|
107 |
}
|
108 |
},
|
|
|
113 |
"locations": [
|
114 |
{
|
115 |
"name": "France",
|
116 |
+
"relevance": "Center of Napoleonic art production"
|
117 |
},
|
118 |
{
|
119 |
"name": "Spain",
|
120 |
+
"relevance": "Goya's war paintings perspective"
|
121 |
}
|
122 |
]
|
123 |
}
|
|
|
134 |
|
135 |
Generate only the JSON response, maintaining strict JSON format."""
|
136 |
|
|
|
|
|
137 |
class ArtExplorer:
|
138 |
def __init__(self):
|
139 |
self.client = openai.OpenAI(
|
|
|
149 |
"""Create a Plotly map figure from location data"""
|
150 |
if not locations:
|
151 |
locations = [{"name": "Paris", "lat": 48.8566, "lon": 2.3522}]
|
152 |
+
|
153 |
fig = go.Figure(go.Scattermapbox(
|
154 |
lat=[loc.get('lat') for loc in locations],
|
155 |
lon=[loc.get('lon') for loc in locations],
|
|
|
176 |
"style": {"level": self.current_state["zoom_level"], "selection": ""},
|
177 |
"subject": {"level": self.current_state["zoom_level"], "selection": ""}
|
178 |
}
|
179 |
+
|
180 |
if zoom_context:
|
181 |
for key, value in zoom_context.items():
|
182 |
if key in current_zoom_states:
|
183 |
current_zoom_states[key]["selection"] = value
|
184 |
+
|
185 |
messages=[
|
186 |
{"role": "system", "content": "You are an expert art historian specializing in interactive exploration."},
|
187 |
{"role": "user", "content": CONTEXTUAL_ZOOM_PROMPT.format(
|
188 |
user_query=query,
|
189 |
current_zoom_states=json.dumps(current_zoom_states, indent=2)
|
190 |
)}
|
191 |
+
]
|
192 |
+
|
193 |
print(messages)
|
194 |
response = self.client.chat.completions.create(
|
195 |
model="mixtral-8x7b-32768",
|
|
|
200 |
print(response)
|
201 |
result = json.loads(response.choices[0].message.content)
|
202 |
return result
|
|
|
203 |
except Exception as e:
|
204 |
print(f"Error in LLM response: {str(e)}")
|
205 |
return self.get_default_response()
|
|
|
291 |
|
292 |
def initial_search(query):
|
293 |
config = self.get_llm_response(query)
|
|
|
|
|
294 |
temporal_config = config["axis_configurations"]["temporal"]["current_zoom"]
|
295 |
geographical_config = config["axis_configurations"]["geographical"]["current_zoom"]
|
296 |
style_config = config["axis_configurations"]["style"]["current_zoom"]
|
297 |
|
|
|
298 |
map_fig = self.create_map(geographical_config["locations"])
|
299 |
|
300 |
return {
|
|
|
312 |
query,
|
313 |
zoom_context={axis_name: current_value}
|
314 |
)
|
|
|
315 |
axis_config = config["axis_configurations"][axis_name]["current_zoom"]
|
316 |
|
317 |
if axis_name == "temporal":
|
|
|
336 |
fn=initial_search,
|
337 |
inputs=[query],
|
338 |
outputs=[
|
339 |
+
time_slider,
|
340 |
+
map_plot,
|
341 |
style_select,
|
342 |
+
time_explanation,
|
343 |
+
geo_explanation,
|
344 |
style_explanation
|
345 |
]
|
346 |
)
|