Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
9 |
+
self.current_state = {
|
10 |
+
"zoom_level": 0,
|
11 |
+
"selections": {
|
12 |
+
"temporal": None,
|
13 |
+
"geographical": None,
|
14 |
+
"style": None
|
15 |
+
}
|
16 |
+
}
|
17 |
+
|
18 |
+
def get_llm_configuration(self, query, zoom_level, current_selections=None):
|
19 |
+
prompt = f"""
|
20 |
+
User Query: {query}
|
21 |
+
Current Zoom Level: {zoom_level}
|
22 |
+
Current Selections: {json.dumps(current_selections) if current_selections else 'None'}
|
23 |
+
|
24 |
+
Generate configuration for interactive art exploration interface.
|
25 |
+
"""
|
26 |
+
|
27 |
+
response = self.groq_client.chat.completions.create(
|
28 |
+
messages=[
|
29 |
+
{"role": "system", "content": "You are an art history expert."},
|
30 |
+
{"role": "user", "content": prompt}
|
31 |
+
],
|
32 |
+
model="mixtral-8x7b-32768",
|
33 |
+
temperature=0.1
|
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("# Interactive Art History Explorer")
|
41 |
+
|
42 |
+
# Initial query input
|
43 |
+
with gr.Row():
|
44 |
+
query = gr.Textbox(label="Enter your art history query (e.g., 'Napoleon wars')")
|
45 |
+
search_btn = gr.Button("Explore")
|
46 |
+
|
47 |
+
# Main display area
|
48 |
+
with gr.Row():
|
49 |
+
# Time Period Column
|
50 |
+
with gr.Column():
|
51 |
+
gr.Markdown("## Time Period")
|
52 |
+
time_slider = gr.Slider(
|
53 |
+
minimum=1000,
|
54 |
+
maximum=2024,
|
55 |
+
label="Select Time Period",
|
56 |
+
interactive=True
|
57 |
+
)
|
58 |
+
time_explanation = gr.Markdown("Time period context will appear here")
|
59 |
+
time_zoom = gr.Button("Zoom In - Timeline")
|
60 |
+
|
61 |
+
# Geographic Column
|
62 |
+
with gr.Column():
|
63 |
+
gr.Markdown("## Geographic Location")
|
64 |
+
map_component = gr.Map(label="Select Locations")
|
65 |
+
geo_explanation = gr.Markdown("Geographic context will appear here")
|
66 |
+
geo_zoom = gr.Button("Zoom In - Geography")
|
67 |
+
|
68 |
+
# Style Selection
|
69 |
+
with gr.Row():
|
70 |
+
gr.Markdown("## Artistic Styles")
|
71 |
+
style_dropdown = gr.Dropdown(
|
72 |
+
multiselect=True,
|
73 |
+
label="Select Styles"
|
74 |
+
)
|
75 |
+
style_explanation = gr.Markdown("Style context will appear here")
|
76 |
+
style_zoom = gr.Button("Zoom In - Styles")
|
77 |
+
|
78 |
+
# Initial search handler
|
79 |
+
def initial_search(query):
|
80 |
+
config = self.get_llm_configuration(query, zoom_level=0)
|
81 |
+
|
82 |
+
# Update components with initial values
|
83 |
+
return {
|
84 |
+
time_slider: config["temporal"]["range"],
|
85 |
+
map_component: config["geographical"]["locations"],
|
86 |
+
style_dropdown: gr.Dropdown(choices=config["style"]["options"]),
|
87 |
+
time_explanation: config["temporal"]["explanation"],
|
88 |
+
geo_explanation: config["geographical"]["explanation"],
|
89 |
+
style_explanation: config["style"]["explanation"]
|
90 |
+
}
|
91 |
+
|
92 |
+
# Zoom handlers
|
93 |
+
def zoom_timeline(query, current_time):
|
94 |
+
config = self.get_llm_configuration(
|
95 |
+
query,
|
96 |
+
zoom_level=self.current_state["zoom_level"] + 1,
|
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 |
+
zoom_level=self.current_state["zoom_level"] + 1,
|
119 |
+
current_selections={"style": current_styles}
|
120 |
+
)
|
121 |
+
return {
|
122 |
+
style_dropdown: gr.Dropdown(choices=config["style"]["options"]),
|
123 |
+
style_explanation: config["style"]["explanation"]
|
124 |
+
}
|
125 |
+
|
126 |
+
# Connect event handlers
|
127 |
+
search_btn.click(
|
128 |
+
fn=initial_search,
|
129 |
+
inputs=[query],
|
130 |
+
outputs=[time_slider, map_component, style_dropdown,
|
131 |
+
time_explanation, geo_explanation, style_explanation]
|
132 |
+
)
|
133 |
+
|
134 |
+
time_zoom.click(
|
135 |
+
fn=zoom_timeline,
|
136 |
+
inputs=[query, time_slider],
|
137 |
+
outputs=[time_slider, time_explanation]
|
138 |
+
)
|
139 |
+
|
140 |
+
geo_zoom.click(
|
141 |
+
fn=zoom_geography,
|
142 |
+
inputs=[query, map_component],
|
143 |
+
outputs=[map_component, geo_explanation]
|
144 |
+
)
|
145 |
+
|
146 |
+
style_zoom.click(
|
147 |
+
fn=zoom_styles,
|
148 |
+
inputs=[query, style_dropdown],
|
149 |
+
outputs=[style_dropdown, style_explanation]
|
150 |
+
)
|
151 |
+
|
152 |
+
return demo
|
153 |
+
|
154 |
+
if __name__ == "__main__":
|
155 |
+
explorer = ArtExplorer()
|
156 |
+
demo = explorer.create_interface()
|
157 |
+
demo.launch()
|