-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
145 lines (127 loc) · 4.77 KB
/
models.py
File metadata and controls
145 lines (127 loc) · 4.77 KB
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
from enum import StrEnum
from typing import List, Literal, Optional, Union
from pydantic import BaseModel
import pandas as pd
import json
class Node1ClassificationOutput(BaseModel):
"""Classification logic for the first gate"""
category: Literal["invalid", "simple", "complex"]
response: str
class ChartColumn(BaseModel):
label: str
values: List[Union[str,int,float]]
class ChartData(BaseModel):
"""Structure for chart rendering"""
chart_type: Literal["area", "line", "scatter", "bar"]
data: List[ChartColumn]
x: str
y: List[str]
x_label: str
y_label: str
class FinalResponse(BaseModel):
"""Final response of the chain to be used by the UI"""
response: str
has_chart: bool
chart_data: Optional[ChartData]
class ChatMessage:
def __init__(self, role, text, has_chart, chart_data):
self.role = role
self.text = text
self.has_chart = has_chart
self.chart_data = chart_data
def render(self, st):
with st.chat_message(self.role):
st.markdown(self.text)
if self.has_chart:
# Get reference to the correct streamlit chart method
chart_method = f"{self.chart_data.chart_type}_chart"
method_ref = getattr(st, chart_method)
# Create Pandas DF
data_dict = {col.label : col.values for col in self.chart_data.data}
method_ref(pd.DataFrame(data_dict), x=self.chart_data.x, y=self.chart_data.y, x_label=self.chart_data.x_label, y_label=self.chart_data.y_label)
def __repr__(self):
return f"Role: {self.role} Content: {self.text} Has Chart: {self.has_chart}"
def __str__(self):
return f"Role: {self.role} Content: {self.text} Has Chart: {self.has_chart}"
@classmethod
def from_llm_response(cls, llm_res: FinalResponse):
return cls(role="ai", text=llm_res.response, has_chart=llm_res.has_chart, chart_data=llm_res.chart_data if llm_res.has_chart else None)
class MoveAnalysisJudgement(StrEnum):
INACCURACY = "Inaccuracy"
MISTAKE = "Mistake"
BLUNDER = "Blunder"
MATE = "Mate"
NONE = 'NotAvailable'
class Side(StrEnum):
WHITE = "white"
BLACK = "black"
class ChessOpening:
def __init__(self, name:str, eco:str):
self.name = name
self.eco = eco
def __repr__(self):
return f"GameOpening[name:{self.name},eco:{self.eco}]"
class ChessMove:
def __init__(self, move_san, move_fen, analysis_type, move_number, moving_side, move_eval, forced_mate):
self.move_san = move_san
self.move_fen = move_fen
self.eval = move_eval
self.forced_mate = forced_mate
self.analysis_type = analysis_type
self.move_number= move_number
self.moving_side= moving_side
def __repr__(self):
return f"ChessMove[move_san={self.move_san},move_fen={self.move_fen},analysis_type={self.analysis_type},move_number={self.move_number},forced_mate={self.forced_mate},moving_side={self.moving_side},eval={self.eval}]"
class ChessGame:
def __init__(self,
game_id: str,
white_id: str,
black_id: str,
winner_id: str,
winning_side: Side,
played_on,
status: str,
game_speed: str,
opening: ChessOpening,
n_moves: int = 0,
moves: List[ChessMove] = []):
self.game_id: str = game_id
self.white_id: str = white_id
self.game_speed: str = game_speed
self.black_id: str = black_id
self.n_moves: int = n_moves
self.opening: ChessOpening = opening
self.played_on=played_on
self.moves: List[ChessMove] = moves
self.winner_id: str = winner_id
self.winning_side: Side = winning_side
self.status: str = status
def to_dict(self):
return {
"game_id": self.game_id,
"white_id": self.white_id,
"black_id": self.black_id,
"winner_id": self.winner_id,
"winning_side": str(self.winning_side),
"played_on": self.played_on,
"status": self.status,
"game_speed": self.game_speed,
"perf": self.game_speed,
"opening": {
"name": self.opening.name,
"eco": self.opening.eco
},
"moves": [
{
"move_san": m.move_san,
"move_fen": m.move_fen,
"eval": m.eval,
"is_forced_mate": m.forced_mate,
"analysis_type": str(m.analysis_type),
"move_number": m.move_number,
"moving_side": str(m.moving_side),
} for m in self.moves
]
}
def __repr__(self):
return f"ChessGame[game_id:{self.game_id},white_id:{self.white_id},black_id={self.black_id},played_on:{self.played_on},winner_id:{self.winner_id},winning_side:{self.winning_side},game_speed:{self.game_speed},n_moves:{self.n_moves}opening={self.opening},moves:{str(self.moves)}]"