-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
215 lines (168 loc) · 8.02 KB
/
app.py
File metadata and controls
215 lines (168 loc) · 8.02 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import streamlit as st
import google.generativeai as genai
import pandas as pd
import requests
import os
import telebot
import geocoder
# ✅ Correct import
# ❌ Don't import configure/generate_content separately
if "name" not in st.session_state:
st.session_state.name = ""
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "location" not in st.session_state:
st.session_state.location = None
# ✅ First check API Key
API_KEY = os.getenv("GEMINI_API_KEY")
if not API_KEY:
st.error("❌ API Key not found. Set GEMINI_API_KEY as an environment variable.")
st.stop()
# ✅ Configure after checking
genai.configure(api_key=API_KEY)
# ✅ Create model instance
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
def get_gemini_response(input_text):
try:
response = model.generate_content(input_text)
if not response or not response.candidates or not response.candidates[0].content.parts:
return "⚠️ No valid response from the AI. Try rephrasing your request."
response_text = response.candidates[0].content.parts[0].text
return response_text
except Exception as e:
return f"⚠️ An error occurred: {str(e)}"
def authenticate_user(username, password):
user_data = pd.read_csv("users.csv")
if username in user_data["user_id"].values:
stored_password = user_data.loc[user_data["user_id"] == username, "passwords"].values[0]
if password == stored_password:
st.success("✅ Login successful!")
st.session_state.name = username
else:
st.error("❌ Incorrect password. Please try again.")
else:
st.error("❌ Username not found. Please create a new account.")
def create_new_account(new_userid, username, password, email, dob, height, weight, gender, blood_group):
user_data = pd.read_csv("users.csv")
if len(password) < 8:
st.warning("⚠️ Password must be at least 8 characters long.")
return
if username in user_data["user_name"].values:
st.error("❌ Username already exists. Choose a different username.")
return
new_account = pd.DataFrame({
"user_id": [new_userid], "user_name": [username], "passwords": [password],
"email": [email], "dob": [dob], "height": [height], "weight": [weight],
"gender": [gender], "blood_group": [blood_group]
})
user_data_updated = pd.concat([user_data, new_account], ignore_index=True)
user_data_updated.to_csv("users.csv", index=False)
st.success("✅ Account created successfully!")
def main():
st.title(":blue[Login]")
with st.form(key="login_form"):
userid = st.text_input("User Id:")
password = st.text_input("Enter password:", type="password")
submit_button = st.form_submit_button("Login")
if submit_button:
authenticate_user(userid, password)
with st.form(key="signup_form"):
new_userid = st.text_input("Enter new user ID")
name = st.text_input("Enter your name")
email = st.text_input("Enter your email")
new_password = st.text_input("New Password:", type="password")
dob = st.date_input("Enter your date of birth", min_value=pd.to_datetime("1900-01-01"), max_value=pd.to_datetime("today"))
height = st.number_input("Enter height (cm):", 0, 350)
weight = st.number_input("Enter weight (kg):", 0, 200)
gender = st.radio("Select gender:", ["Male", "Female", "Other"])
blood_group = st.selectbox("Select your blood group:", ["A+", "B+", "A-", "B-", "O+", "O-", "AB+", "AB-"])
create_account_button = st.form_submit_button("Create Account")
if create_account_button:
create_new_account(new_userid, name, new_password, email, dob, height, weight, gender, blood_group)
def chat_page():
st.markdown("<h1 style='text-align: center; color:#28b463;'>CURO BOT🤖</h1>", unsafe_allow_html=True)
st.markdown("<h5 style='text-align: center; color:#28b463;'>WHERE TECHNOLOGY MEETS CARE</h5>", unsafe_allow_html=True)
chat = st.chat_input("Say something...")
if chat:
st.session_state.chat_history.append(chat)
with st.chat_message("user"):
st.write(chat)
with st.chat_message("assistant"):
response = get_gemini_response(chat)
st.write(response)
def med():
st.title("Personalised Medicine 🏥")
symptom = st.text_input("Enter your symptom")
days = st.text_input("For how many days?")
other_symptoms = st.text_input("Other symptoms")
other_diseases = st.text_input("Do you have any other diseases?")
if symptom:
question_prompt = f"Generate a question to better understand the disease based on symptom={symptom}, days={days}, other symptoms={other_symptoms}"
question = get_gemini_response(question_prompt)
answer = st.text_input(question)
diagnosis_prompt = (
f"Predict the disease based on symptoms: {symptom}, duration: {days}, other symptoms: {other_symptoms}, answer to previous question: {answer}, "
f"and existing diseases: {other_diseases}. Provide remedies, medication (without dosage), and alternative treatments."
)
response = get_gemini_response(diagnosis_prompt)
st.write(response)
def predictor():
st.title("Medicine Information 📜")
medicine_name = st.text_input("Enter the name of the medicine")
if medicine_name:
prompt = f"Provide general information about '{medicine_name}'. Include composition, uses, side effects, and alternatives in a table format."
response = get_gemini_response(prompt)
st.write(response)
def emergency_sos():
st.title("🚨 SOS Alert System")
st.write("Send an emergency alert via Telegram bot with live location.")
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
CHAT_IDS = os.getenv("TELEGRAM_CHAT_IDS", "").split(",")
if not TELEGRAM_BOT_TOKEN or not CHAT_IDS:
st.error("❌ Telegram Bot credentials are missing. Set TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_IDS.")
return
name = st.text_input("Enter your name:", key="sos_name")
message = st.text_area("Enter your SOS message:", key="sos_message")
def get_live_location():
g = geocoder.ip('me')
if g.latlng:
return g.latlng
return None
def send_sos_telegram(user_name, message):
location = get_live_location()
full_message = f"🚨 Emergency Alert!\n{user_name} needs urgent help!\n\n📩 Message: {message}"
if location:
full_message += f"\n\n📍 Live Location: {location[0]}, {location[1]}"
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
for chat_id in CHAT_IDS:
chat_id = chat_id.strip()
try:
bot.send_message(chat_id, full_message)
if location:
bot.send_location(chat_id, latitude=location[0], longitude=location[1])
st.success(f"✅ SOS Alert sent successfully ")
except Exception as e:
st.error(f"❌ Failed to send SOS Alert : {e}")
if st.button("Send SOS Message"):
if name and message:
send_sos_telegram(name, message)
else:
st.warning("⚠️ Please fill in all fields.")
# Sidebar Navigation
with st.sidebar:
st.title("Curo Bot")
pages_select = st.selectbox("Navigation", ["Login", "Chatbot", "Personalised Medicine", "Medicine Info", "Emergency SOS"])
st.header("Chat History")
for msg in st.session_state['chat_history']:
st.write(f"{msg}")
# Main Pages
if pages_select == "Login":
main()
elif pages_select == "Chatbot":
chat_page()
elif pages_select == "Personalised Medicine":
med()
elif pages_select == "Medicine Info":
predictor()
elif pages_select == "Emergency SOS":
emergency_sos()