Fredaaaaaa commited on
Commit
30295b2
·
verified ·
1 Parent(s): fce7dac

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. app.py +113 -0
  3. hybrid_model.zip +3 -0
  4. labeled_severity.csv +3 -0
  5. requirements.txt +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ labeled_severity.csv filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import requests
4
+ import gradio as gr
5
+
6
+ # Define device
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+ print(f"🚀 Device: {device}")
9
+
10
+ # Load the pre-trained model and tokenizer from the Hugging Face directory where the model is saved
11
+ model_name = "Fredaaaaaa/hybrid_model" # Update this to the correct model path
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ text_model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=4)
14
+ text_model.to(device)
15
+
16
+ # Function to fetch drug features from an external API (e.g., PubChem)
17
+ def get_drug_features(drug1, drug2):
18
+ # You can modify this function to fetch additional features based on your API choice.
19
+ api_url = f'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{drug1}/property/SMILES,Pharmacology,Toxicity,Mechanism-of-action,Route-of-elimination,Metabolism/JSON'
20
+ response = requests.get(api_url)
21
+
22
+ if response.status_code == 200:
23
+ data = response.json()
24
+ drug_features = {
25
+ 'SMILES': data['PropertyTable']['Properties'][0].get('SMILES', 'No data'),
26
+ 'pharmacology': data['PropertyTable']['Properties'][0].get('Pharmacology', 'No data'),
27
+ 'toxicity': data['PropertyTable']['Properties'][0].get('Toxicity', 'No data'),
28
+ 'mechanism-of-action': data['PropertyTable']['Properties'][0].get('Mechanism-of-action', 'No data'),
29
+ 'route-of-elimination': data['PropertyTable']['Properties'][0].get('Route-of-elimination', 'No data'),
30
+ 'metabolism': data['PropertyTable']['Properties'][0].get('Metabolism', 'No data'),
31
+ }
32
+ return drug_features
33
+ else:
34
+ return None # If no data is returned, handle the missing values gracefully
35
+
36
+ # Define the Hybrid Model (already trained)
37
+ class HybridModel(torch.nn.Module):
38
+ def __init__(self, text_model, input_size, dropout_rate=0.3):
39
+ super(HybridModel, self).__init__()
40
+ self.text_model = text_model
41
+ self.fc1 = torch.nn.Linear(input_size, 128) # Fully connected layer for drug features
42
+ self.fc2 = torch.nn.Linear(128, 64) # Additional fully connected layer
43
+ self.fc3 = torch.nn.Linear(64, 4) # Output layer (4 classes for severity)
44
+ self.dropout = torch.nn.Dropout(dropout_rate)
45
+
46
+ def forward(self, input_ids, attention_mask, drug_features):
47
+ # Process the text data (interaction description) through BioBERT
48
+ text_outputs = self.text_model(input_ids=input_ids, attention_mask=attention_mask)
49
+ text_features = text_outputs.logits # Shape: (batch_size, num_labels)
50
+
51
+ # Pass the drug features through the fully connected layers
52
+ x = torch.relu(self.fc1(drug_features))
53
+ x = self.dropout(x)
54
+ x = torch.relu(self.fc2(x))
55
+ x = self.fc3(x)
56
+
57
+ # Combine the features from the text and drug inputs
58
+ combined = text_features + x
59
+ return combined
60
+
61
+ # Initialize the model
62
+ hybrid_model = HybridModel(text_model, input_size=3) # Example size, adjust based on your data
63
+ hybrid_model.to(device)
64
+
65
+ # Function to process the input and predict severity
66
+ def predict_severity(drug1, drug2):
67
+ # Fetch drug features from external API
68
+ drug_features = get_drug_features(drug1, drug2)
69
+
70
+ if not drug_features:
71
+ return "Error: Could not fetch drug features from the API."
72
+
73
+ # Preprocess text data (interaction description)
74
+ interaction_description = f"{drug1} interacts with {drug2}" # Example interaction description
75
+ inputs = tokenizer(interaction_description, return_tensors="pt", padding=True, truncation=True, max_length=128)
76
+ input_ids = inputs['input_ids'].to(device)
77
+ attention_mask = inputs['attention_mask'].to(device)
78
+
79
+ # Process drug features (SMILES, pharmacology, toxicity, etc.)
80
+ drug_feature_values = [drug_features['SMILES'], drug_features['pharmacology'], drug_features['toxicity']]
81
+
82
+ # Convert to numerical features (handle strings gracefully)
83
+ numerical_features = []
84
+ for feature in drug_feature_values:
85
+ if isinstance(feature, str): # If the feature is a string, convert it to a dummy number or handle it
86
+ numerical_features.append(0) # Placeholder value for missing data
87
+ else:
88
+ numerical_features.append(feature)
89
+
90
+ drug_features_tensor = torch.tensor(numerical_features, dtype=torch.float32).unsqueeze(0).to(device)
91
+
92
+ # Run the model to get predictions
93
+ hybrid_model.eval()
94
+ with torch.no_grad():
95
+ outputs = hybrid_model(input_ids, attention_mask, drug_features_tensor)
96
+
97
+ # Get the predicted class
98
+ prediction = torch.argmax(outputs, dim=1).item()
99
+
100
+ # Map the predicted class index to the severity label
101
+ severity_labels = ["No interaction", "Mild", "Moderate", "Severe"]
102
+ return severity_labels[prediction]
103
+
104
+ # Gradio Interface
105
+ interface = gr.Interface(
106
+ fn=predict_severity,
107
+ inputs=[gr.Textbox(label="Drug 1"), gr.Textbox(label="Drug 2")],
108
+ outputs="text",
109
+ live=True
110
+ )
111
+
112
+ # Launch the interface
113
+ interface.launch()
hybrid_model.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75567f724f1ddab1d748f5190031f11ff2b6cf532333801683ec44293a276dc4
3
+ size 401914674
labeled_severity.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:78f825bb07033ce4bbf16d4f58a756e2e113934a3f8274d298312c359a65edb9
3
+ size 31877277
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio