Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,39 @@
|
|
1 |
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
|
2 |
import tensorflow as tf
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Load the tokenizer and model
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
# Clean the input text
|
11 |
def clean_input(symptom_text):
|
12 |
-
# Remove
|
13 |
symptom_text = ''.join([c for c in symptom_text if ord(c) < 128])
|
14 |
-
symptom_text = symptom_text.lower() #
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Define the predict function
|
18 |
def predict(symptom_text, chat_history=[]):
|
@@ -58,7 +79,7 @@ def predict(symptom_text, chat_history=[]):
|
|
58 |
|
59 |
# Add conversation history
|
60 |
chat_history.append(("User", symptom_text))
|
61 |
-
chat_history.append(("AI", f"Predicted Diagnosis: {diagnosis}
|
62 |
|
63 |
except Exception as e:
|
64 |
chat_history.append(("AI", f"Error: {str(e)}"))
|
@@ -67,7 +88,7 @@ def predict(symptom_text, chat_history=[]):
|
|
67 |
|
68 |
# Gradio UI
|
69 |
with gr.Blocks() as interface:
|
70 |
-
gr.Markdown("<h1 style='text-align: center; font-size:
|
71 |
chatbot = gr.Chatbot()
|
72 |
input_box = gr.Textbox(show_label=False, placeholder="Describe your symptoms here...")
|
73 |
send_button = gr.Button("Send")
|
|
|
1 |
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
|
2 |
import tensorflow as tf
|
3 |
import gradio as gr
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
from nltk.corpus import stopwords
|
7 |
+
from nltk.stem import PorterStemmer
|
8 |
+
|
9 |
+
# Ensure you have the necessary nltk resources
|
10 |
+
import nltk
|
11 |
+
nltk.download('stopwords')
|
12 |
+
|
13 |
+
# Caching the model locally to avoid re-downloading
|
14 |
+
MODEL_NAME = "Zabihin/Symptom_to_Diagnosis"
|
15 |
+
CACHE_DIR = "./cached_model"
|
16 |
+
if not os.path.exists(CACHE_DIR):
|
17 |
+
os.makedirs(CACHE_DIR)
|
18 |
|
19 |
# Load the tokenizer and model
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir=CACHE_DIR)
|
21 |
+
model = TFAutoModelForSequenceClassification.from_pretrained(MODEL_NAME, cache_dir=CACHE_DIR)
|
22 |
+
|
23 |
+
# Initialize stopwords and stemmer
|
24 |
+
stop_words = set(stopwords.words('english'))
|
25 |
+
stemmer = PorterStemmer()
|
26 |
|
27 |
+
# Clean the input text with advanced preprocessing
|
28 |
def clean_input(symptom_text):
|
29 |
+
# Remove non-ASCII characters and convert to lowercase
|
30 |
symptom_text = ''.join([c for c in symptom_text if ord(c) < 128])
|
31 |
+
symptom_text = symptom_text.lower().strip() # Remove leading/trailing spaces
|
32 |
+
|
33 |
+
# Remove stopwords and apply stemming
|
34 |
+
words = symptom_text.split()
|
35 |
+
filtered_words = [stemmer.stem(word) for word in words if word not in stop_words]
|
36 |
+
return ' '.join(filtered_words)
|
37 |
|
38 |
# Define the predict function
|
39 |
def predict(symptom_text, chat_history=[]):
|
|
|
79 |
|
80 |
# Add conversation history
|
81 |
chat_history.append(("User", symptom_text))
|
82 |
+
chat_history.append(("AI", f"Predicted Diagnosis: **{diagnosis}**. {description} Please consult a doctor for more accurate results."))
|
83 |
|
84 |
except Exception as e:
|
85 |
chat_history.append(("AI", f"Error: {str(e)}"))
|
|
|
88 |
|
89 |
# Gradio UI
|
90 |
with gr.Blocks() as interface:
|
91 |
+
gr.Markdown("<h1 style='text-align: center; font-size: 50px; margin-top: 40px; margin-bottom: 40px;'>Medi Mind - Your AI Health Assistant</h1>")
|
92 |
chatbot = gr.Chatbot()
|
93 |
input_box = gr.Textbox(show_label=False, placeholder="Describe your symptoms here...")
|
94 |
send_button = gr.Button("Send")
|