Spaces:
Sleeping
Sleeping
Create art_explorer.ppy
Browse files- art_explorer.ppy +66 -0
art_explorer.ppy
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import instructor
|
4 |
+
from typing import Optional, Dict, Any, List
|
5 |
+
from models import ArtHistoryResponse
|
6 |
+
from prompts import SYSTEM_PROMPT, CONTEXTUAL_ZOOM_PROMPT
|
7 |
+
|
8 |
+
class ArtExplorer:
|
9 |
+
def __init__(self):
|
10 |
+
self.client = instructor.patch(openai.OpenAI(
|
11 |
+
base_url="https://api.groq.com/openai/v1",
|
12 |
+
api_key=os.environ.get("GROQ_API_KEY")
|
13 |
+
))
|
14 |
+
self.current_context = {}
|
15 |
+
self.exploration_history = []
|
16 |
+
|
17 |
+
def explore_art_period(
|
18 |
+
self,
|
19 |
+
query: str,
|
20 |
+
zoom_level: int = 1,
|
21 |
+
previous_context: Optional[Dict] = None
|
22 |
+
) -> ArtHistoryResponse:
|
23 |
+
try:
|
24 |
+
response = self.client.chat.completions.create(
|
25 |
+
model="mixtral-8x7b-32768",
|
26 |
+
response_model=ArtHistoryResponse,
|
27 |
+
messages=[
|
28 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
29 |
+
{"role": "user", "content": CONTEXTUAL_ZOOM_PROMPT.format(
|
30 |
+
query=query,
|
31 |
+
zoom_level=zoom_level,
|
32 |
+
previous_context=previous_context or {}
|
33 |
+
)}
|
34 |
+
]
|
35 |
+
)
|
36 |
+
|
37 |
+
self.current_context = response.model_dump()
|
38 |
+
self.exploration_history.append({
|
39 |
+
"query": query,
|
40 |
+
"zoom_level": zoom_level,
|
41 |
+
"response": self.current_context
|
42 |
+
})
|
43 |
+
|
44 |
+
return response
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
print(f"Error in exploration: {e}")
|
48 |
+
return None
|
49 |
+
|
50 |
+
def get_location_data(self) -> List[Dict[str, Any]]:
|
51 |
+
if not self.current_context:
|
52 |
+
return []
|
53 |
+
|
54 |
+
return [loc.dict() for loc in self.current_context.get("locations", [])]
|
55 |
+
|
56 |
+
def get_timeline_data(self) -> Dict[str, Any]:
|
57 |
+
if not self.current_context:
|
58 |
+
return {}
|
59 |
+
|
60 |
+
time_period = self.current_context.get("time_period", {})
|
61 |
+
return {
|
62 |
+
"period_name": time_period.get("name", ""),
|
63 |
+
"start_year": time_period.get("start_year"),
|
64 |
+
"end_year": time_period.get("end_year"),
|
65 |
+
"events": self.current_context.get("historical_events", [])
|
66 |
+
}
|