Update app.py
Browse files
app.py
CHANGED
@@ -48,135 +48,81 @@
|
|
48 |
# if __name__ == "__main__":
|
49 |
# main()
|
50 |
import streamlit as st
|
51 |
-
import speech_recognition as sr
|
52 |
-
# import pyttsx3
|
53 |
from fpdf import FPDF
|
54 |
-
|
55 |
-
import
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
"""Listen to audio and convert to text."""
|
71 |
try:
|
72 |
with sr.Microphone() as source:
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
except sr.UnknownValueError:
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
st.write(f"Could not request results from Google Speech Recognition service; {e}")
|
83 |
-
return ""
|
84 |
except Exception as e:
|
85 |
-
|
86 |
-
return ""
|
87 |
|
88 |
-
#
|
89 |
-
def
|
90 |
-
"""Convert text to speech."""
|
91 |
-
engine.say(text)
|
92 |
-
engine.runAndWait()
|
93 |
-
|
94 |
-
# Dummy disease prediction based on symptoms
|
95 |
-
def predict_disease(symptoms):
|
96 |
-
"""Mock prediction of disease based on symptoms."""
|
97 |
-
if 'fever' in symptoms.lower():
|
98 |
-
return "You might have the flu."
|
99 |
-
else:
|
100 |
-
return "No prediction available."
|
101 |
-
|
102 |
-
# Function to generate PDF
|
103 |
-
def generate_pdf(content):
|
104 |
-
"""Generate PDF with the given content."""
|
105 |
pdf = FPDF()
|
106 |
pdf.add_page()
|
107 |
pdf.set_font("Arial", size=12)
|
108 |
-
pdf.multi_cell(
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
return pdf_file
|
113 |
|
114 |
-
# Streamlit
|
115 |
st.title("Medical Assistance for Doctors")
|
|
|
116 |
|
117 |
-
#
|
118 |
-
st.
|
119 |
-
|
120 |
-
# Display a text input box with a microphone button next to it
|
121 |
-
col1, col2 = st.columns([5, 1]) # Create a layout with 5:1 ratio for text input and button
|
122 |
|
123 |
-
|
124 |
-
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
symptoms = listen_to_voice() # Capture voice input when the mic icon is pressed
|
129 |
|
130 |
-
#
|
131 |
-
if
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
st.header("Edit Response (Optional)")
|
141 |
-
|
142 |
-
# Mic button for editing response through voice
|
143 |
-
col3, col4 = st.columns([5, 1])
|
144 |
-
|
145 |
-
with col3:
|
146 |
-
edited_response = st.text_area("Or Edit Response Using Keyboard:", prediction)
|
147 |
-
|
148 |
-
with col4:
|
149 |
-
if st.button("🎤 Edit by Voice"):
|
150 |
-
edited_response = listen_to_voice() # Voice editing
|
151 |
-
|
152 |
-
if edited_response:
|
153 |
-
prediction = edited_response
|
154 |
-
|
155 |
-
# Display the final response
|
156 |
-
st.write(f"Final Response: {prediction}")
|
157 |
-
|
158 |
-
# Text-to-speech option for the final response
|
159 |
-
if st.button("Play Final Response"):
|
160 |
-
tts(prediction)
|
161 |
-
|
162 |
-
# Step 4: Generate and Download PDF
|
163 |
-
st.header("Generate and Download PDF Report")
|
164 |
-
content = f"Symptoms: {symptoms}\nPrediction: {prediction}"
|
165 |
|
166 |
-
if
|
167 |
-
|
168 |
-
st.
|
169 |
|
170 |
-
#
|
171 |
-
|
|
|
172 |
st.download_button(
|
173 |
-
label="Download
|
174 |
-
data=
|
175 |
-
file_name="
|
176 |
mime="application/pdf"
|
177 |
)
|
178 |
-
|
179 |
-
# Run Streamlit App
|
180 |
-
if __name__ == "__main__":
|
181 |
-
st.write("Welcome to the medical assistance system.")
|
182 |
-
|
|
|
48 |
# if __name__ == "__main__":
|
49 |
# main()
|
50 |
import streamlit as st
|
|
|
|
|
51 |
from fpdf import FPDF
|
52 |
+
import speech_recognition as sr
|
53 |
+
from io import BytesIO
|
54 |
+
|
55 |
+
# Function to predict disease based on symptoms
|
56 |
+
def predict_disease(symptoms):
|
57 |
+
# Example logic (replace with actual logic or model predictions)
|
58 |
+
if "fever" in symptoms.lower() and "cough" in symptoms.lower():
|
59 |
+
return "Flu"
|
60 |
+
elif "pain" in symptoms.lower() and "swelling" in symptoms.lower():
|
61 |
+
return "Arthritis"
|
62 |
+
else:
|
63 |
+
return "Unknown disease. Please provide more details."
|
64 |
+
|
65 |
+
# Function to handle voice input
|
66 |
+
def voice_input():
|
67 |
+
r = sr.Recognizer()
|
|
|
68 |
try:
|
69 |
with sr.Microphone() as source:
|
70 |
+
r.adjust_for_ambient_noise(source)
|
71 |
+
st.write("Listening...")
|
72 |
+
audio = r.listen(source)
|
73 |
+
symptoms = r.recognize_google(audio)
|
74 |
+
return symptoms
|
75 |
except sr.UnknownValueError:
|
76 |
+
return "Sorry, I could not understand the audio."
|
77 |
+
except sr.RequestError:
|
78 |
+
return "Error with the speech recognition service."
|
|
|
|
|
79 |
except Exception as e:
|
80 |
+
return f"Microphone error: {e}"
|
|
|
81 |
|
82 |
+
# Function to save response to a PDF
|
83 |
+
def save_to_pdf(response):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
pdf = FPDF()
|
85 |
pdf.add_page()
|
86 |
pdf.set_font("Arial", size=12)
|
87 |
+
pdf.multi_cell(0, 10, f"Medical Assistant Response:\n\n{response}")
|
88 |
+
pdf_output = BytesIO()
|
89 |
+
pdf.output(pdf_output)
|
90 |
+
return pdf_output
|
|
|
91 |
|
92 |
+
# Streamlit app
|
93 |
st.title("Medical Assistance for Doctors")
|
94 |
+
st.write("Enter symptoms either by typing or using voice input.")
|
95 |
|
96 |
+
# Input field for entering symptoms manually
|
97 |
+
symptoms_input = st.text_area("Enter symptoms here:")
|
|
|
|
|
|
|
98 |
|
99 |
+
# Toggle for voice input
|
100 |
+
use_voice_input = st.checkbox("Use Voice Input")
|
101 |
|
102 |
+
# Checkbox to save the result as PDF
|
103 |
+
save_as_pdf = st.checkbox("Save result as PDF")
|
|
|
104 |
|
105 |
+
# Button to trigger prediction
|
106 |
+
if st.button("Submit"):
|
107 |
+
if use_voice_input:
|
108 |
+
symptoms = voice_input() # Get symptoms via voice input
|
109 |
+
if "error" in symptoms.lower():
|
110 |
+
st.error(symptoms) # Display the error message
|
111 |
+
else:
|
112 |
+
st.write(f"Symptoms recognized: {symptoms}")
|
113 |
+
else:
|
114 |
+
symptoms = symptoms_input # Use keyboard input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
if symptoms:
|
117 |
+
prediction = predict_disease(symptoms) # Predict disease based on symptoms
|
118 |
+
st.write(f"Predicted Disease: {prediction}")
|
119 |
|
120 |
+
# Optionally save the response as a PDF
|
121 |
+
if save_as_pdf:
|
122 |
+
pdf_output = save_to_pdf(prediction)
|
123 |
st.download_button(
|
124 |
+
label="Download PDF",
|
125 |
+
data=pdf_output.getvalue(),
|
126 |
+
file_name="medical_assistance.pdf",
|
127 |
mime="application/pdf"
|
128 |
)
|
|
|
|
|
|
|
|
|
|