Delete app.py
Browse files
app.py
DELETED
@@ -1,126 +0,0 @@
|
|
1 |
-
|
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):
|
20 |
-
response = requests.post(API_URL, headers=headers, json=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 |
-
|
28 |
-
# Function to find nearby clinics/pharmacies using geopy
|
29 |
-
def find_nearby_clinics(address):
|
30 |
-
geolocator = Nominatim(user_agent="healthcare_companion")
|
31 |
-
location = geolocator.geocode(address)
|
32 |
-
if location:
|
33 |
-
return (location.latitude, location.longitude)
|
34 |
-
else:
|
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}}
|
106 |
-
st.write(f"Debug: Payload sent to model: {payload}") # Debugging: Check payload
|
107 |
-
result = query(payload)
|
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, how about i contact a doctor."
|
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:")
|
117 |
-
if address:
|
118 |
-
location = find_nearby_clinics(address)
|
119 |
-
if location:
|
120 |
-
st.write(f"**Nearby Clinics/Pharmacies (Coordinates):** {location}")
|
121 |
-
|
122 |
-
# Additional features like prescription info can be added similarly
|
123 |
-
|
124 |
-
if __name__ == "__main__":
|
125 |
-
main()
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|