Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import pytesseract
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# Load the model
|
9 |
+
model = pipeline("text2text-generation", model="google/paligemma-3b-mix-224")
|
10 |
+
|
11 |
+
# Function to extract text from image using OCR
|
12 |
+
def extract_text_from_image(image_file):
|
13 |
+
image = Image.open(image_file)
|
14 |
+
img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
15 |
+
text = pytesseract.image_to_string(img_cv)
|
16 |
+
return text
|
17 |
+
|
18 |
+
# Function to get AI interpretation of the prescription
|
19 |
+
def interpret_prescription(text):
|
20 |
+
response = model(text)
|
21 |
+
return response[0]['generated_text'].strip()
|
22 |
+
|
23 |
+
# Set Streamlit page configuration
|
24 |
+
st.set_page_config(
|
25 |
+
page_title="Prescription Reader",
|
26 |
+
page_icon="π",
|
27 |
+
layout="centered",
|
28 |
+
)
|
29 |
+
|
30 |
+
# Header
|
31 |
+
st.title("Doctor's Prescription Reader π")
|
32 |
+
|
33 |
+
# Upload prescription image
|
34 |
+
uploaded_file = st.file_uploader("Upload Prescription Image", type=["jpg", "jpeg", "png"])
|
35 |
+
|
36 |
+
if uploaded_file is not None:
|
37 |
+
# Display uploaded image
|
38 |
+
st.image(uploaded_file, caption="Uploaded Prescription", use_column_width=True)
|
39 |
+
|
40 |
+
with st.spinner("Extracting text from prescription..."):
|
41 |
+
# Extract text from image using OCR
|
42 |
+
extracted_text = extract_text_from_image(uploaded_file)
|
43 |
+
st.subheader("Extracted Text from Prescription:")
|
44 |
+
st.text(extracted_text)
|
45 |
+
|
46 |
+
if extracted_text:
|
47 |
+
# Interpret extracted text using the model
|
48 |
+
with st.spinner("Interpreting the prescription..."):
|
49 |
+
ai_response = interpret_prescription(extracted_text)
|
50 |
+
st.subheader("AI Interpretation:")
|
51 |
+
st.text(ai_response)
|