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