Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
from google.cloud import aiplatform
|
3 |
-
#import google.generativeai as genai
|
4 |
import os
|
5 |
import logging
|
6 |
import whisper
|
@@ -9,8 +8,14 @@ import tempfile
|
|
9 |
from pydub import AudioSegment
|
10 |
from groq import Groq, GroqError
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Initialize the client
|
16 |
def init_palm(api_key):
|
@@ -24,139 +29,19 @@ def generate_palm_response(prompt):
|
|
24 |
)
|
25 |
return response.predictions[0]['content']
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
init_palm(api_key)
|
30 |
-
response = generate_palm_response("Heart health query...")
|
31 |
-
print(response)
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
# Configure API keys securely
|
36 |
-
#GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
37 |
-
#genai.configure(api_key=GOOGLE_API_KEY)
|
38 |
-
|
39 |
-
groq_api_key = os.getenv('GROQ_API_KEY')
|
40 |
-
if not groq_api_key:
|
41 |
-
raise ValueError("GROQ_API_KEY is not set.")
|
42 |
-
|
43 |
-
# Set up logging
|
44 |
-
logging.basicConfig(level=logging.INFO)
|
45 |
-
logger = logging.getLogger(__name__)
|
46 |
-
|
47 |
-
# Initialize Groq API client
|
48 |
-
try:
|
49 |
-
groq_client = Groq(api_key=groq_api_key)
|
50 |
-
logger.info("Groq API key is set and client is initialized.")
|
51 |
-
except GroqError as e:
|
52 |
-
logger.error(f"Failed to initialize Groq client: {e}")
|
53 |
-
raise
|
54 |
-
|
55 |
-
# Load Whisper model
|
56 |
-
try:
|
57 |
-
whisper_model = whisper.load_model("base")
|
58 |
-
logger.info("Whisper model loaded successfully.")
|
59 |
-
except Exception as e:
|
60 |
-
logger.error(f"Failed to load Whisper model: {e}")
|
61 |
-
raise
|
62 |
-
|
63 |
-
# Streamlit page configuration
|
64 |
-
st.set_page_config(
|
65 |
-
page_title="Heart Health & Audio Processing App",
|
66 |
-
page_icon="🫀",
|
67 |
-
layout="centered",
|
68 |
-
initial_sidebar_state="collapsed",
|
69 |
-
)
|
70 |
-
|
71 |
-
# Initialize the Generative Model for heart health chatbot
|
72 |
-
model = genai.GenerativeModel(
|
73 |
-
'gemini-1.5-flash',
|
74 |
-
system_instruction=(
|
75 |
-
"Persona: You are Dr. Assad Siddiqui, a heart specialist. Only provide information related to heart health, symptoms, and advice. "
|
76 |
-
"Ask users about their heart-related symptoms and provide consultation and guidance based on their input. "
|
77 |
-
"Always provide brief answers. If the inquiry is not related to heart health, politely say that you can only provide heart-related information. "
|
78 |
-
"Responses should be in Urdu written in English and in English."
|
79 |
-
)
|
80 |
-
)
|
81 |
-
|
82 |
-
# Function to get chatbot response
|
83 |
-
def get_chatbot_response(user_input):
|
84 |
-
response = model.generate_content(user_input)
|
85 |
-
return response.text.strip()
|
86 |
-
|
87 |
-
# Function to process audio input
|
88 |
-
def process_audio(audio_file):
|
89 |
-
try:
|
90 |
-
result = whisper_model.transcribe(audio_file)
|
91 |
-
user_text = result['text']
|
92 |
-
logger.info(f"Transcription successful: {user_text}")
|
93 |
-
except Exception as e:
|
94 |
-
logger.error(f"Error in transcribing audio: {e}")
|
95 |
-
return "Error in transcribing audio.", None
|
96 |
-
|
97 |
-
try:
|
98 |
-
chat_completion = groq_client.chat.completions.create(
|
99 |
-
messages=[{"role": "user", "content": user_text}],
|
100 |
-
model="llama3-8b-8192",
|
101 |
-
)
|
102 |
-
response_text = chat_completion.choices[0].message.content
|
103 |
-
logger.info(f"Received response from Groq API: {response_text}")
|
104 |
-
except GroqError as e:
|
105 |
-
logger.error(f"Error in generating response with Groq API: {e}")
|
106 |
-
return "Error in generating response with Groq API.", None
|
107 |
-
|
108 |
try:
|
109 |
-
|
110 |
-
|
111 |
-
tts.save(audio_file.name)
|
112 |
-
logger.info("Text-to-speech conversion successful.")
|
113 |
-
except Exception as e:
|
114 |
-
logger.error(f"Error in text-to-speech conversion: {e}")
|
115 |
-
return "Error in text-to-speech conversion.", None
|
116 |
-
|
117 |
-
return response_text, audio_file.name
|
118 |
|
119 |
-
#
|
120 |
-
|
121 |
-
|
|
|
|
|
122 |
|
123 |
-
|
124 |
-
tab1, tab2 = st.tabs(["Heart Health Chatbot", "Audio Processing"])
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
st.header("Chat with Heart Health Specialist Dr. Assad Siddiqui")
|
129 |
-
|
130 |
-
if "history" not in st.session_state:
|
131 |
-
st.session_state.history = []
|
132 |
-
|
133 |
-
user_input = st.text_input("Ask about heart health:", placeholder="Type here...")
|
134 |
-
|
135 |
-
if st.button("Send") and user_input:
|
136 |
-
bot_response = get_chatbot_response(user_input)
|
137 |
-
st.session_state.history.append({"role": "user", "content": user_input})
|
138 |
-
st.session_state.history.append({"role": "bot", "content": bot_response})
|
139 |
-
|
140 |
-
for chat in st.session_state.history:
|
141 |
-
if chat["role"] == "user":
|
142 |
-
st.write(f"**You:** {chat['content']}")
|
143 |
-
else:
|
144 |
-
st.write(f"**Bot:** {chat['content']}")
|
145 |
-
|
146 |
-
# Tab 2: Audio Processing
|
147 |
-
with tab2:
|
148 |
-
st.header("Audio Processing with Whisper and Groq")
|
149 |
-
|
150 |
-
uploaded_audio = st.file_uploader("Upload an audio file for transcription and response", type=["mp3", "wav", "ogg"])
|
151 |
-
|
152 |
-
if uploaded_audio:
|
153 |
-
with st.spinner("Processing audio..."):
|
154 |
-
response_text, audio_file_path = process_audio(uploaded_audio)
|
155 |
-
|
156 |
-
if response_text:
|
157 |
-
st.write(f"**Response:** {response_text}")
|
158 |
-
st.audio(audio_file_path)
|
159 |
-
|
160 |
-
# Run the app
|
161 |
-
if __name__ == "__main__":
|
162 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
from google.cloud import aiplatform
|
|
|
3 |
import os
|
4 |
import logging
|
5 |
import whisper
|
|
|
8 |
from pydub import AudioSegment
|
9 |
from groq import Groq, GroqError
|
10 |
|
11 |
+
# Set up Google Cloud credentials
|
12 |
+
def setup_google_cloud_credentials():
|
13 |
+
google_credentials_path = "/path/to/your-service-account-file.json"
|
14 |
+
if os.path.exists(google_credentials_path):
|
15 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = google_credentials_path
|
16 |
+
logging.info(f"Google Cloud credentials set from {google_credentials_path}")
|
17 |
+
else:
|
18 |
+
raise FileNotFoundError(f"Google Cloud credentials file not found at {google_credentials_path}")
|
19 |
|
20 |
# Initialize the client
|
21 |
def init_palm(api_key):
|
|
|
29 |
)
|
30 |
return response.predictions[0]['content']
|
31 |
|
32 |
+
# Main code to run the application
|
33 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
try:
|
35 |
+
# Set up Google Cloud credentials
|
36 |
+
setup_google_cloud_credentials()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Call the model
|
39 |
+
api_key = "your-google-api-key"
|
40 |
+
init_palm(api_key)
|
41 |
+
response = generate_palm_response("Heart health query...")
|
42 |
+
print(response)
|
43 |
|
44 |
+
# Streamlit app or other logic goes here...
|
|
|
45 |
|
46 |
+
except Exception as e:
|
47 |
+
logging.error(f"Error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|