Spaces:
Running
Running
File size: 4,564 Bytes
290e3a2 0aec70a a8487e6 0aec70a a8487e6 0aec70a 290e3a2 47eeccd fab5988 47eeccd 290e3a2 a8487e6 d4b42e3 a8487e6 d4b42e3 a8487e6 d4b42e3 a8487e6 d4b42e3 a8487e6 d4b42e3 a8487e6 0aec70a a8487e6 0aec70a a8487e6 290e3a2 0288630 290e3a2 0aec70a a8487e6 0aec70a a8487e6 290e3a2 0aec70a 290e3a2 0aec70a 290e3a2 0aec70a 290e3a2 a8487e6 |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
import streamlit as st
import cv2
import numpy as np
import tempfile
import os
from langchain_community.document_loaders import UnstructuredImageLoader
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_huggingface import HuggingFaceEndpoint
# Set Hugging Face API keys
os.environ["HUGGINGFACEHUB_API_KEY"] = os.getenv("HF")
os.environ["HF_TOKEN"] = os.getenv("HF")
st.set_page_config(
page_title="MediAssist - Prescription Analyzer",
layout="wide",
page_icon="💊"
)
st.sidebar.title("💊 MediAssist")
st.sidebar.markdown("Analyze prescriptions with ease using AI")
st.sidebar.markdown("---")
st.sidebar.markdown("🔗 **Connect with me:**")
st.sidebar.markdown("""
<div style='display: flex; gap: 10px;'>
<a href="https://github.com/Yashvj22" target="_blank">
<img src="https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white" style="height:30px;">
</a>
<a href="https://www.linkedin.com/in/yash-jadhav-454b0a237/" target="_blank">
<img src="https://img.shields.io/badge/LinkedIn-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white" style="height:30px;">
</a>
</div>
""", unsafe_allow_html=True)
st.sidebar.markdown("---")
st.markdown("""
<h1 style='text-align: center; color: #4A90E2;'>🧠 MediAssist</h1>
<h3 style='text-align: center;'>Prescription Analyzer using AI and OCR</h3>
<p style='text-align: center;'>Upload a doctor's prescription image, and MediAssist will extract, translate, and explain it for you.</p>
<br>
""", unsafe_allow_html=True)
uploaded_file = st.file_uploader("📤 Upload Prescription Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
if uploaded_file:
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
temp_file.write(uploaded_file.read())
orig_path = temp_file.name
# Step 1: Read and preprocess image
image = cv2.imread(orig_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary_inv = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV)
kernel = np.ones((3, 3), np.uint8)
dilated = cv2.dilate(binary_inv, kernel, iterations=1)
# Save preprocessed image for OCR
dilated_path = orig_path.replace(".png", "_dilated.png")
cv2.imwrite(dilated_path, dilated)
loader = UnstructuredImageLoader(dilated_path)
documents = loader.load()
extracted_text = "\n".join([doc.page_content for doc in documents])
template = """
You are a helpful assistant. Here is a prescription text extracted from an image:
{prescription_text}
Please summarize the key medicine names and instructions in bullet points.
"""
prompt = PromptTemplate(input_variables=["prescription_text"], template=template)
model = HuggingFaceEndpoint(
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
provider="novita",
temperature=0.6,
max_new_tokens=300,
task="conversational"
)
chain = LLMChain(llm=model, prompt=prompt)
col1, col2 = st.columns([1, 2])
with col1:
st.image(dilated, caption="Preprocessed Prescription", channels="GRAY", use_container_width=True)
with col2:
st.success("✅ Prescription Uploaded & Preprocessed Successfully")
st.markdown("### 📜 Extracted Text")
st.code(extracted_text)
if st.button("🔍 Analyze Text"):
with st.spinner("Analyzing..."):
response = chain.run(prescription_text=extracted_text)
st.success(response)
# Cleanup temp files
os.remove(orig_path)
os.remove(dilated_path)
else:
st.image("https://cdn.dribbble.com/users/285475/screenshots/14711920/media/bd46dc2873f7099e4ef9fd53e6f7f1df.png", width=600)
st.markdown("<center><i>Upload a prescription image to begin analysis.</i></center>", unsafe_allow_html=True)
# st.markdown("### 🌐 Translated Text")
# st.code("पेरासिटामोल 500 मिलीग्राम\nभोजन के बाद दिन में दो बार 1 गोली लें", language='text')
# st.markdown("### ⏱️ Tablet Timing & Instructions")
# st.info("- Morning after breakfast\n- Night after dinner\n- Take with water\n- Do not exceed 2 tablets in 24 hours")
# st.markdown("### ⚠️ Possible Side Effects")
# st.warning("- Nausea\n- Dizziness\n- Liver damage (on overdose)")
# os.remove(temp_path)
# os.remove(orig_path)
# os.remove(dilated_path) |