Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,87 +4,72 @@ import nltk
|
|
4 |
from nltk.tokenize import word_tokenize
|
5 |
from nltk.corpus import stopwords
|
6 |
|
|
|
7 |
nltk.download('punkt')
|
8 |
nltk.download("stopwords")
|
9 |
|
10 |
# Buat objek terjemahan
|
11 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-id-en")
|
12 |
terjemahkan = pipeline("translation", model="Helsinki-NLP/opus-mt-en-id")
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
# Pipeline generasi teks dengan format pesan
|
18 |
-
text_generation = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
19 |
-
|
20 |
-
def preprocess_text(teks):
|
21 |
-
# Pra-pemrosesan teks menggunakan NLTK
|
22 |
tokens = word_tokenize(teks)
|
23 |
tokens = [token for token in tokens if token.isalnum()] # Menghapus kata bukan alfanumerik
|
24 |
tokens = [token for token in tokens if token.lower() not in stopwords.words("indonesian")] # Menghapus stopword
|
25 |
preprocessed_text = " ".join(tokens)
|
26 |
-
|
27 |
return preprocessed_text
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
# Pra-pemrosesan teks gejala
|
32 |
-
gejala_diproses = preprocess_text(gejala)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
# kepercayaan = klasifikasi[0]["score"]
|
44 |
-
|
45 |
-
# Menggunakan format pesan untuk definisi penyakit
|
46 |
-
definisi = text_generation([{
|
47 |
-
"role": "user",
|
48 |
-
"content": f"You are a doctor who needs to diagnose a patient's illness. In your opinion, if I a have a {terjemahan_inggris}, what illness could it be?"
|
49 |
-
}])
|
50 |
-
definisi_teks = definisi[0]['generated_text']
|
51 |
-
|
52 |
-
# Melakukan terjemahan definisi kembali ke bahasa Indonesia
|
53 |
-
terjemahan_definisi = terjemahkan(definisi_teks, max_length=100)
|
54 |
-
definisi = terjemahan_definisi[0]["translation_text"]
|
55 |
|
56 |
-
|
57 |
-
return definisi
|
58 |
-
|
59 |
-
except Exception as e:
|
60 |
-
return "Tidak dapat mendiagnosis", 0.0, "Terjadi kesalahan saat mendiagnosis penyakit."
|
61 |
-
|
62 |
-
# Halaman Streamlit
|
63 |
-
st.title("Diagnosis Penyakit")
|
64 |
-
|
65 |
-
with st.form("diagnosis_form"):
|
66 |
-
# Masukan gejala
|
67 |
-
gejala = st.text_area("Masukkan gejala Anda:", "")
|
68 |
-
|
69 |
-
# Masukan usia
|
70 |
-
usia = st.number_input("Masukkan usia Anda:", min_value=0, max_value=120)
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from nltk.tokenize import word_tokenize
|
5 |
from nltk.corpus import stopwords
|
6 |
|
7 |
+
# Download NLTK data
|
8 |
nltk.download('punkt')
|
9 |
nltk.download("stopwords")
|
10 |
|
11 |
# Buat objek terjemahan
|
12 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-id-en")
|
13 |
terjemahkan = pipeline("translation", model="Helsinki-NLP/opus-mt-en-id")
|
14 |
+
pipe1 = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
15 |
+
pipe2 = pipeline("text-generation", model="SumayyaAli/tiny-llama-1.1b-chat-medical")
|
16 |
|
17 |
+
# Pra-pemrosesan teks menggunakan NLTK
|
18 |
+
def pra_pemrosesan_teks(teks):
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
tokens = word_tokenize(teks)
|
20 |
tokens = [token for token in tokens if token.isalnum()] # Menghapus kata bukan alfanumerik
|
21 |
tokens = [token for token in tokens if token.lower() not in stopwords.words("indonesian")] # Menghapus stopword
|
22 |
preprocessed_text = " ".join(tokens)
|
|
|
23 |
return preprocessed_text
|
24 |
|
25 |
+
# Streamlit UI
|
26 |
+
st.title("Diagnosa Penyakit Berdasarkan Gejala")
|
|
|
|
|
27 |
|
28 |
+
# Masukkan gejala, usia, dan jenis kelamin
|
29 |
+
usia = st.number_input("Masukkan usia Anda:", min_value=0, max_value=120)
|
30 |
+
jenis_kelamin = st.selectbox("Masukkan jenis kelamin Anda:", ["Laki-laki", "Perempuan"])
|
31 |
+
teks_indonesia = st.text_area("Masukkan gejala Anda:")
|
32 |
|
33 |
+
if st.button("Diagnosa"):
|
34 |
+
if teks_indonesia and usia and jenis_kelamin:
|
35 |
+
# Melakukan terjemahan gejala dari ID ke EN
|
36 |
+
terjemahan = translator(teks_indonesia, max_length=100)
|
37 |
+
terjemahan_inggris = terjemahan[0]["translation_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
gabungan = f"I am {usia} years old, {jenis_kelamin}. And my current symptoms are {terjemahan_inggris}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
# Pesan untuk model
|
42 |
+
messages = [
|
43 |
+
{"role": "system", "content": "You are a doctor who needs to diagnose a patient's illness. Provide one diagnosis that you believe is most confident."},
|
44 |
+
{"role": "user", "content": f"Based on your assessment, {gabungan}, what illness could it be?"}
|
45 |
+
]
|
46 |
+
|
47 |
+
# Fungsi untuk mendapatkan content dari role 'assistant'
|
48 |
+
def get_assistant_content(response):
|
49 |
+
generated_text = response[0]['generated_text']
|
50 |
+
return generated_text
|
51 |
+
|
52 |
+
# Dapatkan response dari pipe1 (model TinyLlama)
|
53 |
+
response1 = pipe1(messages, num_return_sequences=1, truncation=True)
|
54 |
+
assistant_content1 = get_assistant_content(response1)
|
55 |
+
|
56 |
+
# Dapatkan response dari pipe2 (model SumayyaAli)
|
57 |
+
response2 = pipe2(messages, num_return_sequences=1, truncation=True)
|
58 |
+
assistant_content2 = get_assistant_content(response2)
|
59 |
+
|
60 |
+
# Gabungkan hasil dari pipe1 dan pipe2 untuk pertanyaan final
|
61 |
+
kalimat = [{'role': 'user', 'content': f"{assistant_content1}. {assistant_content2}. Based on these two sentences, what is your final conclusion of my current symptom? Please provide a brief answer with one diagnosis."}]
|
62 |
+
|
63 |
+
# Dapatkan hasil akhir dari diagnosis
|
64 |
+
hasil = pipe1(kalimat)
|
65 |
+
assistant_content3 = get_assistant_content(hasil)
|
66 |
+
|
67 |
+
# Terjemahkan hasil akhir ke bahasa Indonesia
|
68 |
+
terjemahan_hasil = terjemahkan(assistant_content3, max_length=100)
|
69 |
+
diagnosa_terjemahan = terjemahan_hasil[0]["translation_text"]
|
70 |
+
|
71 |
+
# Menampilkan hasil ke Streamlit
|
72 |
+
st.subheader("Hasil Diagnosis:")
|
73 |
+
st.write(f"Diagnosis: {diagnosa_terjemahan}")
|
74 |
+
else:
|
75 |
+
st.error("Harap isi semua kolom sebelum menekan tombol Diagnosis.")
|