ahmadmac commited on
Commit
df77779
·
verified ·
1 Parent(s): 32910d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -45
app.py CHANGED
@@ -56,38 +56,41 @@ from fpdf import FPDF
56
  recognizer = sr.Recognizer()
57
  engine = pyttsx3.init()
58
 
 
59
  def listen_to_voice():
60
  """Listen to audio and convert to text."""
61
- with sr.Microphone() as source:
62
- st.write("Listening for symptoms (speak now)...")
63
- try:
64
  audio = recognizer.listen(source, timeout=5, phrase_time_limit=10)
65
  text = recognizer.recognize_google(audio)
66
  st.write(f"You said: {text}")
67
  return text
68
- except sr.UnknownValueError:
69
- st.write("Sorry, I did not understand the audio.")
70
- return ""
71
- except sr.RequestError as e:
72
- st.write(f"Could not request results from Google Speech Recognition service; {e}")
73
- return ""
74
- except Exception as e:
75
- st.write(f"Error: {str(e)}")
76
- return ""
77
-
 
78
  def tts(text):
79
  """Convert text to speech."""
80
  engine.say(text)
81
  engine.runAndWait()
82
 
 
83
  def predict_disease(symptoms):
84
  """Mock prediction of disease based on symptoms."""
85
- # Dummy response for demo purposes
86
  if 'fever' in symptoms.lower():
87
  return "You might have the flu."
88
  else:
89
  return "No prediction available."
90
 
 
91
  def generate_pdf(content):
92
  """Generate PDF with the given content."""
93
  pdf = FPDF()
@@ -99,19 +102,23 @@ def generate_pdf(content):
99
  pdf.output(pdf_file)
100
  return pdf_file
101
 
102
- # Streamlit App
103
- st.title("Medical Assistance for Doctors (Voice and Text)")
104
 
105
- # Step 1: Doctor inputs symptoms through voice or typing
106
  st.header("Enter Symptoms")
107
- symptoms = ""
108
- if st.button("Use Voice Input for Symptoms"):
109
- symptoms = listen_to_voice()
110
 
111
- typed_symptoms = st.text_area("Or Enter Symptoms Using Keyboard:", symptoms)
112
- if typed_symptoms:
113
- symptoms = typed_symptoms
 
 
 
 
 
 
114
 
 
115
  if symptoms:
116
  st.write(f"Entered Symptoms: {symptoms}")
117
 
@@ -120,42 +127,47 @@ if st.button("Predict Disease"):
120
  prediction = predict_disease(symptoms)
121
  st.write(f"Prediction: {prediction}")
122
 
123
- # Step 3: Allow editing through text or voice
124
  st.header("Edit Response (Optional)")
125
- if st.button("Edit Response Using Voice"):
126
- edited_response = listen_to_voice()
127
- if edited_response:
128
- prediction = edited_response
129
- else:
130
  edited_response = st.text_area("Or Edit Response Using Keyboard:", prediction)
131
- if edited_response:
132
- prediction = edited_response
133
-
134
- # Final Response
 
 
 
 
 
135
  st.write(f"Final Response: {prediction}")
136
 
137
- # Option to listen to the final response
138
  if st.button("Play Final Response"):
139
  tts(prediction)
140
 
141
- # Step 4: Generate PDF
142
- st.header("Generate PDF Report")
143
  content = f"Symptoms: {symptoms}\nPrediction: {prediction}"
 
144
  if st.button("Generate PDF"):
145
  pdf_file = generate_pdf(content)
146
  st.success(f"PDF report generated successfully: {pdf_file}")
147
 
148
- # Step 5: Download PDF
149
- with open(pdf_file, "rb") as file:
150
- st.download_button(
151
- label="Download Medical Report",
152
- data=file,
153
- file_name="medical_report.pdf",
154
- mime="application/pdf"
155
- )
156
 
157
  # Run Streamlit App
158
  if __name__ == "__main__":
159
  st.write("Welcome to the medical assistance system.")
160
 
161
-
 
56
  recognizer = sr.Recognizer()
