Russellml commited on
Commit
55f3a9e
·
verified ·
1 Parent(s): e4b0995

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +16 -9
  2. OMANI-Chatbot.py +90 -0
Dockerfile CHANGED
@@ -1,20 +1,27 @@
1
- FROM python:3.13.5-slim
 
2
 
 
3
  WORKDIR /app
4
 
 
5
  RUN apt-get update && apt-get install -y \
6
  build-essential \
7
- curl \
8
- git \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
- COPY requirements.txt ./
12
- COPY src/ ./src/
13
 
14
- RUN pip3 install -r requirements.txt
 
15
 
16
- EXPOSE 8501
 
17
 
18
- HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
 
19
 
20
- ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
 
1
+ # Use an official Python runtime
2
+ FROM python:3.11-slim
3
 
4
+ # Set working directory
5
  WORKDIR /app
6
 
7
+ # Install system dependencies (needed for audio + ML libs)
8
  RUN apt-get update && apt-get install -y \
9
  build-essential \
10
+ ffmpeg \
11
+ libsndfile1 \
12
  && rm -rf /var/lib/apt/lists/*
13
 
14
+ # Copy dependency list first
15
+ COPY requirements.txt .
16
 
17
+ # Install Python dependencies
18
+ RUN pip install --no-cache-dir -r requirements.txt
19
 
20
+ # Copy the full project
21
+ COPY . .
22
 
23
+ # Expose Streamlit default port
24
+ EXPOSE 8501
25
 
26
+ # Run the chatbot
27
+ CMD ["streamlit", "run", "OMANI-Chatbot.py", "--server.port=8501", "--server.address=0.0.0.0"]
OMANI-Chatbot.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from app.ui import consent_banner, emergency_resources, audio_input, display_latest_audio
3
+ from app.intent_analysis import analyze_intent
4
+ from app.crisis_toolchain import detect_crisis, get_session_id
5
+ from app.response_gen import generate_response
6
+ from app.tts_pipeline import text_to_speech
7
+ from app.rag_layer import setup_vectorstore
8
+ from app import config
9
+ import logging
10
+
11
+
12
+
13
+ # Configure logging with session_id
14
+ logging.basicConfig(
15
+ filename="crisis_log.txt",
16
+ level=logging.ERROR,
17
+ format="%(asctime)s - %(levelname)s - Session: %(session_id)s - %(message)s",
18
+ force=True
19
+ )
20
+ logging.getLogger().handlers[0].setFormatter(
21
+ logging.Formatter(
22
+ fmt="%(asctime)s - %(levelname)s - Session: %(session_id)s - %(message)s",
23
+ datefmt="%Y-%m-%d %H:%M:%S"
24
+ )
25
+ )
26
+ logging.getLogger().handlers[0].setLevel(logging.ERROR)
27
+
28
+ def main():
29
+ st.set_page_config(page_title="Omani Therapist Voice Bot", layout="centered")
30
+ st.title("🧠 Omani Therapist Voice Bot (Prototype)")
31
+
32
+ if not config.OPENAI_API_KEY:
33
+ st.error("OpenAI API key not found in .env file. Please set OPENAI_API_KEY.")
34
+ emergency_resources()
35
+ return
36
+
37
+ # Initialize vectorstore
38
+ try:
39
+ vectorstore = setup_vectorstore()
40
+ st.session_state.vectorstore = vectorstore
41
+ except Exception as e:
42
+ st.error(f"Failed to initialize vectorstore: {str(e)}. App may have limited functionality.")
43
+ logging.error(f"Vectorstore initialization failed: {str(e)}")
44
+
45
+ if not consent_banner():
46
+ st.error("❌ Consent not given. Redirected to emergency support:")
47
+ emergency_resources()
48
+ return
49
+
50
+ # Audio input and chat history
51
+ with st.spinner("Processing your input..."):
52
+ chat_history = audio_input()
53
+
54
+ # Process new transcript
55
+ if st.session_state.get("transcript"):
56
+ transcript = st.session_state.transcript
57
+ with st.spinner("Analyzing and generating response..."):
58
+ # Analyze intent (emotion + rule-based intent)
59
+ emotions, intent = analyze_intent(transcript)
60
+ st.session_state.emotions = emotions
61
+ st.session_state.intent = intent
62
+ st.write("**Detected Emotions:**", ", ".join([f"{e['label']} ({e['score']:.2f})" for e in emotions]))
63
+ st.write(f"**Intent:** {intent}")
64
+
65
+ # Crisis Toolchain
66
+ crisis_risk, score = detect_crisis(transcript, emotions)
67
+ st.session_state.crisis_risk = crisis_risk
68
+ st.write(f"**Crisis Risk:** {crisis_risk} ({score:.2f})")
69
+
70
+ if crisis_risk.lower() in ['high risk', 'medium risk'] and score > 0.8:
71
+ st.error("⚠️ أشوفك محتاج دعم فوري، ياخي. تواصل مع مستشفى المسارة على 2487 3268 أو اتصل 9999 للطوارئ.")
72
+ emergency_resources()
73
+ if crisis_risk.lower() == 'high risk':
74
+ return # Halt conversation for high risk
75
+
76
+ # Response Generation and TTS
77
+ response = generate_response(
78
+ transcript,
79
+ ", ".join([e['label'] for e in emotions]),
80
+ intent,
81
+ crisis_risk
82
+ )
83
+ st.write(f"**Bot Response:** {response}")
84
+ text_to_speech(response)
85
+
86
+ # Display latest audio (if needed)
87
+ display_latest_audio()
88
+
89
+ if __name__ == "__main__":
90
+ main()