Hey, everybody. I want to connect Search Page Ranking template to backend search service, but I am not getting anything. I have read the documentation, looked at examples, tried many different ways to generate json, but the result does not want to be displayed in the template. I’m trying to run the code that phind.com generated for me. Please point out my mistakes. Working with dynamic selection is not well covered.
<View>
<View style="margin:5px; width:575px; border-radius:30px; border:1px solid #dcdcdc; height:45px; width:500px; font-size:16px; display: flex; justify-content: center; padding: 8px; outline: none; background-image: url('https://htx-pub.s3.amazonaws.com/samples/google-search-magnifying-glass-icon-5.jpeg'); background-position: left center; background-size: 24px; background-repeat: no-repeat; background-origin: content-box; ">
<Text name="text" value="$query"/>
</View>
<View className="dynamic_choices">
<Choices name="dynamic_choices" toName="text" selection="checkbox" value="$options" layout="vertical" choice="multiple" allownested="true"/>
</View>
<Style>
.searchresultsarea {
margin-left: 10px;
font-family: 'Arial';
}
.searchresult {
margin-left: 8px;
}
.searchresult h2 {
font-size: 19px;
line-height: 18px;
font-weight: normal;
color: rgb(29, 1, 189);
margin-bottom: 0px;
margin-top: 25px;
}
.searchresult a {
font-size: 14px;
line-height: 14px;
color: green;
margin-bottom: 0px;
}
.searchresult button {
font-size: 10px;
line-height: 14px;
color: green;
margin-bottom: 0px;
padding: 0px;
border-width: 0px;
background-color: white;
}
</Style>
</View>
class Search(LabelStudioMLBase):
def setup(self):
self.set("model_version", f'{self.__class__.__name__}-v0.0.1')
def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) -> List[Dict]:
predictions = []
for task in tasks:
print(">> task = ", task)
# Получаем поисковый запрос из задачи
query = task.get('data', {}).get('query', '')
print(">> query = ", query)
# Здесь должна быть логика получения результатов поиска
# Для демонстрации используем статические данные
mock_results = self._generate_mock_results(query)
prediction = {
'task_id': task['id'],
'predictions': [{
'result': mock_results,
'model_version': self.model_version,
'score': 1.0
}]
}
predictions.append(prediction)
return predictions
def _generate_mock_results(self, query: str) -> List[Dict]:
# В реальном приложении здесь должна быть интеграция с поисковой системой
return [
{
"html": "<div class='searchresultsarea'><div class='searchresult'><h2>Result 1</h2><a href='#'>Link 1</a></div></div>",
"value": "result1"
},
{
"html": "<div class='searchresultsarea'><div class='searchresult'><h2>Result 2</h2><a href='#'>Link 2</a></div></div>",
"value": "result2"
}
]