57
  engine = pyttsx3.init()
58
 
59
+ # Function to listen to voice input
60
  def listen_to_voice():
61
  """Listen to audio and convert to text."""
62
+ try:
63
+ with sr.Microphone() as source:
64
+ st.write("Listening for symptoms (speak now)...")
65
  audio = recognizer.listen(source, timeout=5, phrase_time_limit=10)
66
  text = recognizer.recognize_google(audio)
67
  st.write(f"You said: {text}")
68
  return text
69
+ except sr.UnknownValueError:
70
+ st.write("Sorry, I did not understand the audio.")
71
+ return ""
72
+ except sr.RequestError as e:
73
+ st.write(f"Could not request results from Google Speech Recognition service; {e}")
74
+ return ""
75
+ except Exception as e:
76
+ st.write(f"Error: {str(e)}")
77
+ return ""
78
+
79
+ # Text-to-speech function
80
  def tts(text):
81
  """Convert text to speech."""
82
  engine.say(text)
83
  engine.runAndWait()
84
 
85
+ # Dummy disease prediction based on symptoms
86
  def predict_disease(symptoms):
87
  """Mock prediction of disease based on symptoms."""
 
88
  if 'fever' in symptoms.lower():
89
  return "You might have the flu."
90
  else:
91
  return "No prediction available."
92
 
93
+ # Function to generate PDF
94
  def generate_pdf(content):
95
  """Generate PDF with the given content."""
96
  pdf = FPDF()
 
102
  pdf.output(pdf_file)
103
  return pdf_file
104
 
105
+ # Streamlit App Interface
106
+ st.title("Medical Assistance for Doctors")
107
 
108
+ # Step 1: Input Symptoms via Text Box with a voice input icon
109
  st.header("Enter Symptoms")
 
 
 
110
 
111
+ # Display a text input box with a microphone button next to it
112
+ col1, col2 = st.columns([5, 1]) # Create a layout with 5:1 ratio for text input and button
113
+
114
+ with col1:
115
+ symptoms = st.text_area("Enter Symptoms Using Keyboard:")
116
+
117
+ with col2:
118
+ if st.button("🎤"):
119
+ symptoms = listen_to_voice() # Capture voice input when the mic icon is pressed
120
 
121
+ # Display entered symptoms
122
  if symptoms:
123
  st.write(f"Entered Symptoms: {symptoms}")
124
 
 
127
  prediction = predict_disease(symptoms)
128
  st.write(f"Prediction: {prediction}")
129
 
130
+ # Step 3: Edit Response via Voice or Text
131
  st.header("Edit Response (Optional)")
132
+
133
+ # Mic button for editing response through voice
134
+ col3, col4 = st.columns([5, 1])
135
+
136
+ with col3:
137
  edited_response = st.text_area("Or Edit Response Using Keyboard:", prediction)
138
+
139
+ with col4:
140
+ if st.button("🎤 Edit by Voice"):
141
+ edited_response = listen_to_voice() # Voice editing
142
+
143
+ if edited_response:
144
+ prediction = edited_response
145
+
146
+ # Display the final response
147
  st.write(f"Final Response: {prediction}")
148
 
149
+ # Text-to-speech option for the final response
150
  if st.button("Play Final Response"):
151
  tts(prediction)
152
 
153
+ # Step 4: Generate and Download PDF
154
+ st.header("Generate and Download PDF Report")
155
  content = f"Symptoms: {symptoms}\nPrediction: {prediction}"
156
+
157
  if st.button("Generate PDF"):
158
  pdf_file = generate_pdf(content)
159
  st.success(f"PDF report generated successfully: {pdf_file}")
160
 
161
+ # Option to download the PDF file
162
+ with open(pdf_file, "rb") as file:
163
+ st.download_button(
164
+ label="Download Medical Report",
165
+ data=file,
166
+ file_name="medical_report.pdf",
167
+ mime="application/pdf"
168
+ )
169
 
170
  # Run Streamlit App
171
  if __name__ == "__main__":
172
  st.write("Welcome to the medical assistance system.")
173