Spaces:
Sleeping
Sleeping
Update art_explorer.py
Browse files- art_explorer.py +44 -58
art_explorer.py
CHANGED
@@ -1,66 +1,52 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from
|
5 |
-
from models import ArtHistoryResponse
|
6 |
-
from prompts import SYSTEM_PROMPT, CONTEXTUAL_ZOOM_PROMPT
|
7 |
|
8 |
-
class
|
9 |
-
def __init__(self):
|
10 |
-
self.client =
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
{
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
)
|
36 |
|
37 |
-
|
38 |
-
|
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
|
48 |
-
return
|
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 |
-
}
|
|
|
1 |
+
from typing import Optional, List, Dict
|
2 |
+
import json
|
3 |
+
from groq import Groq
|
4 |
+
from .prompts import SYSTEM_PROMPT, format_exploration_prompt, DEFAULT_RESPONSE
|
|
|
|
|
5 |
|
6 |
+
class ExplorationPathGenerator:
|
7 |
+
def __init__(self, api_key: str):
|
8 |
+
self.client = Groq(api_key=api_key)
|
9 |
+
|
10 |
+
def generate_exploration_path(
|
11 |
+
self,
|
12 |
+
user_query: str,
|
13 |
+
selected_path: Optional[List[Dict[str, str]]] = None,
|
14 |
+
exploration_parameters: Optional[Dict] = None
|
15 |
+
) -> Dict:
|
16 |
+
if selected_path is None:
|
17 |
+
selected_path = []
|
18 |
+
if exploration_parameters is None:
|
19 |
+
exploration_parameters = {
|
20 |
+
"depth": 5,
|
21 |
+
"domain": None,
|
22 |
+
"previous_explorations": []
|
23 |
+
}
|
24 |
+
|
25 |
+
formatted_prompt = format_exploration_prompt(
|
26 |
+
user_query=user_query,
|
27 |
+
selected_path=selected_path,
|
28 |
+
exploration_parameters=exploration_parameters
|
29 |
+
)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
try:
|
32 |
response = self.client.chat.completions.create(
|
|
|
|
|
33 |
messages=[
|
34 |
+
{
|
35 |
+
"role": "system",
|
36 |
+
"content": SYSTEM_PROMPT
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"role": "user",
|
40 |
+
"content": formatted_prompt
|
41 |
+
}
|
42 |
+
],
|
43 |
+
model="mixtral-8x7b-32768", # or your preferred GROQ model
|
44 |
+
temperature=0.7,
|
45 |
+
max_tokens=2000
|
46 |
)
|
47 |
|
48 |
+
result = response.choices[0].message.content
|
49 |
+
return json.loads(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
except Exception as e:
|
51 |
+
print(f"Error generating exploration path: {e}")
|
52 |
+
return DEFAULT_RESPONSE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|