ahmadmac commited on
Commit
59c8cf4
·
verified ·
1 Parent(s): c8f8577

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -112
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
- #!sudo apt-get install espeak
55
- import pyttsx3
56
- #engine = pyttsx3.init('espeak')
57
- engine = pyttsx3.init('espeak')
58
-
59
- # Get a list of available voices
60
- voices = engine.getProperty('voices')
61
-
62
- # Choose a voice (adjust the index as needed)
63
- engine.setProperty('voice', voices[0].id)
64
- # Initialize speech recognition and TTS engine
65
- recognizer = sr.Recognizer()
66
- # engine = pyttsx3.init()
67
-
68
- # Function to listen to voice input
69
- def listen_to_voice():
70
- """Listen to audio and convert to text."""
71
  try:
72
  with sr.Microphone() as source:
73
- st.write("Listening for symptoms (speak now)...")
74
- audio = recognizer.listen(source, timeout=5, phrase_time_limit=10)
75
- text = recognizer.recognize_google(audio)
76
- st.write(f"You said: {text}")
77
- return text
78
  except sr.UnknownValueError:
79
- st.write("Sorry, I did not understand the audio.")
80
- return ""
81
- except sr.RequestError as e:
82
- st.write(f"Could not request results from Google Speech Recognition service; {e}")
83
- return ""
84
  except Exception as e:
85
- st.write(f"Error: {str(e)}")
86
- return ""
87
 
88
- # Text-to-speech function
89
- def tts(text):
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(200, 10, content)
109
- # Save the PDF to a file
110
- pdf_file = 'medical_report.pdf'
111
- pdf.output(pdf_file)
112
- return pdf_file
113
 
114
- # Streamlit App Interface
115
  st.title("Medical Assistance for Doctors")
 
116
 
117
- # Step 1: Input Symptoms via Text Box with a voice input icon
118
- st.header("Enter Symptoms")
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
- with col1:
124
- symptoms = st.text_area("Enter Symptoms Using Keyboard:")
125
 
126
- with col2:
127
- if st.button("🎤"):
128
- symptoms = listen_to_voice() # Capture voice input when the mic icon is pressed
129
 
130
- # Display entered symptoms
131
- if symptoms:
132
- st.write(f"Entered Symptoms: {symptoms}")
133
-
134
- # Step 2: Predict disease based on symptoms
135
- if st.button("Predict Disease"):
136
- prediction = predict_disease(symptoms)
137
- st.write(f"Prediction: {prediction}")
138
-
139
- # Step 3: Edit Response via Voice or Text
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 st.button("Generate PDF"):
167
- pdf_file = generate_pdf(content)
168
- st.success(f"PDF report generated successfully: {pdf_file}")
169
 
170
- # Option to download the PDF file
171
- with open(pdf_file, "rb") as file:
 
172
  st.download_button(
173
- label="Download Medical Report",
174
- data=file,
175
- file_name="medical_report.pdf",
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
  )