Spaces:
Obotu
/
Build error

Obotu commited on
Commit
76025ff
1 Parent(s): f545f5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -7
app.py CHANGED
@@ -2,10 +2,18 @@
2
  import streamlit as st
3
  import requests
4
  from geopy.geocoders import Nominatim
 
 
 
 
 
5
 
6
  # Set your Hugging Face API URL and API key
7
  API_URL = "https://api-inference.huggingface.co/models/dmis-lab/biobert-base-cased-v1.1"
8
- headers = {"Authorization": ""}
 
 
 
9
 
10
  # Function to query the Hugging Face model
11
  def query(payload):
@@ -13,7 +21,7 @@ def query(payload):
13
  if response.status_code == 200:
14
  return response.json()
15
  else:
16
- st.error("Error: Unable to fetch response from model")
17
  st.error(response.text)
18
  return None
19
 
@@ -27,16 +35,71 @@ def find_nearby_clinics(address):
27
  st.error("Error: Address not found")
28
  return None
29
 
 
 
 
 
 
 
 
 
30
  # Main function to create the Streamlit app
31
  def main():
32
  st.title("Healthcare Companion")
33
  st.write("This app provides healthcare guidance, prescription information, and locates nearby clinics or pharmacies.")
34
 
35
- # User input for medical symptoms
36
- symptoms = st.text_area("Enter your symptoms (e.g., 'I am having a cough, weak knee, swollen eyes'):")
37
- if symptoms:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  context = """
39
- This is a healthcare question and answer platform. The following text contains typical symptoms, treatments, and medical conditions commonly asked about in healthcare settings.
40
  For example, symptoms of COVID-19 include fever, dry cough, and tiredness. Treatment options for hypertension include lifestyle changes and medications. The platform is designed to assist with general medical inquiries.
41
  """
42
  payload = {"inputs": {"question": symptoms, "context": context}}
@@ -45,7 +108,9 @@ def main():
45
  st.write(f"Debug: Response from model: {result}") # Debugging: Check response
46
  if result:
47
  st.write("**Medical Advice:**")
48
- st.write(result.get('answer', "Sorry, I don't have information on that."))
 
 
49
 
50
  # User input for address to find nearby clinics/pharmacies
51
  address = st.text_input("Enter your address to find nearby clinics/pharmacies:")
@@ -58,3 +123,4 @@ def main():
58
 
59
  if __name__ == "__main__":
60
  main()
 
 
2
  import streamlit as st
3
  import requests
4
  from geopy.geocoders import Nominatim
5
+ import whisper
6
+ import tempfile
7
+ from pydub import AudioSegment
8
+ from io import BytesIO
9
+ from streamlit_js_eval import streamlit_js_eval
10
 
11
  # Set your Hugging Face API URL and API key
12
  API_URL = "https://api-inference.huggingface.co/models/dmis-lab/biobert-base-cased-v1.1"
13
+ headers = {"Authorization": f"secret"}
14
+
15
+ # Initialize Whisper model
16
+ whisper_model = whisper.load_model("base")
17
 
18
  # Function to query the Hugging Face model
19
  def query(payload):
 
21
  if response.status_code == 200:
22
  return response.json()
23
  else:
24
+ st.error(f"Error: Unable to fetch response from model (status code: {response.status_code})")
25
  st.error(response.text)
26
  return None
27
 
 
35
  st.error("Error: Address not found")
36
  return None
37
 
38
+ # Function to transcribe audio to text using Whisper
39
+ def transcribe_audio(audio_bytes):
40
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
41
+ audio = AudioSegment.from_file(BytesIO(audio_bytes), format="wav")
42
+ audio.export(temp_audio_file.name, format="wav")
43
+ result = whisper_model.transcribe(temp_audio_file.name)
44
+ return result["text"]
45
+
46
  # Main function to create the Streamlit app
47
  def main():
48
  st.title("Healthcare Companion")
49
  st.write("This app provides healthcare guidance, prescription information, and locates nearby clinics or pharmacies.")
50
 
51
+ # JavaScript code to capture audio
52
+ js_code = """
53
+ async function recordAudio() {
54
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
55
+ const mediaRecorder = new MediaRecorder(stream);
56
+ let audioChunks = [];
57
+
58
+ mediaRecorder.ondataavailable = event => {
59
+ audioChunks.push(event.data);
60
+ };
61
+
62
+ mediaRecorder.onstop = async () => {
63
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
64
+ const audioBuffer = await audioBlob.arrayBuffer();
65
+ const audioBase64 = arrayBufferToBase64(audioBuffer);
66
+ document.getElementById('audio_data').value = audioBase64;
67
+ document.getElementById('audio_form').submit();
68
+ };
69
+
70
+ mediaRecorder.start();
71
+ setTimeout(() => mediaRecorder.stop(), 5000); // Record for 5 seconds
72
+
73
+ function arrayBufferToBase64(buffer) {
74
+ let binary = '';
75
+ const bytes = new Uint8Array(buffer);
76
+ const len = bytes.byteLength;
77
+ for (let i = 0; i < len; i++) {
78
+ binary += String.fromCharCode(bytes[i]);
79
+ }
80
+ return window.btoa(binary);
81
+ }
82
+ }
83
+
84
+ recordAudio();
85
+ """
86
+
87
+ # Placeholder for audio data
88
+ st_js_code = streamlit_js_eval(js_code, key="record_audio")
89
+
90
+ # Form to receive audio data from JavaScript
91
+ with st.form("audio_form", clear_on_submit=True):
92
+ audio_data = st.text_input("audio_data", type="hidden")
93
+ submit_button = st.form_submit_button("Submit")
94
+
95
+ if submit_button and audio_data:
96
+ audio_bytes = BytesIO(base64.b64decode(audio_data))
97
+ symptoms = transcribe_audio(audio_bytes)
98
+ st.write(f"Transcribed symptoms: {symptoms}")
99
+
100
+ if 'symptoms' in locals() and symptoms:
101
  context = """
102
+ This is a healthcare question and answer platform. The following text contains typical symptoms, treatments, and medical conditions commonly asked about in healthcare settings.
103
  For example, symptoms of COVID-19 include fever, dry cough, and tiredness. Treatment options for hypertension include lifestyle changes and medications. The platform is designed to assist with general medical inquiries.
104
  """
105
  payload = {"inputs": {"question": symptoms, "context": context}}
 
108
  st.write(f"Debug: Response from model: {result}") # Debugging: Check response
109
  if result:
110
  st.write("**Medical Advice:**")
111
+ # Check the response structure and extract the answer appropriately
112
+ answer = result.get('answer') if 'answer' in result else "Sorry, I don't have information on that."
113
+ st.write(answer)
114
 
115
  # User input for address to find nearby clinics/pharmacies
116
  address = st.text_input("Enter your address to find nearby clinics/pharmacies:")
 
123
 
124
  if __name__ == "__main__":
125
  main()
126
+