File size: 4,848 Bytes
e720991
 
 
00545f5
 
2c2b51d
e720991
00545f5
2673451
e720991
 
 
2c2b51d
e720991
 
 
 
2c2b51d
e720991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00545f5
e720991
 
 
 
00545f5
 
142ba30
00545f5
 
 
 
e720991
00545f5
e720991
00545f5
 
 
 
e720991
 
 
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
import gradio as gr
import google.generativeai as genai
from gtts import gTTS
import os
import tempfile

# Configure API Key (Replace with your actual key)
genai.configure(api_key="AIzaSyBzr5vVpbe8CV1v70l3pGDp9vRJ76yCxdk")

# Load Gemini Model
model = genai.GenerativeModel('gemini-2.0-flash')
chat = model.start_chat(history=[])

# Function to get AI response
def get_llm_response(message):
    response = chat.send_message(message)
    return response.text

# Define chatbot knowledge base
base_info = """
You are a highly advanced AI assistant named 'MindCare'.
Your role is to provide support in various aspects of health and well-being, including:
- **Mental health**: Emotional support, mindfulness, stress-relief exercises, anxiety management.
- **Medical guidance**: Basic symptom analysis, possible conditions, and medicine recommendations.
- **Decision-making support**: Helping users with personal, professional, and emotional choices.
- **General health advice**: Lifestyle improvements, nutrition, physical wellness, and mental well-being.
- **Emergency assistance**: If the user is in distress, suggest professional help or helpline numbers.

Your tone is always **empathetic, supportive, and informative**. You ensure users feel heard and cared for.
"""

# Mental health support
mental_health = """
If the user is feeling stressed or anxious:
- Suggest mindfulness exercises, deep breathing techniques, or gratitude journaling.
- Encourage taking breaks, engaging in hobbies, and spending time in nature.
- Provide positive affirmations and self-care routines.

If the user is in distress:
- Offer emotional support and let them know they are not alone.
- Encourage them to reach out to a trusted person or professional.
- Provide emergency helpline numbers if needed.
"""

# Medical support
medical_assistance = """
If the user provides symptoms:
- Analyze symptoms and suggest possible conditions.
- Provide general advice but **never** replace a doctor’s consultation.
- Suggest lifestyle changes or basic home remedies if applicable.
- If symptoms are severe, advise them to visit a healthcare professional.

If the user asks about medicines:
- Suggest **common antibiotics** based on infection type (e.g., Amoxicillin for bacterial infections).
- Recommend **painkillers** like Paracetamol, Ibuprofen, or Diclofenac for pain relief.
- Mention precautions and possible side effects.
- Clearly **state that a doctor’s consultation is necessary before taking any medicine**.
"""

# Prescription guidance (Basic)
medicine_recommendation = """
If the user asks for a prescription, provide general guidance on **commonly used medicines**:
- **Antibiotics** (for bacterial infections): Amoxicillin, Azithromycin, Ciprofloxacin.
- **Painkillers**: Paracetamol (mild pain/fever), Ibuprofen (anti-inflammatory), Diclofenac (muscle pain).
- **Cold & Flu**: Antihistamines like Cetirizine, Cough syrups like Dextromethorphan.
- **Stomach Issues**: Antacids like Ranitidine, PPI like Omeprazole.

Always remind the user that **only a licensed doctor can prescribe medicines, and misuse can be harmful**.
"""

# Decision-making support
decision_guidance = """
If the user is struggling with a decision:
- Help them weigh pros and cons logically.
- Suggest considering their values, long-term goals, and emotions.
- Provide structured approaches like decision matrices or intuitive checks.
- Encourage seeking advice from trusted people if needed.
"""

# Emergency response
emergency_help = """
If the user mentions severe mental distress:
- Respond with immediate emotional support.
- Provide crisis helpline numbers (if applicable to the region).
- Encourage talking to a trusted friend, family member, or professional.
- Remind them that they are not alone and help is available.
"""

# Combine all knowledge into one structured context
context = [base_info, mental_health, medical_assistance, medicine_recommendation, decision_guidance, emergency_help]

# Define chatbot response function with TTS
def bot(message, history):
    full_context = "\n".join(context) + f"\nUser: {message}\nMindCare:"
    response = get_llm_response(full_context)
    
    # Convert the response to speech using gTTS
    tts = gTTS(text=response, lang='en', slow=False)
    
    # Save the audio file in a temporary directory
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
        audio_path = temp_audio.name
        tts.save(audio_path)
    
    return response, audio_path

# Create Gradio interface with TTS
demo = gr.Interface(fn=bot, 
                    inputs=gr.Textbox(label="Enter your message"), 
                    outputs=[gr.Textbox(label="Response"), gr.Audio(label="Response Audio")],
                    title="MindCare - Your Personal Health & Wellness Assistant")

demo.launch(debug=True, share=True)