vikaso001/phi2-rag-api
0
1import streamlit as st2from PIL import Image3import tempfile4from preprocessing.process import modify_image5from model.loader import ImageProcessor6from text_classifier.classifier import extract_medical_info, extract_and_save_medical_info7from search.extractor import get_medication_details8import os9 10# โ
Must be the first Streamlit command11st.set_page_config(page_title="Medical Image Analyzer", page_icon="๐", layout="wide")12 13# Title14st.title("Medical Image Text Analyzer")15 16# Image upload widget17uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg", "gif", "webp"])18 19# If an image is uploaded20if uploaded_image:21 # Show the uploaded image22 image = Image.open(uploaded_image)23 st.image(image, caption="Uploaded Image", use_column_width=True)24 25 # Process the image when the button is clicked26 if st.button("Analyze Image"):27 try:28 # Save the image temporarily to disk29 with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:30 image.save(temp_file.name)31 32 # Image processing (preprocessing steps: grayscale, sharpen, enhance contrast)33 processed_image = modify_image(temp_file.name)34 35 if processed_image is not None:36 st.image(processed_image, caption="Processed Image", use_column_width=True)37 38 # Use the loader to analyze the image and get the markdown content39 processor = ImageProcessor()40 markdown_result = processor.analyze_image(image_path=temp_file.name)41 42 # Extract the text data from the markdown result43 medical_info = extract_medical_info(markdown_result)44 45 # Optional: Save to file46 extract_and_save_medical_info(markdown_result)47 48 # Display the extracted medical information49 st.subheader("Extracted Medical Information")50 if isinstance(medical_info, dict):51 st.json(medical_info)52 53 # Get medication details (with fuzzy matching)54 medications = medical_info.get("medications", [])55 medication_details_df = get_medication_details(medications)56 57 58 # Display the extracted medical information59 st.subheader("Extracted Medical Information")60 if isinstance(medical_info, dict):61 st.json(medical_info)62 63 # Get medication details (with fuzzy matching)64 medications = medical_info.get("medications", [])65 medication_details_df = get_medication_details(medications)66 67 # Display the medication details as a table68 st.subheader("Medication Details")69 70 # Loop over each medication and render it in a styled box71 for _, row in medication_details_df.iterrows():72 st.markdown(73 f"""74 <div style="color:black; border: 1px solid #ccc; border-radius: 10px; padding: 15px; margin-bottom: 20px; background-color: #f9f9f9;">75 <h4 style="color: #2c3e50;">๐ฉบ {row['Name']}</h4>76 <p style="color: #2c3e50;"><strong>Contains:</strong> {row['Contains']}</p>77 <p style="color: #2c3e50;"><strong>How to Use:</strong> {row['How to Use']}</p>78 <p style="color: #2c3e50;"><strong>Advice:</strong><br>{row['Advice']}</p>79 <p style="color: #2c3e50;"><a href="{row['Link']}" target="_blank">๐ More Info</a></p>80 </div>81 """,82 unsafe_allow_html=True83 )84 85 else:86 st.error("Failed to extract medical information.")87 # โ
Cleanup88 if os.path.exists(temp_file.name):89 os.remove(temp_file.name)90 except Exception as e:91 st.error(f"Error analyzing image: {str(e)}")92 93 