szili2011 commited on
Commit
0440734
1 Parent(s): f5bdeee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -28
app.py CHANGED
@@ -1,36 +1,52 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
 
4
 
5
- # Load the trained model locally
6
- model_path = 'doctor_ai_model.h5'
7
- model = tf.keras.models.load_model(model_path)
 
8
 
9
- # Tokenizer setup (you may need to recreate a tokenizer if your model requires it)
10
- # Assuming you have a tokenizer saved locally
11
- # tokenizer = load_your_tokenizer()
 
 
12
 
13
- def chatbot(question):
14
- # Preprocessing the question (similar to how you did it during training)
15
- # Convert the question into a format the model can understand (tokenization and padding)
16
- # Example placeholder for preprocessing:
17
- question_input = preprocess_question(question) # Replace with actual preprocessing
18
-
19
- # Get model prediction
20
- prediction = model.predict(question_input)
21
-
22
- # Postprocess the prediction into readable text
23
- answer = postprocess_prediction(prediction) # Replace with actual postprocessing
24
-
25
- return answer
26
 
27
- # Set up the Gradio interface
28
- iface = gr.Interface(fn=chatbot,
29
- inputs="text",
30
- outputs="text",
31
- title="Doctor AI - Offline",
32
- description="Ask Doctor AI your medical questions. **This chatbot works completely offline**.",
33
- article="This model is trained on a custom dataset and designed to work without an internet connection.")
34
 
35
- # Launch the interface
36
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
+ import pickle
5
 
6
+ # Paths to model and tokenizer
7
+ model_path = "doctor_ai_model.h5"
8
+ tokenizer_path = "tokenizer.pkl"
9
+ label_encoder_path = "label_encoder.pkl"
10
 
11
+ # Load the trained model
12
+ try:
13
+ model = tf.keras.models.load_model(model_path)
14
+ except Exception as e:
15
+ print(f"Error loading model: {e}")
16
 
17
+ # Load the tokenizer and label encoder
18
+ with open(tokenizer_path, 'rb') as handle:
19
+ tokenizer = pickle.load(handle)
 
 
 
 
 
 
 
 
 
 
20
 
21
+ with open(label_encoder_path, 'rb') as handle:
22
+ label_encoder = pickle.load(handle)
 
 
 
 
 
23
 
24
+ # Define the prediction function
25
+ def predict_answer(question):
26
+ try:
27
+ # Tokenize the input question
28
+ seq = tokenizer.texts_to_sequences([question])
29
+ padded_seq = tf.keras.preprocessing.sequence.pad_sequences(seq, maxlen=27) # Adjust maxlen to match your model
30
+
31
+ # Make prediction
32
+ prediction = model.predict(padded_seq)
33
+
34
+ # Convert prediction to label
35
+ predicted_label = label_encoder.inverse_transform(np.argmax(prediction, axis=1))
36
+
37
+ return predicted_label[0]
38
+ except Exception as e:
39
+ return f"Error during prediction: {e}"
40
+
41
+ # Define the Gradio interface
42
+ iface = gr.Interface(
43
+ fn=predict_answer,
44
+ inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your question..."),
45
+ outputs="text",
46
+ title="Doctor AI Chatbot",
47
+ description="This AI chatbot provides answers based on your medical-related questions. Works completely offline."
48
+ )
49
+
50
+ # Launch the Gradio app
51
+ if __name__ == "__main__":
52
+ iface.launch()