Spaces:
Runtime error
Runtime error
Commit
·
1c45df7
1
Parent(s):
5e50320
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ import os
|
|
| 3 |
import pickle
|
| 4 |
import time
|
| 5 |
import g4f
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(page_title="MEDICAL ASSISTANT")
|
| 8 |
|
|
@@ -65,6 +69,9 @@ def display_chats_sidebar():
|
|
| 65 |
st.session_state.conversations = []
|
| 66 |
st.session_state.current_conversation = []
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
with st.sidebar.container():
|
| 69 |
st.header('Conversations')
|
| 70 |
for idx, conversation in enumerate(st.session_state.conversations):
|
|
@@ -75,23 +82,59 @@ def display_chats_sidebar():
|
|
| 75 |
st.session_state.current_conversation = st.session_state.conversations[idx]
|
| 76 |
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def main_app():
|
| 79 |
for message in st.session_state.current_conversation:
|
| 80 |
with st.chat_message(message["role"]):
|
| 81 |
st.write(message["content"])
|
| 82 |
|
| 83 |
def generate_response(prompt_input):
|
| 84 |
-
string_dialogue =
|
| 85 |
-
You are a virtual medical assistant/advisor with extensive knowledge in general health, medicine, and wellness.
|
| 86 |
-
Your role is to provide helpful, informative, and non-diagnostic advice on general health questions, such as tips for maintaining a healthy lifestyle,
|
| 87 |
-
explanations of medical conditions in layman's terms, and general information about medications and treatments.
|
| 88 |
-
You do not provide personal medical consultations, diagnoses, or treatment recommendations.
|
| 89 |
-
You are programmed to refrain from answering any queries that are not related to general medical knowledge or advice.
|
| 90 |
-
'''
|
| 91 |
for dict_message in st.session_state.current_conversation:
|
| 92 |
string_dialogue += dict_message["role"].capitalize() + ": " + dict_message["content"] + "\\n\\n"
|
| 93 |
|
| 94 |
-
prompt = f"{string_dialogue}\n
|
| 95 |
response_generator = g4f.ChatCompletion.create(
|
| 96 |
model="gpt-3.5-turbo",
|
| 97 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -119,4 +162,10 @@ def main_app():
|
|
| 119 |
|
| 120 |
|
| 121 |
display_chats_sidebar()
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import pickle
|
| 4 |
import time
|
| 5 |
import g4f
|
| 6 |
+
import tempfile
|
| 7 |
+
import PyPDF2
|
| 8 |
+
from pdf2image import convert_from_path
|
| 9 |
+
import pytesseract
|
| 10 |
|
| 11 |
st.set_page_config(page_title="MEDICAL ASSISTANT")
|
| 12 |
|
|
|
|
| 69 |
st.session_state.conversations = []
|
| 70 |
st.session_state.current_conversation = []
|
| 71 |
|
| 72 |
+
if st.sidebar.button('Summarize Bills', key="summarize_bills", use_container_width=True):
|
| 73 |
+
st.session_state.page = "summarize_bills"
|
| 74 |
+
|
| 75 |
with st.sidebar.container():
|
| 76 |
st.header('Conversations')
|
| 77 |
for idx, conversation in enumerate(st.session_state.conversations):
|
|
|
|
| 82 |
st.session_state.current_conversation = st.session_state.conversations[idx]
|
| 83 |
|
| 84 |
|
| 85 |
+
def summarize_bill():
|
| 86 |
+
st.header("Summarize Bills")
|
| 87 |
+
|
| 88 |
+
if st.button("Back to Chat"):
|
| 89 |
+
st.session_state.page = "chat"
|
| 90 |
+
|
| 91 |
+
uploaded_file = st.file_uploader("Upload a Bill", type=['pdf'])
|
| 92 |
+
if uploaded_file is not None:
|
| 93 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 94 |
+
tmp_file.write(uploaded_file.read())
|
| 95 |
+
extracted_text = extract_text_from_pdf(tmp_file.name)
|
| 96 |
+
|
| 97 |
+
if st.button('Summarize'):
|
| 98 |
+
# Assuming g4f.ChatCompletion can be used for summarization
|
| 99 |
+
# Replace with appropriate summarization logic if needed
|
| 100 |
+
summary = g4f.ChatCompletion.create(
|
| 101 |
+
model="gpt-3.5-turbo",
|
| 102 |
+
messages=[{"role": "user", "content": extracted_text}],
|
| 103 |
+
temperature=0.5, # You can adjust parameters as needed
|
| 104 |
+
max_tokens=150 # Adjust the token limit as needed
|
| 105 |
+
)
|
| 106 |
+
st.text_area("Summary", summary, height=400)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def extract_text_from_pdf(file_path: str) -> str:
|
| 110 |
+
try:
|
| 111 |
+
with open(file_path, 'rb') as file:
|
| 112 |
+
reader = PyPDF2.PdfReader(file)
|
| 113 |
+
text = ''
|
| 114 |
+
for page_number in range(len(reader.pages)):
|
| 115 |
+
page = reader.pages[page_number]
|
| 116 |
+
text += page.extract_text()
|
| 117 |
+
return text
|
| 118 |
+
except Exception as e:
|
| 119 |
+
try:
|
| 120 |
+
images = convert_from_path(file_path)
|
| 121 |
+
extracted_texts = [pytesseract.image_to_string(image) for image in images]
|
| 122 |
+
return "\n".join(extracted_texts)
|
| 123 |
+
except Exception as e:
|
| 124 |
+
raise ValueError(f"Failed to process {file_path} using PDF Reader and OCR. Error: {e}")
|
| 125 |
+
|
| 126 |
+
|
| 127 |
def main_app():
|
| 128 |
for message in st.session_state.current_conversation:
|
| 129 |
with st.chat_message(message["role"]):
|
| 130 |
st.write(message["content"])
|
| 131 |
|
| 132 |
def generate_response(prompt_input):
|
| 133 |
+
string_dialogue = "You are a helpful Medical Assistant. You will Only respond to Medical related Queries. Say Sorry to any other Type of Queries."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
for dict_message in st.session_state.current_conversation:
|
| 135 |
string_dialogue += dict_message["role"].capitalize() + ": " + dict_message["content"] + "\\n\\n"
|
| 136 |
|
| 137 |
+
prompt = f"{string_dialogue}\n {prompt_input} Assistant: "
|
| 138 |
response_generator = g4f.ChatCompletion.create(
|
| 139 |
model="gpt-3.5-turbo",
|
| 140 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
display_chats_sidebar()
|
| 165 |
+
if st.session_state.get('page') == "summarize_bills":
|
| 166 |
+
summarize_bill()
|
| 167 |
+
elif st.session_state.get('page') == "chat":
|
| 168 |
+
main_app()
|
| 169 |
+
else:
|
| 170 |
+
# Default page when the app starts or when the state is not set
|
| 171 |
+
main_app()
|