Yashvj123 commited on
Commit
0aec70a
·
verified ·
1 Parent(s): 0288630

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -20
app.py CHANGED
@@ -4,6 +4,13 @@ import numpy as np
4
  import tempfile
5
  import os
6
 
 
 
 
 
 
 
 
7
  st.set_page_config(
8
  page_title="MediAssist - Prescription Analyzer",
9
  layout="wide",
@@ -43,21 +50,33 @@ uploaded_file = st.file_uploader("📤 Upload Prescription Image (JPG/PNG)", typ
43
  # Columns for layout
44
  col1, col2 = st.columns([1, 2])
45
 
46
- if uploaded_file:
47
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
48
- temp_file.write(uploaded_file.read())
49
- temp_path = temp_file.name
 
 
 
50
 
51
- # Step 1: Read and convert to grayscale
52
- image = cv2.imread(temp_path)
53
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
54
 
55
- # Step 2: Inverse Binary Thresholding
56
- _, binary_inv = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
57
 
58
- # Step 3: Dilation with 2 iterations
59
- kernel = np.ones((3, 3), np.uint8)
60
- dilated = cv2.dilate(binary_inv, kernel, iterations=1)
 
 
 
 
 
 
 
 
 
 
61
 
62
  with col1:
63
  st.image(dilated, caption="Preprocessed Prescription", channels="GRAY", use_container_width=True)
@@ -65,20 +84,28 @@ if uploaded_file:
65
  with col2:
66
  st.success("✅ Prescription Uploaded & Preprocessed Successfully")
67
 
68
- # Placeholder text (replace with actual OCR + LLM output later)
69
  st.markdown("### 📜 Extracted Text")
70
- st.code("Paracetamol 500mg\nTake 1 tablet twice a day after meals", language='text')
 
 
 
 
 
 
 
71
 
72
- st.markdown("### 🌐 Translated Text")
73
- st.code("पेरासिटामोल 500 मिलीग्राम\nभोजन के बाद दिन में दो बार 1 गोली लें", language='text')
74
 
75
- st.markdown("### ⏱️ Tablet Timing & Instructions")
76
- st.info("- Morning after breakfast\n- Night after dinner\n- Take with water\n- Do not exceed 2 tablets in 24 hours")
77
 
78
- st.markdown("### ⚠️ Possible Side Effects")
79
- st.warning("- Nausea\n- Dizziness\n- Liver damage (on overdose)")
80
 
81
  os.remove(temp_path)
 
 
82
 
83
  else:
84
  st.image("https://cdn.dribbble.com/users/285475/screenshots/14711920/media/bd46dc2873f7099e4ef9fd53e6f7f1df.png", width=600)
 
4
  import tempfile
5
  import os
6
 
7
+ from langchain_community.document_loaders import UnstructuredImageLoader
8
+ from langchain.prompts import PromptTemplate
9
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
10
+
11
+ os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
12
+ os.environ["HF_TOKEN"] = os.getenv("HF")
13
+
14
  st.set_page_config(
15
  page_title="MediAssist - Prescription Analyzer",
16
  layout="wide",
 
50
  # Columns for layout
51
  col1, col2 = st.columns([1, 2])
52
 
53
+ dilated_path = orig_path.replace(".png", "_dilated.png")
54
+ cv2.imwrite(dilated_path, dilated)
55
+
56
+ # Load and extract text
57
+ loader = UnstructuredImageLoader(dilated_path)
58
+ documents = loader.load()
59
+ extracted_text = "\n".join([doc.page_content for doc in documents])
60
 
61
+ # Define prompt
62
+ template = """
63
+ You are a helpful assistant. Here is a prescription text extracted from an image:
64
 
65
+ {prescription_text}
 
66
 
67
+ Please summarize the key medicine names and instructions in bullet points.
68
+ """
69
+ prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
70
+
71
+ model = HuggingFaceEndpoint(
72
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3",
73
+ provider="novita",
74
+ temperature=0.6,
75
+ max_new_tokens=300,
76
+ task="conversational"
77
+ )
78
+
79
+ chain = LLMChain(llm=model, prompt=prompt)
80
 
81
  with col1:
82
  st.image(dilated, caption="Preprocessed Prescription", channels="GRAY", use_container_width=True)
 
84
  with col2:
85
  st.success("✅ Prescription Uploaded & Preprocessed Successfully")
86
 
 
87
  st.markdown("### 📜 Extracted Text")
88
+ st.code(extracted_text)
89
+
90
+ if st.button("🔍 Analyze Text"):
91
+ with st.spinner("Analyzing..."):
92
+ response = chain.run(prescription_text=extracted_text)
93
+ # st.markdown("### LLM Output")
94
+ st.success(response)
95
+
96
 
97
+ # st.markdown("### 🌐 Translated Text")
98
+ # st.code("पेरासिटामोल 500 मिलीग्राम\nभोजन के बाद दिन में दो बार 1 गोली लें", language='text')
99
 
100
+ # st.markdown("### ⏱️ Tablet Timing & Instructions")
101
+ # st.info("- Morning after breakfast\n- Night after dinner\n- Take with water\n- Do not exceed 2 tablets in 24 hours")
102
 
103
+ # st.markdown("### ⚠️ Possible Side Effects")
104
+ # st.warning("- Nausea\n- Dizziness\n- Liver damage (on overdose)")
105
 
106
  os.remove(temp_path)
107
+ os.remove(orig_path)
108
+ os.remove(dilated_path)
109
 
110
  else:
111
  st.image("https://cdn.dribbble.com/users/285475/screenshots/14711920/media/bd46dc2873f7099e4ef9fd53e6f7f1df.png", width=600)