Spaces:
Obotu
/
Build error

File size: 6,063 Bytes
d57c11d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""new.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/10l5M_bqlqlmx8isz58qrPCp9YwDOKjiC
"""

from re import S
!pip install Streamlit
!pip install whisper
!pip install pydub
!pip install mapbox
!pip install openai-whisper pydub streamlit requests geopy
!pip install streamlit-webrtc

from google.colab import userdata
userdata.get('secret')

import streamlit as st
import requests
from geopy.geocoders import Nominatim
import whisper
import tempfile
from pydub import AudioSegment
from io import BytesIO
import base64
import folium
import requests
from mapbox import Geocoder
import folium


# Set your Hugging Face API URL and API key
API_URL = "https://api-inference.huggingface.co/models/dmis-lab/biobert-base-cased-v1.1"
headers = {"Authorization": f"secret"}

# Initialize Whisper model
whisper_model = whisper.load_model("base")

# Initialize Mapbox Geocoder with your API key
MAPBOX_API_KEY = "sk.eyJ1IjoibWFyaW9jYWxlYiIsImEiOiJjbHdkZWV3b2wwdnlxMmtwbnhqaHdnNzA3In0.iOhyIXlUFztYtc1iZhSMng"
geocoder = Geocoder(access_token=MAPBOX_API_KEY)

# Function to query the Hugging Face model
def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    if response.status_code == 200:
        return response.json()
    else:
        st.error(f"Error: Unable to fetch response from model (status code: {response.status_code})")
        st.error(response.text)
        return None

# Function to find nearby clinics/pharmacies using Mapbox
def find_nearby_clinics(address):
    response = geocoder.forward(address)
    if response.status_code == 200:
        features = response.json()['features']
        if features:
            location = features[0]['center']
            return location
        else:
            st.error("No locations found")
            return None
    else:
        st.error("Error: Unable to fetch location data")
        return None

# Function to transcribe audio to text using Whisper
def transcribe_audio(audio_bytes):
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
        audio = AudioSegment.from_file(BytesIO(audio_bytes), format="wav")
        audio.export(temp_audio_file.name, format="wav")
        result = whisper_model.transcribe(temp_audio_file.name)
        return result["text"]

# Main function to create the Streamlit app
def main():
    st.title("Healthcare Companion")
    st.write("Welcome to Healthcare Companion! Your personal healthcare guide.")

    st.header("Speak Your Symptoms")
    st.write("Press the button and speak your symptoms clearly.")

    # JavaScript code to capture audio
    js_code = """
    async function recordAudio() {
        const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
        const mediaRecorder = new MediaRecorder(stream);
        let audioChunks = [];

        mediaRecorder.ondataavailable = event => {
            audioChunks.push(event.data);
        };

        mediaRecorder.onstop = async () => {
            const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
            const audioBuffer = await audioBlob.arrayBuffer();
            const audioBase64 = arrayBufferToBase64(audioBuffer);
            document.getElementById('audio_data').value = audioBase64;
            document.getElementById('audio_form').submit();
        };

        mediaRecorder.start();
        setTimeout(() => mediaRecorder.stop(), 5000);  // Record for 5 seconds

        function arrayBufferToBase64(buffer) {
            let binary = '';
            const bytes = new Uint8Array(buffer);
            const len = bytes.byteLength;
            for (let i = 0; i < len; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            return window.btoa(binary);
        }
    }

    recordAudio();
    """

    # Placeholder for audio data
    #st_js_code = streamlit_js_eval(js_code, key="record_audio")

    # Form to receive audio data from JavaScript
    with st.form("audio_form", clear_on_submit=True):
        audio_data = st.text_input("audio_data", type="default")
        submit_button = st.form_submit_button("Submit")

        if submit_button and audio_data:
            audio_bytes = BytesIO(base64.b64decode(audio_data))
            symptoms = transcribe_audio(audio_bytes)
            st.write(f"**Transcribed symptoms:** {symptoms}")

    if 'symptoms' in locals() and symptoms:
        st.header("Symptom Checker")
        st.write("Enter your symptoms below for advice.")

        context = """
        This is a healthcare question and answer platform. The following text contains typical symptoms, treatments, and medical conditions commonly asked about in healthcare settings.
        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.
        """
        payload = {"inputs": {"question": symptoms, "context": context}}
        result = query(payload)
        if result:
            st.write("**Medical Advice:**")
            answer = result.get('answer', "Sorry, I don't have information on that.")
            st.write(answer)

    st.header("Locate Nearest Pharmacy/Clinic")
    st.write("Enter your address to find the nearest pharmacy or clinic.")

    address = st.text_input("Enter your address here:")
    if address:
        location = find_nearby_clinics(address)
        if location:
            map_ = folium.Map(location=[location[1], location[0]], zoom_start=15)
            folium.Marker([location[1], location[0]], popup="Your Location").add_to(map_)
            st_folium(map_, width=700, height=500)
            st.write(f"**Nearby Clinics/Pharmacies (Coordinates):** {location}")

    st.write("Providing reliable healthcare guidance and information. Please consult a healthcare professional for medical emergencies.")

if __name__ == "__main__":
    main()