Spaces:
Sleeping
Sleeping
Update tools/classifier_tool.py
Browse files- tools/classifier_tool.py +30 -43
tools/classifier_tool.py
CHANGED
|
@@ -1,67 +1,54 @@
|
|
| 1 |
-
from
|
| 2 |
-
import
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
class
|
| 6 |
-
name = '
|
| 7 |
-
description = "
|
| 8 |
|
| 9 |
inputs = {
|
| 10 |
-
'
|
| 11 |
'type': 'string',
|
| 12 |
-
'description': '
|
| 13 |
},
|
| 14 |
-
'
|
| 15 |
'type': 'string',
|
| 16 |
-
'description': '
|
| 17 |
},
|
| 18 |
'categories': {
|
| 19 |
'type': 'string',
|
| 20 |
-
'description': 'Comma-separated
|
| 21 |
},
|
| 22 |
'items': {
|
| 23 |
'type': 'string',
|
| 24 |
-
'description': 'Comma-separated list of items to classify.'
|
| 25 |
},
|
| 26 |
}
|
| 27 |
output_type = 'string'
|
| 28 |
|
| 29 |
-
def __init__(self,
|
| 30 |
-
self.
|
| 31 |
-
|
| 32 |
-
raise ValueError("HF_ENDPOINT must be set as environment variable or passed to constructor.")
|
| 33 |
-
super().__init__(**kwargs)
|
| 34 |
|
| 35 |
-
def forward(self,
|
| 36 |
-
prompt = self._build_prompt(
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
response.raise_for_status()
|
| 44 |
-
result = response.json()
|
| 45 |
-
|
| 46 |
-
if isinstance(result, list) and 'generated_text' in result[0]:
|
| 47 |
-
return result[0]['generated_text'].strip()
|
| 48 |
-
elif isinstance(result, dict) and 'generated_text' in result:
|
| 49 |
-
return result['generated_text'].strip()
|
| 50 |
-
return str(result)
|
| 51 |
-
|
| 52 |
-
def _build_prompt(self, domain: str, context: str, categories: str, items: str) -> str:
|
| 53 |
-
return f"""
|
| 54 |
-
You are a {domain} expert working within a {context} context.
|
| 55 |
-
Classify the following items into the specified categories using your domain expertise.
|
| 56 |
-
|
| 57 |
-
Categories:
|
| 58 |
{categories}
|
| 59 |
|
| 60 |
Items to classify:
|
| 61 |
{items}
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
Other
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
|
|
|
| 1 |
+
from tools.base import Tool
|
| 2 |
+
from mistral_hf_wrapper import MistralInference
|
|
|
|
| 3 |
|
| 4 |
+
class ClassifierTool(Tool):
|
| 5 |
+
name = 'open_classifier'
|
| 6 |
+
description = "Classifies given items into categories from a specific knowledge area."
|
| 7 |
|
| 8 |
inputs = {
|
| 9 |
+
'knowledge_area': {
|
| 10 |
'type': 'string',
|
| 11 |
+
'description': 'Domain or field for classification (e.g., biology, technology).',
|
| 12 |
},
|
| 13 |
+
'environment': {
|
| 14 |
'type': 'string',
|
| 15 |
+
'description': 'Brief description of the context in which items are evaluated.',
|
| 16 |
},
|
| 17 |
'categories': {
|
| 18 |
'type': 'string',
|
| 19 |
+
'description': 'Comma-separated list of categories to classify into.',
|
| 20 |
},
|
| 21 |
'items': {
|
| 22 |
'type': 'string',
|
| 23 |
+
'description': 'Comma-separated list of items to classify.',
|
| 24 |
},
|
| 25 |
}
|
| 26 |
output_type = 'string'
|
| 27 |
|
| 28 |
+
def __init__(self, model=None):
|
| 29 |
+
self.model = model or MistralInference()
|
| 30 |
+
super().__init__()
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def forward(self, knowledge_area: str, environment: str, categories: str, items: str) -> str:
|
| 33 |
+
prompt = self._build_prompt(knowledge_area, environment, categories, items)
|
| 34 |
+
response = self.model(prompt)
|
| 35 |
+
return response.strip()
|
| 36 |
|
| 37 |
+
def _build_prompt(self, knowledge_area, environment, categories, items) -> str:
|
| 38 |
+
return f"""You are an expert classifier in the field of {knowledge_area}.
|
| 39 |
+
Context: {environment}
|
| 40 |
+
You must assign each item below to one of the following categories:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
{categories}
|
| 42 |
|
| 43 |
Items to classify:
|
| 44 |
{items}
|
| 45 |
|
| 46 |
+
Rules:
|
| 47 |
+
- Use your {knowledge_area} knowledge only.
|
| 48 |
+
- Use the environment only if it's essential for disambiguation.
|
| 49 |
+
- Use "Other" only if an item clearly doesn't belong to any provided category.
|
| 50 |
+
- Format:
|
| 51 |
+
Category 1: item1, item2
|
| 52 |
+
Category 2: item3
|
| 53 |
+
Other: item4
|
| 54 |
"""
|