ahmadmac commited on
Commit
8b13f25
·
verified ·
1 Parent(s): 6be36f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -35
app.py CHANGED
@@ -1,49 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
 
2
  from fpdf import FPDF
3
- import os
4
 
5
- llm_result = """
6
- Diagnosis: Pneumonia
 
7
 
8
- Prescription:
9
- - Amoxicillin 500 mg, twice daily for 7 days
10
- - Paracetamol 500 mg, every 6 hours for fever
11
- - Rest and hydration
12
- - Follow-up in 7 days if symptoms persist
13
- """
 
 
 
 
 
 
 
 
14
 
15
- def save_pdf(content):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  pdf = FPDF()
17
  pdf.add_page()
18
  pdf.set_font("Arial", size=12)
19
- pdf.multi_cell(0, 10, txt=content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- pdf_output_path = "prescription.pdf"
22
- pdf.output(pdf_output_path)
 
 
 
 
 
23
 
24
- # Return the path to download
25
- return pdf_output_path
 
 
 
 
 
 
 
 
26
 
27
- # Streamlit app
28
- def main():
29
- st.title("Doctor's Assistance: Review and Edit Prescription")
30
 
31
- st.write("## Review the LLM-generated prescription and make edits if necessary.")
32
- edited_text = st.text_area("Edit Prescription", value=llm_result, height=300)
33
-
34
- if st.button("Save Prescription"):
35
- if edited_text.strip():
36
- pdf_file_path = save_pdf(edited_text)
37
- st.success("Prescription saved!")
38
- with open(pdf_file_path, "rb") as file:
39
- st.download_button(
40
- label="Download Prescription as PDF",
41
- data=file,
42
- file_name="prescription.pdf",
43
- mime="application/pdf"
44
- )
45
- else:
46
- st.error("Prescription content is empty. Please add details.")
47
 
 
 
 
 
 
 
 
 
 
 
48
  if __name__ == "__main__":
49
- main()
 
 
1
+ # import streamlit as st
2
+ # from fpdf import FPDF
3
+ # import os
4
+
5
+ # llm_result = """
6
+ # Diagnosis: Pneumonia
7
+
8
+ # Prescription:
9
+ # - Amoxicillin 500 mg, twice daily for 7 days
10
+ # - Paracetamol 500 mg, every 6 hours for fever
11
+ # - Rest and hydration
12
+ # - Follow-up in 7 days if symptoms persist
13
+ # """
14
+
15
+ # def save_pdf(content):
16
+ # pdf = FPDF()
17
+ # pdf.add_page()
18
+ # pdf.set_font("Arial", size=12)
19
+ # pdf.multi_cell(0, 10, txt=content)
20
+
21
+ # pdf_output_path = "prescription.pdf"
22
+ # pdf.output(pdf_output_path)
23
+
24
+ # # Return the path to download
25
+ # return pdf_output_path
26
+
27
+ # # Streamlit app
28
+ # def main():
29
+ # st.title("Doctor's Assistance: Review and Edit Prescription")
30
+
31
+ # st.write("## Review the LLM-generated prescription and make edits if necessary.")
32
+ # edited_text = st.text_area("Edit Prescription", value=llm_result, height=300)
33
+
34
+ # if st.button("Save Prescription"):
35
+ # if edited_text.strip():
36
+ # pdf_file_path = save_pdf(edited_text)
37
+ # st.success("Prescription saved!")
38
+ # with open(pdf_file_path, "rb") as file:
39
+ # st.download_button(
40
+ # label="Download Prescription as PDF",
41
+ # data=file,
42
+ # file_name="prescription.pdf",
43
+ # mime="application/pdf"
44
+ # )
45
+ # else:
46
+ # st.error("Prescription content is empty. Please add details.")
47
+
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
+ # Initialize speech recognition and TTS engine
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
+ audio = recognizer.listen(source)
64
+ try:
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
+ except sr.RequestError:
71
+ st.write("Sorry, there was an issue with the speech recognition service.")
72
+ return ""
73
 
74
+ def tts(text):
75
+ """Convert text to speech."""
76
+ engine.say(text)
77
+ engine.runAndWait()
78
+
79
+ def predict_disease(symptoms):
80
+ """Mock prediction of disease based on symptoms."""
81
+ # Dummy response for demo purposes
82
+ if 'fever' in symptoms.lower():
83
+ return "You might have the flu."
84
+ else:
85
+ return "No prediction available."
86
+
87
+ def generate_pdf(content):
88
+ """Generate PDF with the given content."""
89
  pdf = FPDF()
90
  pdf.add_page()
91
  pdf.set_font("Arial", size=12)
92
+ pdf.multi_cell(200, 10, content)
93
+ # Save the PDF to a file
94
+ pdf_file = 'medical_report.pdf'
95
+ pdf.output(pdf_file)
96
+ return pdf_file
97
+
98
+ # Streamlit App
99
+ st.title("Medical Assistance for Doctors (Voice and Text)")
100
+
101
+ # Step 1: Doctor inputs symptoms through voice or typing
102
+ st.header("Enter Symptoms")
103
+ symptoms = ""
104
+ if st.button("Use Voice Input for Symptoms"):
105
+ symptoms = listen_to_voice()
106
+
107
+ typed_symptoms = st.text_area("Or Enter Symptoms Using Keyboard:", symptoms)
108
+ if typed_symptoms:
109
+ symptoms = typed_symptoms
110
 
111
+ if symptoms:
112
+ st.write(f"Entered Symptoms: {symptoms}")
113
+
114
+ # Step 2: Predict disease based on symptoms
115
+ if st.button("Predict Disease"):
116
+ prediction = predict_disease(symptoms)
117
+ st.write(f"Prediction: {prediction}")
118
 
119
+ # Step 3: Allow editing through text or voice
120
+ st.header("Edit Response (Optional)")
121
+ if st.button("Edit Response Using Voice"):
122
+ edited_response = listen_to_voice()
123
+ if edited_response:
124
+ prediction = edited_response
125
+ else:
126
+ edited_response = st.text_area("Or Edit Response Using Keyboard:", prediction)
127
+ if edited_response:
128
+ prediction = edited_response
129
 
130
+ # Final Response
131
+ st.write(f"Final Response: {prediction}")
 
132
 
133
+ # Option to listen to the final response
134
+ if st.button("Play Final Response"):
135
+ tts(prediction)
136
+
137
+ # Step 4: Generate PDF
138
+ st.header("Generate PDF Report")
139
+ content = f"Symptoms: {symptoms}\nPrediction: {prediction}"
140
+ if st.button("Generate PDF"):
141
+ pdf_file = generate_pdf(content)
142
+ st.success(f"PDF report generated successfully: {pdf_file}")
 
 
 
 
 
 
143
 
144
+ # Step 5: Download PDF
145
+ with open(pdf_file, "rb") as file:
146
+ st.download_button(
147
+ label="Download Medical Report",
148
+ data=file,
149
+ file_name="medical_report.pdf",
150
+ mime="application/pdf"
151
+ )
152
+
153
+ # Run Streamlit App
154
  if __name__ == "__main__":
155
+ st.write("Welcome to the medical assistance system.")
156
+