Spaces:
Sleeping
Sleeping
Update pages/model.py
Browse files- pages/model.py +78 -0
pages/model.py
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
import easyocr
|
| 7 |
+
|
| 8 |
+
from langchain.prompts import PromptTemplate
|
| 9 |
+
from langchain.chains import LLMChain
|
| 10 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF_TOKEN")
|
| 14 |
+
|
| 15 |
+
st.set_page_config(page_title="π§ MediAssist", layout="centered")
|
| 16 |
+
|
| 17 |
+
st.markdown("<h1 style='text-align: center; color: #4A90E2;'>π§ MediAssist</h1>", unsafe_allow_html=True)
|
| 18 |
+
st.markdown("<h4 style='text-align: center;'>Upload a doctor's prescription and get detailed medicine analysis</h4><br>", unsafe_allow_html=True)
|
| 19 |
+
|
| 20 |
+
uploaded_file = st.file_uploader("π€ Upload Prescription Image", type=["jpg", "jpeg", "png"])
|
| 21 |
+
|
| 22 |
+
if uploaded_file:
|
| 23 |
+
|
| 24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
| 25 |
+
temp_file.write(uploaded_file.read())
|
| 26 |
+
img_path = temp_file.name
|
| 27 |
+
|
| 28 |
+
image = cv2.imread(img_path)
|
| 29 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
_, binary = cv2.threshold(gray, 130, 255, cv2.THRESH_BINARY_INV)
|
| 31 |
+
dilated = cv2.dilate(binary, np.ones((2, 2), np.uint8), iterations=1)
|
| 32 |
+
|
| 33 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 34 |
+
extracted_text = "\n".join(reader.readtext(dilated, detail=0))
|
| 35 |
+
|
| 36 |
+
st.image(dilated, caption="π§Ύ Preprocessed Image", use_column_width=True)
|
| 37 |
+
st.markdown("### π Extracted Text from Image")
|
| 38 |
+
st.code(extracted_text)
|
| 39 |
+
|
| 40 |
+
template = """
|
| 41 |
+
You're a medical assistant AI. Below is a doctor's handwritten prescription text:
|
| 42 |
+
|
| 43 |
+
{prescription_text}
|
| 44 |
+
|
| 45 |
+
Based on the text, please do the following:
|
| 46 |
+
1. Extract all medicine names (ignore other notes).
|
| 47 |
+
2. For each medicine, mention:
|
| 48 |
+
- When to take it (morning/night, before/after food)
|
| 49 |
+
- Dosage
|
| 50 |
+
- Possible side effects
|
| 51 |
+
- Any precautions or special instructions
|
| 52 |
+
Return results in clear bullet points.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
|
| 56 |
+
|
| 57 |
+
llm = ChatHuggingFace(
|
| 58 |
+
llm=HuggingFaceEndpoint(
|
| 59 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 60 |
+
provider="novita",
|
| 61 |
+
temperature=0.5,
|
| 62 |
+
max_new_tokens=500,
|
| 63 |
+
task="conversational"
|
| 64 |
+
)
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
| 68 |
+
|
| 69 |
+
if st.button("π Analyze Prescription"):
|
| 70 |
+
with st.spinner("Analyzing the prescription..."):
|
| 71 |
+
response = chain.run(prescription_text=extracted_text)
|
| 72 |
+
st.markdown("### π Medicine Summary")
|
| 73 |
+
st.success(response)
|
| 74 |
+
|
| 75 |
+
os.remove(img_path)
|
| 76 |
+
|
| 77 |
+
else:
|
| 78 |
+
st.info("Upload a prescription image to start the analysis.")
|