Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer, AutoModelForCausalLM, AutoTokenizer
|
3 |
+
from gtts import gTTS
|
4 |
+
from IPython.display import Audio
|
5 |
+
|
6 |
+
# Load Pretrained Model (GPT-2 for Reminders & Health Tips)
|
7 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
8 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
9 |
+
tokenizer.pad_token = tokenizer.eos_token
|
10 |
+
|
11 |
+
# Load DialoGPT (for chatbot functionality)
|
12 |
+
chatbot_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
13 |
+
chatbot_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
14 |
+
|
15 |
+
# Function to Generate Medication Reminder
|
16 |
+
def generate_reminder(prompt):
|
17 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
18 |
+
outputs = model.generate(
|
19 |
+
**inputs, max_length=50, num_return_sequences=1, temperature=0.7, do_sample=True
|
20 |
+
)
|
21 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
|
23 |
+
# Function to Generate Health Tip
|
24 |
+
def generate_health_tip(prompt):
|
25 |
+
return generate_reminder(prompt)
|
26 |
+
|
27 |
+
# Function to Get Chatbot Response
|
28 |
+
def chatbot_response(user_input):
|
29 |
+
inputs = chatbot_tokenizer.encode(user_input + chatbot_tokenizer.eos_token, return_tensors="pt")
|
30 |
+
outputs = chatbot_model.generate(inputs, max_length=1000, pad_token_id=chatbot_tokenizer.eos_token_id)
|
31 |
+
response = chatbot_tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
32 |
+
return response
|
33 |
+
|
34 |
+
# Function for Text-to-Speech
|
35 |
+
def text_to_speech(text):
|
36 |
+
tts = gTTS(text=text, lang='en')
|
37 |
+
tts.save("output.mp3")
|
38 |
+
return "output.mp3"
|
39 |
+
|
40 |
+
# Streamlit UI
|
41 |
+
st.title("HealthEase - Medication Reminder and Health Assistant")
|
42 |
+
|
43 |
+
# Medication Reminder
|
44 |
+
st.header("Generate Medication Reminder")
|
45 |
+
medication_input = st.text_input("Enter medication details (e.g., 'Reminder for insulin at 7 PM:')")
|
46 |
+
if medication_input:
|
47 |
+
reminder = generate_reminder(medication_input)
|
48 |
+
st.write("Generated Reminder:", reminder)
|
49 |
+
audio_file = text_to_speech(reminder)
|
50 |
+
st.audio(audio_file)
|
51 |
+
|
52 |
+
# Health Tip
|
53 |
+
st.header("Generate Health Tip")
|
54 |
+
health_tip_input = st.text_input("Enter health tip details (e.g., 'Daily health tip for a diabetic patient:')")
|
55 |
+
if health_tip_input:
|
56 |
+
health_tip = generate_health_tip(health_tip_input)
|
57 |
+
st.write("Generated Health Tip:", health_tip)
|
58 |
+
audio_file = text_to_speech(health_tip)
|
59 |
+
st.audio(audio_file)
|
60 |
+
|
61 |
+
# Chatbot
|
62 |
+
st.header("Conversational Chatbot")
|
63 |
+
user_query = st.text_input("Ask a question (e.g., 'What are the side effects of ibuprofen?')")
|
64 |
+
if user_query:
|
65 |
+
bot_reply = chatbot_response(user_query)
|
66 |
+
st.write("Chatbot Reply:", bot_reply)
|
67 |
+
|