Mitul6299 commited on
Commit
5e466b4
Β·
verified Β·
1 Parent(s): ad092cb

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +128 -0
  2. requirements.txt +17 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import torch
4
+ import numpy as np
5
+ import pandas as pd
6
+ import faiss
7
+ import base64
8
+ import tempfile
9
+ import speech_recognition as sr
10
+ from gtts import gTTS
11
+ from io import BytesIO
12
+ from PIL import Image
13
+ from sentence_transformers import SentenceTransformer
14
+ from transformers import (
15
+ AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig,
16
+ pipeline, AutoFeatureExtractor, AutoModelForAudioClassification,
17
+ AutoImageProcessor, AutoModelForImageClassification,
18
+ AutoModelForSequenceClassification
19
+ )
20
+ import gradio as gr
21
+
22
+ # Device setup
23
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
24
+
25
+ # Model loading (global, runs on app start)
26
+ PRIMARY_MODEL = "tiiuae/falcon-rw-1b"
27
+ quantization_config = BitsAndBytesConfig(
28
+ load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16
29
+ )
30
+ tokenizer = AutoTokenizer.from_pretrained(PRIMARY_MODEL)
31
+ model = AutoModelForCausalLM.from_pretrained(
32
+ PRIMARY_MODEL, device_map="auto", quantization_config=quantization_config
33
+ )
34
+
35
+ # Sentiment, emotion, ABSA, etc. (load all pipelines as in notebook)
36
+ sentiment_pipe = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest", device=device.index if 'cuda' in str(device) else -1)
37
+ emotion_pipe = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True, device=device.index if 'cuda' in str(device) else -1)
38
+ absa_pipe = pipeline("text-classification", model="yangheng/deberta-v3-base-absa-v1.1", device=device.index if 'cuda' in str(device) else -1)
39
+
40
+ # Embed model for safety/RAG
41
+ embed_model = SentenceTransformer("all-MiniLM-L6-v2", device=device)
42
+
43
+ # Safety model
44
+ safety_tokenizer = AutoTokenizer.from_pretrained("unitary/toxic-bert")
45
+ safety_model = AutoModelForSequenceClassification.from_pretrained("unitary/toxic-bert").to(device)
46
+
47
+ # Voice emotion
48
+ feature_extractor = AutoFeatureExtractor.from_pretrained("superb/hubert-base-superb-er")
49
+ ser_model = AutoModelForAudioClassification.from_pretrained("superb/hubert-base-superb-er").to(device)
50
+
51
+ # Facial emotion
52
+ face_processor = AutoImageProcessor.from_pretrained("dima806/facial_emotions_image_detection")
53
+ face_model = AutoModelForImageClassification.from_pretrained("dima806/facial_emotions_image_detection").to(device)
54
+
55
+ # RAG setup
56
+ RAG_XLSX_PATH = "https://raw.githubusercontent.com/Mitul060299/Hackathon/main/RAG_Knowledge_Base_WithID.xlsx"
57
+ rag_df = pd.read_excel(RAG_XLSX_PATH)
58
+ documents = rag_df["Knowledge Entry"].dropna().astype(str).tolist()
59
+ doc_ids = rag_df["ID"].dropna().astype(str).tolist() if "ID" in rag_df.columns else [str(i) for i in range(len(documents))]
60
+ doc_embeddings = embed_model.encode(documents, convert_to_numpy=True, normalize_embeddings=True)
61
+ dim = doc_embeddings.shape[1]
62
+ index = faiss.IndexFlatIP(dim)
63
+ index.add(doc_embeddings)
64
+
65
+ # Safety keywords/embeddings (as in notebook)
66
+ unsafe_keywords = ["suicide", "kill myself", "self harm", "hurt myself", "end my life", "overdose", "cutting", "hang myself", "can't go on", "want to die", "give up on life", "life is pointless", "i see no future", "end it all"]
67
+ unsafe_emb = embed_model.encode(unsafe_keywords, convert_to_tensor=True)
68
+ CRISIS_MESSAGE = "πŸ’› I’m concerned about your safety. I can’t assist with that here. Please contact local emergency services or a crisis helpline right now.\n\nIf in India: AASRA +91-9820466726\nUS: 988\nUK: Samaritans 116 123"
69
+
70
+ # Aspect keywords (from notebook)
71
+ _ASPECT_KEYWORDS = {
72
+ 'girlfriend','boyfriend','partner','husband','wife','relationship','marriage','heartbreak','breakup','divorce',
73
+ 'family','mother','father','parent','sibling','friend',
74
+ 'job','career','work','boss','manager','colleague','layoff','termination','unemployment','job loss',
75
+ 'study','school','college','university','exam','test','marks','grades','education',
76
+ 'depression','depressed','anxiety','stressed','stress','fear','worry','lonely','isolation',
77
+ 'sad','sadness','grief','loss','trauma','hopeless','confused',
78
+ 'angry','anger','frustrated','irritated',
79
+ 'health','illness','sick','tired','fatigue','disease','mental health','therapy','counseling',
80
+ 'change','moving','transition'
81
+ }
82
+
83
+ # All functions from notebook (generate_from_model, detect_sentiment, detect_text_emotion, detect_absa, is_unsafe_message, soft_duplicate_filter, retrieve_docs, detect_voice_emotion, detect_facial_emotion, detect_intent, generate_contextual_response, build_prompt_enhanced, generate_response_pipeline_enhanced)
84
+ # ... (Copy-paste all function definitions from the notebook pages here. I've omitted them for brevity in this response, but include them fully in your app.py. They start from generate_from_model in Cell 4 and go through to generate_response_pipeline_enhanced in Cell 14.)
85
+
86
+ # Global history for duplicate filter
87
+ _previous_responses = []
88
+
89
+ # Gradio chatbot function
90
+ def chatbot_fn(message, history, audio, image):
91
+ prev_user_messages = [h[0] for h in history] # User messages from history
92
+
93
+ user_text = message
94
+ voice_path = audio
95
+ face_path = image
96
+
97
+ if audio:
98
+ recognizer = sr.Recognizer()
99
+ with sr.AudioFile(audio) as source:
100
+ audio_data = recognizer.record(source)
101
+ user_text = recognizer.recognize_google(audio_data) if not user_text else user_text
102
+
103
+ reply, te, tes, sent, aspects = generate_response_pipeline_enhanced(
104
+ user_text, prev_user_messages, voice_audio_path=voice_path, face_image_path=face_path
105
+ )
106
+
107
+ # TTS for voice output
108
+ tts = gTTS(reply)
109
+ audio_buffer = BytesIO()
110
+ tts.write_to_fp(audio_buffer)
111
+ audio_buffer.seek(0)
112
+
113
+ return reply, audio_buffer
114
+
115
+ # Gradio interface
116
+ with gr.Blocks() as demo:
117
+ gr.Markdown("# Mental Health Chatbot")
118
+ chatbot = gr.Chatbot()
119
+ msg = gr.Textbox(placeholder="Type your message or use mic/webcam...")
120
+ audio_in = gr.Audio(source="microphone", type="filepath", label="Speak (optional)")
121
+ image_in = gr.Image(source="webcam", type="filepath", label="Webcam (optional)")
122
+ audio_out = gr.Audio(label="Bot Response (Voice)", autoplay=True)
123
+
124
+ msg.submit(
125
+ chatbot_fn, [msg, chatbot, audio_in, image_in], [msg, audio_out]
126
+ ).then(lambda: None, None, chatbot, queue=False) # Update chat history
127
+
128
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ bitsandbytes
4
+ accelerate
5
+ sentencepiece
6
+ sentence-transformers
7
+ numpy
8
+ faiss-cpu
9
+ pandas
10
+ openpyxl
11
+ librosa
12
+ soundfile
13
+ speechrecognition
14
+ gtts # For TTS output
15
+ gradio
16
+ pyaudio # For audio handling (if needed)
17
+ opencv-python-headless # For any image processing (cv2)