File size: 2,157 Bytes
d73cde1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
60
61
62
63
from agent import generate_response,chat_history
import chainlit as cl
import fitz  # PyMuPDF for PDF extraction
import pytesseract
from PIL import Image
import io


@cl.author_rename
def rename(orig_author: str):
    rename_dict = {"Chatbot": "Makewell Assitant"}
    return rename_dict.get(orig_author, orig_author)

@cl.on_chat_start
async def start():
    await cl.Message(content=f"""# Welcome to Makewell Assistant!

    Hi There,👋 We're excited to have you on board This Conversational Solution is designed to assist you in creating a detailed tentative health care plan based on your medical prescription records and conversations.

.

    """).send()

# @cl.on_message
# async def main(message: cl.Message):
#   res = generate_response(message.content)
#   if message.content.lower() == "confirm":
#     print("confirm is called")
#     user_data=extract_data(chat_history=chat_history)
#   await cl.Message(res, author="AppointmentBot").send()
@cl.on_message
async def main(message: cl.Message):
    if message.elements:
        texts = []

        images = [file for file in message.elements if "image" in file.mime]

        for image in images:
            image_data = await image.get_bytes()
            image_text = await extract_text_from_image(image_data)
            texts.append(image_text)

        if texts:
            res = "\n".join(texts)
        else:
            res = "No supported files were found in the attachments."
    else:
        res = generate_response(message.content)

    if message.content.lower() == "confirm":
        user_data = extract_data(chat_history=chat_history)

    await cl.Message(content=res, author="Makewell Assistant").send()

async def extract_text_from_pdf(attachment):
    pdf_data = await attachment.get_bytes()
    pdf_text = ""
    with fitz.open(io.BytesIO(pdf_data)) as doc:
        for page in doc:
            pdf_text += page.get_text()
    return pdf_text

async def extract_text_from_image(image_data):
    image = Image.open(io.BytesIO(image_data))
    image_text = pytesseract.image_to_string(image)
    return image_text