Arko006 commited on
Commit
c4615d4
·
verified ·
1 Parent(s): dfa6c70

Upload agent/toxicologist_agent.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. agent/toxicologist_agent.py +85 -0
agent/toxicologist_agent.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from groq import Groq
4
+ from pydantic import BaseModel, Field, TypeAdapter
5
+ from typing import List, Optional
6
+ from config import GROQ_API_KEY, GROQ_MODEL
7
+
8
+
9
+ class ToxicologyReport(BaseModel):
10
+ target_assay: str = Field(description="The target assay endpoint evaluated.")
11
+ risk_level: str = Field(description="Overall risk level: Low, Moderate, High, or Critical.")
12
+ toxicophore_assessment: str = Field(description="Analysis of which structural features contribute to predicted toxicity.")
13
+ biochemical_mechanism: str = Field(description="Explanation of the biochemical mechanism linking structure to endpoint.")
14
+ structural_alerts_found: List[dict] = Field(description="List of structural alerts matched and their relevance.")
15
+ bioisosteric_replacements: List[str] = Field(description="Suggested safer bioisosteric replacements.")
16
+ confidence: str = Field(description="Confidence in this assessment based on model prediction and alert matching.")
17
+
18
+
19
+ class ToxicologistAgent:
20
+ def __init__(self):
21
+ self.client = Groq(api_key=GROQ_API_KEY)
22
+ self.model = GROQ_MODEL
23
+ self.schema_adapter = TypeAdapter(ToxicologyReport)
24
+ self.json_schema = self._build_strict_schema()
25
+
26
+ def _build_strict_schema(self) -> dict:
27
+ raw = self.schema_adapter.json_schema()
28
+ raw["additionalProperties"] = False
29
+ if "properties" in raw:
30
+ for prop_val in raw["properties"].values():
31
+ if isinstance(prop_val, dict) and "properties" in prop_val:
32
+ prop_val["additionalProperties"] = False
33
+ if "$defs" in raw:
34
+ for def_val in raw["$defs"].values():
35
+ if isinstance(def_val, dict):
36
+ def_val["additionalProperties"] = False
37
+ return raw
38
+
39
+ def generate_report(self, smiles: str, target_assay: str, predictions: list,
40
+ shap_attributions: list, high_attr_indices: list,
41
+ alerts_found: list) -> ToxicologyReport:
42
+ prompt = f"""You are an expert computational toxicologist. Analyze the following compound's toxicological profile.
43
+
44
+ COMPOUND SMILES: {smiles}
45
+ TARGET ASSAY: {target_assay} ({predictions[0].get('target_class', 'Unknown')})
46
+ PREDICTED PROBABILITY: {predictions[0].get('probability', 0.5):.4f}
47
+ PREDICTED CLASS: {predictions[0].get('predicted_class', 'Unknown')}
48
+
49
+ SHAP HIGH-ATTRIBUTION ATOM INDICES: {high_attr_indices}
50
+ FULL SHAP VECTOR: {shap_attributions}
51
+
52
+ STRUCTURAL ALERTS DETECTED: {json.dumps(alerts_found, indent=2)}
53
+
54
+ Based on this data:
55
+ 1. Identify which specific structural fragments (toxicophores) are driving the {predictions[0].get('predicted_class', 'Unknown')} prediction for {target_assay}.
56
+ 2. Explain the biochemical mechanism linking these fragments to the assay endpoint.
57
+ 3. Suggest 2-3 specific bioisosteric replacements that could mitigate toxicity while preserving core scaffold.
58
+ 4. Assess overall risk level.
59
+ 5. Provide confidence in the assessment.
60
+
61
+ Output a structured report adhering to the provided schema.
62
+ """
63
+ chat_completion = self.client.chat.completions.create(
64
+ model=self.model,
65
+ messages=[
66
+ {
67
+ "role": "system",
68
+ "content": "You are a professional computational toxicologist. Output reports strictly adhering to the provided JSON schema."
69
+ },
70
+ {"role": "user", "content": prompt}
71
+ ],
72
+ response_format={
73
+ "type": "json_schema",
74
+ "json_schema": {
75
+ "name": "Toxicology_Report",
76
+ "strict": True,
77
+ "schema": self.json_schema
78
+ }
79
+ },
80
+ temperature=0.1,
81
+ )
82
+
83
+ raw_json = chat_completion.choices[0].message.content
84
+ report = self.schema_adapter.validate_json(raw_json)
85
+ return report