Spaces:
Sleeping
Sleeping
import gradio as gr | |
import json | |
import os | |
from dotenv import load_dotenv | |
from art_explorer import ExplorationPathGenerator | |
from typing import Dict, Any | |
# Load environment variables | |
load_dotenv() | |
# Initialize the generator | |
generator = ExplorationPathGenerator(api_key=os.getenv("GROQ_API_KEY")) | |
def format_output(result: Dict[str, Any]) -> str: | |
"""Format the exploration result for display""" | |
return json.dumps(result, indent=2) | |
def parse_json_input(json_str: str, default_value: Any) -> Any: | |
"""Safely parse JSON input with fallback to default value""" | |
try: | |
return json.loads(json_str) if json_str else default_value | |
except json.JSONDecodeError: | |
return default_value | |
def explore( | |
query: str, | |
path_history: str = "[]", | |
parameters: str = "{}", | |
depth: int = 5, | |
domain: str = "" | |
) -> str: | |
""" | |
Generate exploration path based on user input | |
Args: | |
query (str): User's exploration query | |
path_history (str): JSON string of previous exploration path | |
parameters (str): JSON string of exploration parameters | |
depth (int): Exploration depth parameter | |
domain (str): Specific domain context | |
Returns: | |
str: Formatted JSON string of exploration results | |
""" | |
# Parse path history | |
selected_path = parse_json_input(path_history, []) | |
# Parse and merge parameters | |
base_parameters = { | |
"depth": depth, | |
"domain": domain if domain else None, | |
"previous_explorations": [] | |
} | |
custom_parameters = parse_json_input(parameters, {}) | |
exploration_parameters = {**base_parameters, **custom_parameters} | |
try: | |
# Generate exploration path | |
result = generator.generate_exploration_path( | |
user_query=query, | |
selected_path=selected_path, | |
exploration_parameters=exploration_parameters | |
) | |
# Format and return result | |
return format_output(result) | |
except Exception as e: | |
error_response = { | |
"error": str(e), | |
"status": "failed", | |
"message": "Failed to generate exploration path" | |
} | |
return format_output(error_response) | |
def create_interface() -> gr.Interface: | |
"""Create and configure the Gradio interface""" | |
# Define interface inputs | |
inputs = [ | |
gr.Textbox( | |
label="Exploration Query", | |
placeholder="Enter your art history exploration query...", | |
lines=2 | |
), | |
gr.Textbox( | |
label="Path History (JSON)", | |
placeholder="[]", | |
lines=3, | |
value="[]" | |
), | |
gr.Textbox( | |
label="Additional Parameters (JSON)", | |
placeholder="{}", | |
lines=3, | |
value="{}" | |
), | |
gr.Slider( | |
label="Exploration Depth", | |
minimum=1, | |
maximum=10, | |
value=5, | |
step=1 | |
), | |
gr.Textbox( | |
label="Domain Context", | |
placeholder="Optional: Specify art history period or movement", | |
lines=1 | |
) | |
] | |
# Define interface output | |
output = gr.Textbox( | |
label="Exploration Result", | |
lines=20 | |
) | |
# Create and return interface | |
return gr.Interface( | |
fn=explore, | |
inputs=inputs, | |
outputs=output, | |
title="Art History Exploration Path Generator", | |
description=""" | |
Generate structured exploration paths for art history queries. | |
- Enter your query about any art history topic | |
- Optionally provide previous exploration path as JSON | |
- Adjust exploration parameters as needed | |
- View the generated exploration path with multiple dimensions | |
""", | |
examples=[ | |
[ | |
"Explore the evolution of Renaissance painting techniques", | |
"[]", | |
"{}", | |
5, | |
"Renaissance" | |
], | |
[ | |
"Investigate the influence of Japanese art on Impressionism", | |
"[]", | |
"{}", | |
7, | |
"Impressionism" | |
] | |
], | |
theme="default" | |
) | |
# Create and launch the interface | |
iface = create_interface() | |
if __name__ == "__main__": | |
# Launch with custom configurations | |
iface.launch() |