-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
66 lines (54 loc) · 2.84 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import tkinter as tk
from tkinter import Label, Entry, Text, Button
import requests
import json
class ChatbotUI:
def __init__(self, root):
self.root = root
self.root.title("Heart Disease Prediction")
self.create_form()
def create_form(self):
# Labels and Entry widgets for input
labels = ['Age','Sex','RestingBP','Cholesterol','FastingBS','RestingECG','MaxHR','ExerciseAngina','Oldpeak','ST_Slope']
self.entries = {label: Entry(self.root) for label in labels}
for i, label in enumerate(labels):
Label(self.root, text=label).grid(row=i, column=0, sticky='e')
self.entries[label].grid(row=i, column=1, pady=5, padx=5)
# Result text area
Label(self.root, text="Result:").grid(row=len(labels), column=0, sticky='e')
self.result_text = Text(self.root, height=2, width=30)
self.result_text.grid(row=len(labels), column=1, pady=5, padx=5)
# Submit button
submit_button = Button(self.root, text="Submit", command=self.predict)
submit_button.grid(row=len(labels) + 1, column=1, pady=10)
def preprocess_input(self):
# Collect input values from the Entry widgets
input_values = {label: float(entry.get()) if entry.get() and label not in ['Sex', 'RestingECG', 'ExerciseAngina', 'ST_Slope'] else entry.get()
for label, entry in self.entries.items()}
return input_values
def predict(self):
# Preprocess input data
input_values = self.preprocess_input()
# Print the collected input data
print("Collected Input Data:", input_values)
# Make a request to the Flask API endpoint for prediction
api_url = 'http://localhost:5000/predict' # Assuming Flask app is running on localhost:5000
response = requests.post(api_url, json={'new_record': input_values})
# Check if the request was successful (status code 200)
if response.status_code == 200:
try:
# Attempt to parse the JSON response
predictions = response.json()
result_text = f"Predicted Chest Pain Type: {predictions['predicted_chest_pain_type']}, Heart Patient : {predictions['predicted_patient_has_disease']}"
self.result_text.delete(1.0, tk.END) # Clear previous result
self.result_text.insert(tk.END, result_text)
except json.JSONDecodeError:
self.result_text.delete(1.0, tk.END)
self.result_text.insert(tk.END, "Error decoding JSON in the response.")
else:
self.result_text.delete(1.0, tk.END)
self.result_text.insert(tk.END, f"Error: Received a non-OK response with status code {response.status_code}\n{response.text}")
if __name__ == "__main__":
root = tk.Tk()
chatbot_ui = ChatbotUI(root)
root.mainloop()