Spaces:
Sleeping
Sleeping
Create modules/module_1_intent_router.py
Browse files
modules/modules/module_1_intent_router.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ==========================================================
|
| 2 |
+
# PATHOGENAGENT - MODULE 1: INTENT ROUTER
|
| 3 |
+
# ==========================================================
|
| 4 |
+
|
| 5 |
+
import re
|
| 6 |
+
from typing import Dict, List, Tuple
|
| 7 |
+
|
| 8 |
+
class IntentRouter:
|
| 9 |
+
"""تشخیص نوع سوال و تعیین ابزارهای مورد نیاز"""
|
| 10 |
+
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.variant_patterns = [
|
| 13 |
+
r'c\.[0-9]+[a-z]?[0-9]*',
|
| 14 |
+
r'p\.[a-z][0-9]+[a-z]',
|
| 15 |
+
r'rs[0-9]+',
|
| 16 |
+
r'[a-z]+[0-9]+[a-z]',
|
| 17 |
+
r'[A-Z][0-9]+[A-Z]'
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
self.gene_list = [
|
| 21 |
+
"brca1", "brca2", "cftr", "tp53", "egfr", "kras",
|
| 22 |
+
"alk", "ros1", "her2", "apoe", "mlh1", "msh2",
|
| 23 |
+
"pten", "atm", "chek2", "palb2", "vhl"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
self.pathogen_keywords = [
|
| 27 |
+
"virus", "genome", "sequence", "organism", "strain",
|
| 28 |
+
"sars", "cov", "ebola", "influenza", "mers", "hiv",
|
| 29 |
+
"dengue", "zika", "tuberculosis", "malaria"
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
def detect_intent(self, query: str) -> Dict:
|
| 33 |
+
"""تشخیص نوع سوال"""
|
| 34 |
+
q = query.lower().strip()
|
| 35 |
+
confidence = 0.0
|
| 36 |
+
intent = "general"
|
| 37 |
+
tools = ["PubMed"]
|
| 38 |
+
|
| 39 |
+
# بررسی واریانت
|
| 40 |
+
for pattern in self.variant_patterns:
|
| 41 |
+
if re.search(pattern, q, re.IGNORECASE):
|
| 42 |
+
confidence = 0.9
|
| 43 |
+
intent = "variant"
|
| 44 |
+
tools = ["PubMed", "ClinVar"]
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
# بررسی ژن
|
| 48 |
+
if confidence < 0.7:
|
| 49 |
+
for gene in self.gene_list:
|
| 50 |
+
if gene in q:
|
| 51 |
+
confidence = 0.7
|
| 52 |
+
intent = "gene"
|
| 53 |
+
tools = ["PubMed", "Gene"]
|
| 54 |
+
break
|
| 55 |
+
|
| 56 |
+
# بررسی پاتوژن
|
| 57 |
+
if confidence < 0.5:
|
| 58 |
+
for keyword in self.pathogen_keywords:
|
| 59 |
+
if keyword in q:
|
| 60 |
+
confidence = 0.8
|
| 61 |
+
intent = "pathogen"
|
| 62 |
+
tools = ["PubMed", "GenBank"]
|
| 63 |
+
break
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
"query": query,
|
| 67 |
+
"intent": intent,
|
| 68 |
+
"confidence": confidence,
|
| 69 |
+
"required_tools": tools
|
| 70 |
+
}
|