Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
from groq import Groq, GroqError
|
6 |
+
import whisper
|
7 |
+
from gtts import gTTS
|
8 |
+
import tempfile
|
9 |
+
import logging
|
10 |
+
import numpy as np
|
11 |
+
from pydub import AudioSegment
|
12 |
+
|
13 |
+
# Set up logging
|
14 |
+
logging.basicConfig(level=logging.INFO)
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
|
17 |
+
# Securely configure Google API Key for generative AI (set this in the deployment environment)
|
18 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
19 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
20 |
+
|
21 |
+
# Set up Groq API key for audio processing
|
22 |
+
groq_api_key = os.getenv('GROQ_API_KEY', "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")
|
23 |
+
|
24 |
+
if not groq_api_key:
|
25 |
+
raise ValueError("GROQ_API_KEY is not set.")
|
26 |
+
try:
|
27 |
+
groq_client = Groq(api_key=groq_api_key)
|
28 |
+
logger.info("Groq API key is set and client is initialized.")
|
29 |
+
except GroqError as e:
|
30 |
+
logger.error(f"Failed to initialize Groq client: {e}")
|
31 |
+
raise
|
32 |
+
|
33 |
+
try:
|
34 |
+
# Load Whisper model for audio transcription
|
35 |
+
whisper_model = whisper.load_model("base")
|
36 |
+
logger.info("Whisper model loaded successfully.")
|
37 |
+
except Exception as e:
|
38 |
+
logger.error(f"Failed to load Whisper model: {e}")
|
39 |
+
raise
|
40 |
+
|
41 |
+
# Initialize the Generative Model for heart health chatbot
|
42 |
+
model = genai.GenerativeModel(
|
43 |
+
'gemini-1.5-flash',
|
44 |
+
system_instruction=(
|
45 |
+
"Persona: You are Dr. Assad Siddiqui, a heart specialist. Only provide information related to heart health, symptoms, and advice. "
|
46 |
+
"Ask users about their heart-related symptoms and provide consultation and guidance based on their input. "
|
47 |
+
"Always provide brief answers. If the inquiry is not related to heart health, politely say that you can only provide heart-related information. "
|
48 |
+
"Responses should be in Urdu written in English and in English."
|
49 |
+
)
|
50 |
+
)
|
51 |
+
|
52 |
+
# Function to get response from the Generative AI chatbot
|
53 |
+
def get_chatbot_response(user_input):
|
54 |
+
response = model.generate_content(user_input)
|
55 |
+
return response.text.strip()
|
56 |
+
|
57 |
+
# Function to process audio using Whisper, Groq, and gTTS
|
58 |
+
def process_audio(audio_file):
|
59 |
+
try:
|
60 |
+
# Transcribe audio using Whisper
|
61 |
+
result = whisper_model.transcribe(audio_file)
|
62 |
+
user_text = result['text']
|
63 |
+
logger.info(f"Transcription successful: {user_text}")
|
64 |
+
except Exception as e:
|
65 |
+
logger.error(f"Error in transcribing audio: {e}")
|
66 |
+
return "Error in transcribing audio.", None
|
67 |
+
|
68 |
+
try:
|
69 |
+
# Generate response using Groq API
|
70 |
+
chat_completion = groq_client.chat.completions.create(
|
71 |
+
messages=[
|
72 |
+
{"role": "user", "content": user_text}
|
73 |
+
],
|
74 |
+
model="llama3-8b-8192",
|
75 |
+
)
|
76 |
+
response_text = chat_completion.choices[0].message.content
|
77 |
+
logger.info(f"Received response from Groq API: {response_text}")
|
78 |
+
except GroqError as e:
|
79 |
+
logger.error(f"Error in generating response with Groq API: {e}")
|
80 |
+
return "Error in generating response with Groq API.", None
|
81 |
+
|
82 |
+
try:
|
83 |
+
# Convert response text to speech using gTTS
|
84 |
+
tts = gTTS(text=response_text, lang='en')
|
85 |
+
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
86 |
+
tts.save(audio_file.name)
|
87 |
+
logger.info("Text-to-speech conversion successful.")
|
88 |
+
except Exception as e:
|
89 |
+
logger.error(f"Error in text-to-speech conversion: {e}")
|
90 |
+
return "Error in text-to-speech conversion.", None
|
91 |
+
|
92 |
+
return response_text, audio_file.name
|
93 |
+
|
94 |
+
# Streamlit page configuration
|
95 |
+
st.set_page_config(
|
96 |
+
page_title="Heart Health & Audio Chatbot",
|
97 |
+
page_icon="👨⚕️",
|
98 |
+
layout="centered",
|
99 |
+
initial_sidebar_state="collapsed",
|
100 |
+
)
|
101 |
+
|
102 |
+
# Background image and custom CSS
|
103 |
+
st.markdown("""
|
104 |
+
<style>
|
105 |
+
.stApp {
|
106 |
+
background-image: url('https://cdn.wallpapersafari.com/29/34/8Ak1Sf.png');
|
107 |
+
background-size: cover;
|
108 |
+
background-position: center;
|
109 |
+
background-attachment: fixed;
|
110 |
+
}
|
111 |
+
.chat-bubble {
|
112 |
+
background-color: #128c7E;
|
113 |
+
color: white;
|
114 |
+
padding: 10px;
|
115 |
+
border-radius: 10px;
|
116 |
+
max-width: 70%;
|
117 |
+
}
|
118 |
+
.user-bubble {
|
119 |
+
background-color: #075e54;
|
120 |
+
color: white;
|
121 |
+
padding: 10px;
|
122 |
+
border-radius: 10px;
|
123 |
+
max-width: 70%;
|
124 |
+
align-self: flex-end;
|
125 |
+
}
|
126 |
+
img.avatar {
|
127 |
+
width: 50px;
|
128 |
+
height: 50px;
|
129 |
+
border-radius: 50%;
|
130 |
+
}
|
131 |
+
</style>
|
132 |
+
""", unsafe_allow_html=True)
|
133 |
+
|
134 |
+
# Custom header
|
135 |
+
def load_header():
|
136 |
+
st.markdown("""
|
137 |
+
<div style="padding:10px;text-align:center;color:white;">
|
138 |
+
<h1>Heart Health & Audio Chatbot 🫀</h1>
|
139 |
+
<p>Ask me anything about heart diseases or process audio queries!</p>
|
140 |
+
</div>
|
141 |
+
""", unsafe_allow_html=True)
|
142 |
+
|
143 |
+
# Initialize session state for chat history
|
144 |
+
if "history" not in st.session_state:
|
145 |
+
st.session_state.history = []
|
146 |
+
|
147 |
+
# Avatar URLs
|
148 |
+
user_avatar_url = "https://img.freepik.com/free-photo/sad-cartoon-anatomical-heart_23-2149767987.jpg"
|
149 |
+
bot_avatar_url = "https://img.freepik.com/premium-photo/3d-render-man-doctor-avatar-round-sticker-with-cartoon-character-face-user-id-thumbnail-modern-69_1181551-3160.jpg"
|
150 |
+
|
151 |
+
# Function to display chat history
|
152 |
+
def display_chat_history():
|
153 |
+
for chat in st.session_state.history:
|
154 |
+
if chat["role"] == "user":
|
155 |
+
st.markdown(f"""
|
156 |
+
<div style="display: flex; justify-content: flex-end; align-items: center; margin-bottom: 10px;">
|
157 |
+
<div class="user-bubble">
|
158 |
+
<p><b>You:</b> {chat['content']}</p>
|
159 |
+
</div>
|
160 |
+
<img src="{user_avatar_url}" class="avatar"/>
|
161 |
+
</div>
|
162 |
+
""", unsafe_allow_html=True)
|
163 |
+
else:
|
164 |
+
st.markdown(f"""
|
165 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
166 |
+
<img src="{bot_avatar_url}" class="avatar">
|
167 |
+
<div class="chat-bubble">
|
168 |
+
<p><b>Bot:</b> {chat['content']}</p>
|
169 |
+
</div>
|
170 |
+
</div>
|
171 |
+
""", unsafe_allow_html=True)
|
172 |
+
|
173 |
+
# Main application layout
|
174 |
+
def main():
|
175 |
+
load_header()
|
176 |
+
|
177 |
+
with st.container():
|
178 |
+
display_chat_history()
|
179 |
+
|
180 |
+
# User input for text-based chat
|
181 |
+
with st.form(key="user_input_form", clear_on_submit=True):
|
182 |
+
user_input = st.text_input(
|
183 |
+
label="Type your message...",
|
184 |
+
placeholder="Ask about heart health...",
|
185 |
+
max_chars=500
|
186 |
+
)
|
187 |
+
submit_button = st.form_submit_button(label="Send")
|
188 |
+
|
189 |
+
if submit_button and user_input.strip():
|
190 |
+
with st.spinner("Thinking..."):
|
191 |
+
bot_response = get_chatbot_response(user_input)
|
192 |
+
|
193 |
+
# Update chat history
|
194 |
+
st.session_state.history.append({"role": "user", "content": user_input})
|
195 |
+
st.session_state.history.append({"role": "bot", "content": bot_response})
|
196 |
+
|
197 |
+
# Refresh chat display
|
198 |
+
display_chat_history()
|
199 |
+
|
200 |
+
# File uploader for audio processing
|
201 |
+
st.subheader("Upload audio for processing")
|
202 |
+
audio_file = st.file_uploader("Choose an audio file", type=["mp3", "wav"])
|
203 |
+
|
204 |
+
if audio_file is not None:
|
205 |
+
with st.spinner("Processing audio..."):
|
206 |
+
response_text, audio_response = process_audio(audio_file)
|
207 |
+
st.text(f"Response: {response_text}")
|
208 |
+
st.audio(audio_response)
|
209 |
+
|
210 |
+
# Run the app
|
211 |
+
if __name__ == "__main__":
|
212 |
+
main()
|