Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,152 +1,150 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
-
from groq import Groq
|
4 |
-
import os
|
5 |
|
6 |
class ArtExplorer:
|
7 |
def __init__(self):
|
8 |
-
self.
|
|
|
|
|
|
|
9 |
self.current_state = {
|
10 |
"zoom_level": 0,
|
11 |
-
"selections": {
|
12 |
-
"temporal": None,
|
13 |
-
"geographical": None,
|
14 |
-
"style": None
|
15 |
-
}
|
16 |
}
|
17 |
|
18 |
-
def
|
19 |
-
|
20 |
-
User Query: {query}
|
21 |
-
Current Zoom Level: {zoom_level}
|
22 |
-
Current Selections: {json.dumps(current_selections) if current_selections else 'None'}
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
messages=[
|
29 |
-
{"role": "system", "content":
|
30 |
-
{"role": "user", "content":
|
31 |
],
|
32 |
-
|
33 |
-
|
34 |
)
|
35 |
|
36 |
return json.loads(response.choices[0].message.content)
|
37 |
|
38 |
def create_interface(self):
|
39 |
with gr.Blocks() as demo:
|
40 |
-
gr.Markdown("#
|
41 |
|
42 |
-
#
|
43 |
with gr.Row():
|
44 |
-
query = gr.Textbox(
|
|
|
|
|
|
|
45 |
search_btn = gr.Button("Explore")
|
46 |
|
47 |
-
#
|
48 |
with gr.Row():
|
49 |
-
#
|
50 |
with gr.Column():
|
51 |
-
gr.Markdown("## Time Period")
|
52 |
time_slider = gr.Slider(
|
53 |
minimum=1000,
|
54 |
maximum=2024,
|
55 |
-
label="
|
56 |
interactive=True
|
57 |
)
|
58 |
-
time_explanation = gr.Markdown(
|
59 |
-
time_zoom = gr.Button("Zoom
|
60 |
|
61 |
-
#
|
62 |
with gr.Column():
|
63 |
-
gr.
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
|
68 |
-
# Style
|
69 |
with gr.Row():
|
70 |
-
gr.
|
71 |
-
style_dropdown = gr.Dropdown(
|
72 |
multiselect=True,
|
73 |
-
label="
|
74 |
)
|
75 |
-
style_explanation = gr.Markdown(
|
76 |
-
style_zoom = gr.Button("Zoom
|
77 |
|
78 |
-
# Initial search handler
|
79 |
def initial_search(query):
|
80 |
-
config = self.
|
|
|
|
|
81 |
|
82 |
-
# Update components with initial values
|
83 |
return {
|
84 |
time_slider: config["temporal"]["range"],
|
85 |
-
|
86 |
-
|
87 |
time_explanation: config["temporal"]["explanation"],
|
88 |
geo_explanation: config["geographical"]["explanation"],
|
89 |
style_explanation: config["style"]["explanation"]
|
90 |
}
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
current_selections={"temporal": current_time}
|
98 |
-
)
|
99 |
-
return {
|
100 |
-
time_slider: config["temporal"]["range"],
|
101 |
-
time_explanation: config["temporal"]["explanation"]
|
102 |
-
}
|
103 |
-
|
104 |
-
def zoom_geography(query, current_locations):
|
105 |
-
config = self.get_llm_configuration(
|
106 |
-
query,
|
107 |
-
zoom_level=self.current_state["zoom_level"] + 1,
|
108 |
-
current_selections={"geographical": current_locations}
|
109 |
-
)
|
110 |
-
return {
|
111 |
-
map_component: config["geographical"]["locations"],
|
112 |
-
geo_explanation: config["geographical"]["explanation"]
|
113 |
-
}
|
114 |
-
|
115 |
-
def zoom_styles(query, current_styles):
|
116 |
-
config = self.get_llm_configuration(
|
117 |
query,
|
118 |
-
|
119 |
-
current_selections={"style": current_styles}
|
120 |
)
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
# Connect event handlers
|
127 |
search_btn.click(
|
128 |
fn=initial_search,
|
129 |
inputs=[query],
|
130 |
-
outputs=[time_slider,
|
131 |
time_explanation, geo_explanation, style_explanation]
|
132 |
)
|
133 |
|
134 |
time_zoom.click(
|
135 |
-
fn=
|
136 |
inputs=[query, time_slider],
|
137 |
outputs=[time_slider, time_explanation]
|
138 |
)
|
139 |
|
140 |
geo_zoom.click(
|
141 |
-
fn=
|
142 |
-
inputs=[query,
|
143 |
-
outputs=[
|
144 |
)
|
145 |
|
146 |
style_zoom.click(
|
147 |
-
fn=
|
148 |
-
inputs=[query,
|
149 |
-
outputs=[
|
150 |
)
|
151 |
|
152 |
return demo
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
import gradio as gr
|
4 |
import json
|
|
|
|
|
5 |
|
6 |
class ArtExplorer:
|
7 |
def __init__(self):
|
8 |
+
self.client = openai.OpenAI(
|
9 |
+
base_url="https://api.groq.com/openai/v1",
|
10 |
+
api_key=os.environ.get("GROQ_API_KEY")
|
11 |
+
)
|
12 |
self.current_state = {
|
13 |
"zoom_level": 0,
|
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 |
|
20 |
+
user_prompt = f"""
|
21 |
+
Query: {query}
|
22 |
+
Zoom Level: {self.current_state['zoom_level']}
|
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(
|
29 |
+
model="mixtral-8x7b-32768",
|
30 |
messages=[
|
31 |
+
{"role": "system", "content": system_prompt},
|
32 |
+
{"role": "user", "content": user_prompt}
|
33 |
],
|
34 |
+
temperature=0.1,
|
35 |
+
max_tokens=1024
|
36 |
)
|
37 |
|
38 |
return json.loads(response.choices[0].message.content)
|
39 |
|
40 |
def create_interface(self):
|
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",
|
48 |
+
placeholder="e.g., Napoleon wars, Renaissance Italy"
|
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():
|
|
|
56 |
time_slider = gr.Slider(
|
57 |
minimum=1000,
|
58 |
maximum=2024,
|
59 |
+
label="Time Period",
|
60 |
interactive=True
|
61 |
)
|
62 |
+
time_explanation = gr.Markdown()
|
63 |
+
time_zoom = gr.Button("π Zoom Time Period")
|
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,
|
78 |
+
label="Artistic Styles"
|
79 |
)
|
80 |
+
style_explanation = gr.Markdown()
|
81 |
+
style_zoom = gr.Button("π Zoom Styles")
|
82 |
|
|
|
83 |
def initial_search(query):
|
84 |
+
config = self.get_llm_response(query)
|
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"],
|
94 |
style_explanation: config["style"]["explanation"]
|
95 |
}
|
96 |
|
97 |
+
def zoom_axis(query, axis_name, current_value):
|
98 |
+
self.current_state["zoom_level"] += 1
|
99 |
+
self.current_state["selections"][axis_name] = current_value
|
100 |
+
|
101 |
+
config = self.get_llm_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
query,
|
103 |
+
zoom_context={axis_name: current_value}
|
|
|
104 |
)
|
105 |
+
|
106 |
+
updates = {}
|
107 |
+
if axis_name == "temporal":
|
108 |
+
updates.update({
|
109 |
+
time_slider: config["temporal"]["range"],
|
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":
|
118 |
+
updates.update({
|
119 |
+
style_select: gr.Dropdown(choices=config["style"]["options"]),
|
120 |
+
style_explanation: config["style"]["explanation"]
|
121 |
+
})
|
122 |
+
return updates
|
123 |
|
124 |
# Connect event handlers
|
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 |
|
132 |
time_zoom.click(
|
133 |
+
fn=lambda q, v: zoom_axis(q, "temporal", v),
|
134 |
inputs=[query, time_slider],
|
135 |
outputs=[time_slider, time_explanation]
|
136 |
)
|
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
|