-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp.py
More file actions
189 lines (145 loc) · 6.37 KB
/
sp.py
File metadata and controls
189 lines (145 loc) · 6.37 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import pandas as pd
from flask import Flask, request, jsonify
from flask_cors import CORS
import logging
import traceback
import google.generativeai as genai
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
# Set up logging
logging.basicConfig(level=logging.DEBUG)
# Initialize the Flask app
app = Flask(__name__)
CORS(app)
# Ensure the API key is set correctly
api_key = "AIzaSyDFiEwUSBtEuIqfMVeVoTjWUIP4NGMyYFA" # Replace with your actual API key
if not api_key:
raise ValueError("No API_KEY found. Please set the `API_KEY` environment variable.")
genai.configure(api_key=api_key)
# Create the model for Gemini
generation_config = {
"temperature": 1.05,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
}
try:
gemini_model = genai.GenerativeModel(
model_name="gemini-1.5-pro",
generation_config=generation_config,
)
except Exception as e:
logging.error(f"Error initializing Gemini model: {str(e)}")
raise
history = []
# Load dataset and preprocess it for the decision tree model
df = pd.read_csv(r'C:\Users\dhars\Downloads\Flutter Train\healthcare_app\doctor_schedules (1).csv')
df['intime'] = pd.to_datetime(df['intime'], format='%H:%M:%S').dt.hour
df['outtime'] = pd.to_datetime(df['outtime'], format='%H:%M:%S').dt.hour
# Encode categorical variables
le_place = LabelEncoder()
df['place'] = le_place.fit_transform(df['place'])
le_hospital_name = LabelEncoder()
df['hospital_name'] = le_hospital_name.fit_transform(df['hospital_name'])
le_doctor_name = LabelEncoder()
df['doctor_name'] = le_doctor_name.fit_transform(df['doctor_name'])
le_specialization = LabelEncoder()
df['specialization'] = le_specialization.fit_transform(df['specialization'])
le_qualification = LabelEncoder()
df['qualification'] = le_qualification.fit_transform(df['qualification'])
# Create a target variable
df['target'] = df.apply(lambda row: f"{row['hospital_name']}_{row['doctor_name']}_{row['qualification']}", axis=1)
le_target = LabelEncoder()
df['target'] = le_target.fit_transform(df['target'])
# Split data into features and target
X = df[['place', 'specialization', 'intime', 'outtime']]
y = df['target']
# Train the decision tree model
dt_model = DecisionTreeClassifier()
dt_model.fit(X, y)
# Function to extract hospitals based on location
def extract_hospitals_by_location(location):
logging.debug(f"Extracting hospitals for location: {location}")
# Convert location to the encoded format
location_encoded = le_place.transform([location])[0]
# Filter data by the specified location
filtered_df = df[df['place'] == location_encoded]
# Extract unique hospital names
hospitals_encoded = filtered_df['hospital_name'].unique()
hospitals = le_hospital_name.inverse_transform(hospitals_encoded)
logging.debug(f"Hospitals found: {hospitals}")
return hospitals.tolist()
@app.route('/hospitals', methods=['POST'])
def get_hospitals():
try:
data = request.get_json()
location = data.get('location')
if not location:
return jsonify({"error": "No location provided"}), 400
# Extract hospitals based on location
hospitals = extract_hospitals_by_location(location)
if hospitals:
return jsonify({"hospitals": hospitals}), 200
else:
return jsonify({"message": "No hospitals found."}), 404
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
logging.error(traceback.format_exc())
return jsonify({"error": str(e), "traceback": traceback.format_exc()}), 500
@app.route('/query', methods=['POST'])
def query():
try:
data = request.get_json()
logging.info(f"Received data: {data}")
user_input = data.get('query')
if not user_input:
return jsonify({"error": "No query provided"}), 400
logging.info(f"Processed query: {user_input}")
# Start the chat session
chat_session = gemini_model.start_chat(history=history)
# Send the user input to the chat session
response = chat_session.send_message(user_input)
# Extract the model's response text
model_response = response.text
# Append the conversation history
history.append({"role": "user", "parts": [user_input]})
history.append({"role": "model", "parts": [model_response]})
logging.info(f"Generated response: {model_response}")
return jsonify({"response": model_response})
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
logging.error(traceback.format_exc())
return jsonify({"error": str(e), "traceback": traceback.format_exc()}), 500
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
# Extract inputs from the request
place = data['place']
specialization = data['specialization']
time = data['time']
# Encode the inputs
place_encoded = le_place.transform([place])[0]
specialization_encoded = le_specialization.transform([specialization])[0]
time_parsed = pd.to_datetime(time, format='%H:%M:%S').hour
# Create input for the model
input_data = pd.DataFrame([[place_encoded, specialization_encoded, time_parsed, time_parsed]],
columns=['place', 'specialization', 'intime', 'outtime'])
# Predict using the model
predictions = dt_model.predict(input_data)
results = []
# Decode and prepare the results
for prediction in predictions:
decoded_target = le_target.inverse_transform([prediction])[0]
hospital_name_encoded, doctor_name_encoded, qualification_encoded = map(int, decoded_target.split('_'))
result = {
'place': place,
'hospital_name': le_hospital_name.inverse_transform([hospital_name_encoded])[0],
'doctor_name': le_doctor_name.inverse_transform([doctor_name_encoded])[0],
'specialization': specialization,
'qualification': le_qualification.inverse_transform([qualification_encoded])[0]
}
results.append(result)
return jsonify(results)
if __name__ == '__main__':
app.run(debug=True, host='192.168.137.1', port=5000)