Update settings.py
Browse files- settings.py +231 -232
settings.py
CHANGED
@@ -1,233 +1,232 @@
|
|
1 |
-
import traceback
|
2 |
-
from datetime import datetime
|
3 |
-
from pathlib import Path
|
4 |
-
import os
|
5 |
-
import random
|
6 |
-
import string
|
7 |
-
import tempfile
|
8 |
-
import re
|
9 |
-
import io
|
10 |
-
import PyPDF2
|
11 |
-
import docx
|
12 |
-
from reportlab.pdfgen import canvas
|
13 |
-
from reportlab.lib.pagesizes import letter
|
14 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
15 |
-
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
16 |
-
from reportlab.lib.enums import TA_JUSTIFY
|
17 |
-
from ai_config import n_of_questions, load_model, openai_api_key, convert_text_to_speech
|
18 |
-
from knowledge_retrieval import setup_knowledge_retrieval, generate_report
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
question_count
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
"
|
75 |
-
"
|
76 |
-
"
|
77 |
-
|
78 |
-
})
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
speech_file_path
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
speech_file_path
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
print(
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
file_content
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
print(
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
"
|
175 |
-
"
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
"
|
193 |
-
"
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
bold_text
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
flowables.
|
230 |
-
|
231 |
-
|
232 |
-
doc.build(flowables)
|
233 |
return temp_file.name
|
|
|
1 |
+
import traceback
|
2 |
+
from datetime import datetime
|
3 |
+
from pathlib import Path
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
import string
|
7 |
+
import tempfile
|
8 |
+
import re
|
9 |
+
import io
|
10 |
+
import PyPDF2
|
11 |
+
import docx
|
12 |
+
from reportlab.pdfgen import canvas
|
13 |
+
from reportlab.lib.pagesizes import letter
|
14 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
15 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
16 |
+
from reportlab.lib.enums import TA_JUSTIFY
|
17 |
+
from ai_config import n_of_questions, load_model, openai_api_key, convert_text_to_speech
|
18 |
+
from knowledge_retrieval import setup_knowledge_retrieval, generate_report
|
19 |
+
|
20 |
+
# Initialize settings
|
21 |
+
n_of_questions = n_of_questions()
|
22 |
+
current_datetime = datetime.now()
|
23 |
+
human_readable_datetime = current_datetime.strftime("%B %d, %Y at %H:%M")
|
24 |
+
current_date = current_datetime.strftime("%Y-%m-%d")
|
25 |
+
|
26 |
+
# Initialize the model and retrieval chain
|
27 |
+
try:
|
28 |
+
llm = load_model(openai_api_key)
|
29 |
+
interview_retrieval_chain, report_retrieval_chain, combined_retriever = setup_knowledge_retrieval(llm)
|
30 |
+
knowledge_base_connected = True
|
31 |
+
print("Successfully connected to the knowledge base.")
|
32 |
+
except Exception as e:
|
33 |
+
print(f"Error initializing the model or retrieval chain: {str(e)}")
|
34 |
+
knowledge_base_connected = False
|
35 |
+
print("Falling back to basic mode without knowledge base.")
|
36 |
+
|
37 |
+
question_count = 0
|
38 |
+
interview_history = []
|
39 |
+
last_audio_path = None # Variable to store the path of the last audio file
|
40 |
+
initial_audio_path = None # Variable to store the path of the initial audio file
|
41 |
+
language = None
|
42 |
+
|
43 |
+
def generate_random_string(length=5):
|
44 |
+
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
45 |
+
|
46 |
+
def respond(message, history):
|
47 |
+
global question_count, interview_history, combined_retriever, last_audio_path, initial_audio_path, language, interview_retrieval_chain, report_retrieval_chain
|
48 |
+
|
49 |
+
if not isinstance(history, list):
|
50 |
+
history = []
|
51 |
+
if not history or not history[-1]:
|
52 |
+
history.append(["", ""])
|
53 |
+
|
54 |
+
# Extract the actual message text
|
55 |
+
if isinstance(message, list):
|
56 |
+
message = message[-1][0] if message and isinstance(message[-1], list) else message[-1]
|
57 |
+
|
58 |
+
question_count += 1
|
59 |
+
interview_history.append(f"Q{question_count}: {message}")
|
60 |
+
history_str = "\n".join(interview_history)
|
61 |
+
|
62 |
+
try:
|
63 |
+
if knowledge_base_connected:
|
64 |
+
if question_count == 1:
|
65 |
+
# Capture the language from the first response
|
66 |
+
language = message.strip().lower()
|
67 |
+
# Reinitialize the interview chain with the new language
|
68 |
+
interview_retrieval_chain, report_retrieval_chain, combined_retriever = setup_knowledge_retrieval(
|
69 |
+
llm, language)
|
70 |
+
|
71 |
+
if question_count < n_of_questions:
|
72 |
+
result = interview_retrieval_chain.invoke({
|
73 |
+
"input": f"Based on the patient's statement: '{message}', what should be the next question?",
|
74 |
+
"history": history_str,
|
75 |
+
"question_number": question_count + 1,
|
76 |
+
"language": language
|
77 |
+
})
|
78 |
+
question = result.get("answer", f"Can you tell me more about that? (in {language})")
|
79 |
+
else:
|
80 |
+
result = generate_report(report_retrieval_chain, interview_history, language)
|
81 |
+
question = result
|
82 |
+
speech_file_path = None # Skip audio generation for the report
|
83 |
+
|
84 |
+
if question:
|
85 |
+
random_suffix = generate_random_string()
|
86 |
+
speech_file_path = Path(__file__).parent / f"question_{question_count}_{random_suffix}.mp3"
|
87 |
+
convert_text_to_speech(question, speech_file_path)
|
88 |
+
print(f"Question {question_count} saved as audio at {speech_file_path}")
|
89 |
+
|
90 |
+
# Remove the last audio file if it exists
|
91 |
+
if last_audio_path and os.path.exists(last_audio_path):
|
92 |
+
os.remove(last_audio_path)
|
93 |
+
last_audio_path = speech_file_path
|
94 |
+
else:
|
95 |
+
speech_file_path = None # Skip audio generation for the report
|
96 |
+
|
97 |
+
else:
|
98 |
+
# Fallback mode without knowledge base
|
99 |
+
question = f"Can you elaborate on that? (in {language})"
|
100 |
+
if question_count < n_of_questions:
|
101 |
+
speech_file_path = Path(__file__).parent / f"question_{question_count}.mp3"
|
102 |
+
convert_text_to_speech(question, speech_file_path)
|
103 |
+
print(f"Question {question_count} saved as audio at {speech_file_path}")
|
104 |
+
|
105 |
+
if last_audio_path and os.path.exists(last_audio_path):
|
106 |
+
os.remove(last_audio_path)
|
107 |
+
last_audio_path = speech_file_path
|
108 |
+
else:
|
109 |
+
speech_file_path = None
|
110 |
+
|
111 |
+
history[-1][1] = f"{question}"
|
112 |
+
|
113 |
+
# Remove the initial question audio file after the first user response
|
114 |
+
if initial_audio_path and os.path.exists(initial_audio_path):
|
115 |
+
os.remove(initial_audio_path)
|
116 |
+
initial_audio_path = None
|
117 |
+
|
118 |
+
return history, str(speech_file_path) if speech_file_path else None
|
119 |
+
|
120 |
+
except Exception as e:
|
121 |
+
print(f"Error in retrieval chain: {str(e)}")
|
122 |
+
print(traceback.format_exc())
|
123 |
+
return history, None
|
124 |
+
|
125 |
+
|
126 |
+
def reset_interview():
|
127 |
+
"""Reset the interview state."""
|
128 |
+
global question_count, interview_history, last_audio_path, initial_audio_path
|
129 |
+
question_count = 0
|
130 |
+
interview_history = []
|
131 |
+
if last_audio_path and os.path.exists(last_audio_path):
|
132 |
+
os.remove(last_audio_path)
|
133 |
+
last_audio_path = None
|
134 |
+
initial_audio_path = None
|
135 |
+
|
136 |
+
|
137 |
+
def read_file(file):
|
138 |
+
if file is None:
|
139 |
+
return "No file uploaded"
|
140 |
+
|
141 |
+
if isinstance(file, str):
|
142 |
+
with open(file, 'r', encoding='utf-8') as f:
|
143 |
+
return f.read()
|
144 |
+
|
145 |
+
if hasattr(file, 'name'): # Check if it's a file-like object
|
146 |
+
if file.name.endswith('.txt'):
|
147 |
+
return file.content
|
148 |
+
elif file.name.endswith('.pdf'):
|
149 |
+
pdf_reader = PyPDF2.PdfReader(io.BytesIO(file.content))
|
150 |
+
return "\n".join(page.extract_text() for page in pdf_reader.pages)
|
151 |
+
elif file.name.endswith('.docx'):
|
152 |
+
doc = docx.Document(io.BytesIO(file.content))
|
153 |
+
return "\n".join(paragraph.text for paragraph in doc.paragraphs)
|
154 |
+
else:
|
155 |
+
return "Unsupported file format"
|
156 |
+
|
157 |
+
return "Unable to read file"
|
158 |
+
|
159 |
+
def generate_report_from_file(file, language):
|
160 |
+
try:
|
161 |
+
file_content = read_file(file)
|
162 |
+
if file_content == "No file uploaded" or file_content == "Unsupported file format" or file_content == "Unable to read file":
|
163 |
+
return file_content
|
164 |
+
|
165 |
+
report_language = language.strip().lower() if language else "english"
|
166 |
+
print('preferred language:', report_language)
|
167 |
+
print(f"Generating report in language: {report_language}") # For debugging
|
168 |
+
|
169 |
+
# Reinitialize the report chain with the new language
|
170 |
+
_, report_retrieval_chain, _ = setup_knowledge_retrieval(llm, report_language)
|
171 |
+
|
172 |
+
result = report_retrieval_chain.invoke({
|
173 |
+
"input": "Please provide a clinical report based on the following content:",
|
174 |
+
"history": file_content,
|
175 |
+
"language": report_language
|
176 |
+
})
|
177 |
+
report_content = result.get("answer", "Unable to generate report due to insufficient information.")
|
178 |
+
pdf_path = create_pdf(report_content)
|
179 |
+
return report_content, pdf_path
|
180 |
+
except Exception as e:
|
181 |
+
return f"An error occurred while processing the file: {str(e)}", None
|
182 |
+
|
183 |
+
|
184 |
+
def generate_interview_report(interview_history, language):
|
185 |
+
try:
|
186 |
+
report_language = language.strip().lower() if language else "english"
|
187 |
+
print('preferred report_language language:', report_language)
|
188 |
+
_, report_retrieval_chain, _ = setup_knowledge_retrieval(llm, report_language)
|
189 |
+
|
190 |
+
result = report_retrieval_chain.invoke({
|
191 |
+
"input": "Please provide a clinical report based on the following interview:",
|
192 |
+
"history": "\n".join(interview_history),
|
193 |
+
"language": report_language
|
194 |
+
})
|
195 |
+
report_content = result.get("answer", "Unable to generate report due to insufficient information.")
|
196 |
+
pdf_path = create_pdf(report_content)
|
197 |
+
return report_content, pdf_path
|
198 |
+
except Exception as e:
|
199 |
+
return f"An error occurred while generating the report: {str(e)}", None
|
200 |
+
|
201 |
+
def create_pdf(content):
|
202 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.pdf')
|
203 |
+
doc = SimpleDocTemplate(temp_file.name, pagesize=letter)
|
204 |
+
styles = getSampleStyleSheet()
|
205 |
+
|
206 |
+
# Create a custom style for bold text
|
207 |
+
bold_style = ParagraphStyle('Bold', parent=styles['Normal'], fontName='Helvetica-Bold', fontSize=10)
|
208 |
+
|
209 |
+
# Create a custom style for normal text with justification
|
210 |
+
normal_style = ParagraphStyle('Normal', parent=styles['Normal'], alignment=TA_JUSTIFY)
|
211 |
+
|
212 |
+
flowables = []
|
213 |
+
|
214 |
+
for line in content.split('\n'):
|
215 |
+
# Use regex to find words surrounded by **
|
216 |
+
parts = re.split(r'(\*\*.*?\*\*)', line)
|
217 |
+
paragraph_parts = []
|
218 |
+
|
219 |
+
for part in parts:
|
220 |
+
if part.startswith('**') and part.endswith('**'):
|
221 |
+
# Bold text
|
222 |
+
bold_text = part.strip('**')
|
223 |
+
paragraph_parts.append(Paragraph(bold_text, bold_style))
|
224 |
+
else:
|
225 |
+
# Normal text
|
226 |
+
paragraph_parts.append(Paragraph(part, normal_style))
|
227 |
+
|
228 |
+
flowables.extend(paragraph_parts)
|
229 |
+
flowables.append(Spacer(1, 12)) # Add space between paragraphs
|
230 |
+
|
231 |
+
doc.build(flowables)
|
|
|
232 |
return temp_file.name
|