samyak152002 commited on
Commit
c201eb6
1 Parent(s): a9fb77e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -9
app.py CHANGED
@@ -1,16 +1,72 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
3
 
4
- # Load the model from Hugging Face
5
- classifier = pipeline("text-classification", model="samyak152002/my-disease-classifier-sih")
 
6
 
7
- # Define the prediction function
8
- def predict(text):
9
- result = classifier(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  return result
11
 
12
- # Set up the Gradio interface
13
- interface = gr.Interface(fn=predict, inputs="text", outputs="json", title="Disease Classifier")
 
 
 
 
 
 
 
 
 
 
14
 
15
  # Launch the app
16
- interface.launch()
 
 
1
  import gradio as gr
2
+ import torch
3
+ import torch.nn.functional as F
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+ import pandas as pd
6
 
7
+ # Load the dataset and create label mappings
8
+ df = pd.read_csv('bert_train.csv') # Update with the correct path
9
+ df["label"] = df["Label"]
10
 
11
+ # Create int2label and label2int mappings
12
+ int2label = {i: disease for i, disease in enumerate(df['label'].unique())}
13
+ label2int = {v: k for k, v in int2label.items()}
14
+
15
+ # Load the model and tokenizer
16
+ model_name = "your-huggingface-model-path" # Replace with your Hugging Face model path
17
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
18
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
19
+
20
+ # Function to classify text and return the top 3 diseases
21
+ def classify_text(text):
22
+ # Set device: GPU if available, else CPU
23
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
24
+
25
+ # Move the model to the correct device
26
+ model.to(device)
27
+
28
+ # Tokenize the input and move it to the same device as the model
29
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
30
+
31
+ # Perform inference
32
+ outputs = model(**inputs)
33
+
34
+ # Get the logits (raw scores)
35
+ logits = outputs.logits
36
+
37
+ # Apply softmax to convert logits into probabilities
38
+ probabilities = F.softmax(logits, dim=1)
39
+
40
+ # Convert the probabilities tensor to a list for easy display
41
+ prob_list = probabilities[0].tolist()
42
+
43
+ # Zip together the disease labels with their respective probabilities
44
+ disease_probs = {int2label[i]: prob for i, prob in enumerate(prob_list)}
45
+
46
+ # Sort the diseases by their probabilities in descending order
47
+ sorted_disease_probs = dict(sorted(disease_probs.items(), key=lambda item: item[1], reverse=True))
48
+
49
+ # Get the top 3 diseases
50
+ top_3_diseases = list(sorted_disease_probs.items())[:3]
51
+
52
+ # Format the result for display
53
+ result = "\n".join([f"{disease}: {prob:.4f}" for disease, prob in top_3_diseases])
54
+
55
  return result
56
 
57
+ # Gradio interface
58
+ def predict_disease(text):
59
+ return classify_text(text)
60
+
61
+ # Define the Gradio interface
62
+ iface = gr.Interface(
63
+ fn=predict_disease,
64
+ inputs="text",
65
+ outputs="text",
66
+ title="Disease Prediction",
67
+ description="Enter your symptoms, and the model will predict the top 3 most likely diseases with probabilities."
68
+ )
69
 
70
  # Launch the app
71
+ if __name__ == "__main__":
72
+ iface.launch()