Rename streamlit_app.py to app.py
Browse files- app.py +84 -0
- streamlit_app.py +0 -87
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import pickle
|
| 5 |
+
import requests
|
| 6 |
+
from collections import defaultdict
|
| 7 |
+
import random
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# GitHub-dan label_encoder yükləmək
|
| 11 |
+
def download_label_encoder():
|
| 12 |
+
url = "https://github.com/AxundovReyal/nlp-disease/raw/main/label_encoder.pkl"
|
| 13 |
+
headers = {}
|
| 14 |
+
github_token = os.getenv("GITHUB_TOKEN")
|
| 15 |
+
if github_token:
|
| 16 |
+
headers["Authorization"] = f"token {github_token}"
|
| 17 |
+
|
| 18 |
+
response = requests.get(url, headers=headers)
|
| 19 |
+
if response.status_code == 200:
|
| 20 |
+
with open("label_encoder.pkl", "wb") as f:
|
| 21 |
+
f.write(response.content)
|
| 22 |
+
print("label_encoder.pkl faylı uğurla yükləndi.")
|
| 23 |
+
else:
|
| 24 |
+
raise Exception(f"Fayl yüklənə bilmədi, error kodu: {response.status_code}")
|
| 25 |
+
|
| 26 |
+
# Model və label_encoder yüklənməsi
|
| 27 |
+
def load_model():
|
| 28 |
+
download_label_encoder()
|
| 29 |
+
with open("label_encoder.pkl", "rb") as f:
|
| 30 |
+
label_encoder = pickle.load(f)
|
| 31 |
+
|
| 32 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
| 33 |
+
model = BertForSequenceClassification.from_pretrained(
|
| 34 |
+
'bert-base-uncased',
|
| 35 |
+
num_labels=len(label_encoder.classes_)
|
| 36 |
+
)
|
| 37 |
+
model.eval()
|
| 38 |
+
return tokenizer, model, label_encoder
|
| 39 |
+
|
| 40 |
+
tokenizer, model, label_encoder = load_model()
|
| 41 |
+
|
| 42 |
+
# Prediction funksiyası
|
| 43 |
+
def predict_disease(text):
|
| 44 |
+
if not text.strip():
|
| 45 |
+
return "Please enter some symptoms!"
|
| 46 |
+
|
| 47 |
+
symptoms = [s.strip() for s in text.split(",") if s.strip()]
|
| 48 |
+
if not symptoms:
|
| 49 |
+
return "Please enter valid symptoms separated by commas!"
|
| 50 |
+
|
| 51 |
+
agg_probs = defaultdict(float)
|
| 52 |
+
n_shuffles = 10
|
| 53 |
+
for _ in range(n_shuffles):
|
| 54 |
+
random.shuffle(symptoms)
|
| 55 |
+
shuffled_text = ", ".join(symptoms)
|
| 56 |
+
inputs = tokenizer(shuffled_text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 57 |
+
with torch.no_grad():
|
| 58 |
+
outputs = model(**inputs)
|
| 59 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1).squeeze()
|
| 60 |
+
for i, p in enumerate(probs):
|
| 61 |
+
agg_probs[i] += p.item()
|
| 62 |
+
|
| 63 |
+
for k in agg_probs:
|
| 64 |
+
agg_probs[k] /= n_shuffles
|
| 65 |
+
|
| 66 |
+
top_3 = sorted(agg_probs.items(), key=lambda x: x[1], reverse=True)[:3]
|
| 67 |
+
results = []
|
| 68 |
+
for idx, prob in top_3:
|
| 69 |
+
label = label_encoder.classes_[idx]
|
| 70 |
+
results.append(f"{label} — Probability: {prob*100:.2f}%")
|
| 71 |
+
return "\n".join(results)
|
| 72 |
+
|
| 73 |
+
# Gradio interface
|
| 74 |
+
iface = gr.Interface(
|
| 75 |
+
fn=predict_disease,
|
| 76 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your symptoms separated by commas"),
|
| 77 |
+
outputs=gr.Textbox(),
|
| 78 |
+
title="Disease NLP Classifier",
|
| 79 |
+
description="Enter your symptoms (comma separated) and get top 3 predicted diseases."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Launch
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
iface.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
streamlit_app.py
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import streamlit as st
|
| 4 |
-
from transformers import BertTokenizer, BertForSequenceClassification # Burada BertTokenizer istifadə edirik
|
| 5 |
-
import torch
|
| 6 |
-
import pickle
|
| 7 |
-
import random
|
| 8 |
-
from collections import defaultdict
|
| 9 |
-
import requests
|
| 10 |
-
|
| 11 |
-
# GitHub-dan fayl yükləmək üçün funksiyanın təyin edilməsi
|
| 12 |
-
def download_label_encoder():
|
| 13 |
-
url = "https://github.com/AxundovReyal/nlp-disease/raw/main/label_encoder.pkl"
|
| 14 |
-
headers = {
|
| 15 |
-
"Authorization": f"token {os.getenv('GITHUB_TOKEN')}" # GitHub personal access token mühit dəyişəni olaraq qeyd olunmalı
|
| 16 |
-
}
|
| 17 |
-
response = requests.get(url, headers=headers)
|
| 18 |
-
|
| 19 |
-
if response.status_code == 200:
|
| 20 |
-
with open("label_encoder.pkl", "wb") as f:
|
| 21 |
-
f.write(response.content)
|
| 22 |
-
print("label_encoder.pkl faylı uğurla yükləndi.")
|
| 23 |
-
else:
|
| 24 |
-
raise Exception(f"Fayl yüklənə bilmədi, error kodu: {response.status_code}")
|
| 25 |
-
|
| 26 |
-
# Modelin və label_encoder-in yüklənməsi
|
| 27 |
-
@st.cache_resource
|
| 28 |
-
def load_model():
|
| 29 |
-
# GitHub-dan label_encoder yükləmək
|
| 30 |
-
download_label_encoder()
|
| 31 |
-
|
| 32 |
-
# Label encoder yüklənməsi əvvəlcə edilir
|
| 33 |
-
with open("label_encoder.pkl", "rb") as f:
|
| 34 |
-
label_encoder = pickle.load(f)
|
| 35 |
-
|
| 36 |
-
# Burada AutoTokenizer əvəzinə BertTokenizer istifadə edirik
|
| 37 |
-
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # BERT Tokenizer
|
| 38 |
-
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=len(label_encoder.classes_)) # BERT Model
|
| 39 |
-
|
| 40 |
-
model.eval()
|
| 41 |
-
|
| 42 |
-
return tokenizer, model, label_encoder
|
| 43 |
-
|
| 44 |
-
tokenizer, model, label_encoder = load_model()
|
| 45 |
-
|
| 46 |
-
st.title("Disease NLP Classifier")
|
| 47 |
-
|
| 48 |
-
text = st.text_area("Enter your symptoms separated by commas (e.g. fever, cough, headache):")
|
| 49 |
-
|
| 50 |
-
def predict(text_input):
|
| 51 |
-
inputs = tokenizer(text_input, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 52 |
-
with torch.no_grad():
|
| 53 |
-
outputs = model(**inputs)
|
| 54 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim=-1).squeeze()
|
| 55 |
-
return probs
|
| 56 |
-
|
| 57 |
-
if st.button("Predict"):
|
| 58 |
-
if not text.strip():
|
| 59 |
-
st.warning("Please enter some symptoms!")
|
| 60 |
-
else:
|
| 61 |
-
symptoms = [s.strip() for s in text.split(",") if s.strip()]
|
| 62 |
-
if not symptoms:
|
| 63 |
-
st.warning("Please enter valid symptoms separated by commas!")
|
| 64 |
-
else:
|
| 65 |
-
agg_probs = defaultdict(float)
|
| 66 |
-
n_shuffles = 10
|
| 67 |
-
for _ in range(n_shuffles):
|
| 68 |
-
random.shuffle(symptoms)
|
| 69 |
-
shuffled_text = ", ".join(symptoms)
|
| 70 |
-
probs = predict(shuffled_text)
|
| 71 |
-
for i, p in enumerate(probs):
|
| 72 |
-
agg_probs[i] += p.item()
|
| 73 |
-
for k in agg_probs:
|
| 74 |
-
agg_probs[k] /= n_shuffles
|
| 75 |
-
top_3 = sorted(agg_probs.items(), key=lambda x: x[1], reverse=True)[:3]
|
| 76 |
-
|
| 77 |
-
st.subheader("Top 3 Predicted Diseases (averaged over shuffled inputs):")
|
| 78 |
-
for idx, prob in top_3:
|
| 79 |
-
label = label_encoder.classes_[idx] # Etiketləri doğru alırıq
|
| 80 |
-
st.write(f"**{label}** — Probability: `{prob * 100:.2f}%`")
|
| 81 |
-
|
| 82 |
-
# Render port düzəlişi
|
| 83 |
-
if __name__ == "__main__":
|
| 84 |
-
port = int(os.environ.get("PORT", 8501))
|
| 85 |
-
sys.argv = ["streamlit", "run", "streamlit_app.py", f"--server.port={port}", "--server.address=0.0.0.0"]
|
| 86 |
-
from streamlit.web.cli import main
|
| 87 |
-
sys.exit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|