| | import streamlit as st
|
| | import torch
|
| | from transformers import AutoTokenizer, AutoModelForCausalLM, AutoProcessor
|
| | from peft import PeftModel
|
| | from PIL import Image
|
| | import io
|
| |
|
| |
|
| | st.set_page_config(
|
| | page_title="ViTCM_LLM Tongue Diagnosis",
|
| | page_icon="๐ผ๏ธ",
|
| | layout="wide"
|
| | )
|
| |
|
| |
|
| | st.title("๐ผ๏ธ ViTCM_LLM Tongue Diagnosis")
|
| | st.markdown("**ViTCM_LLM - Traditional Chinese Medicine Tongue Diagnosis Model**")
|
| |
|
| |
|
| | @st.cache_resource
|
| | def load_model():
|
| | """Load the ViTCM_LLM model for TCM tongue diagnosis."""
|
| | try:
|
| |
|
| | tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct")
|
| | processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct")
|
| |
|
| |
|
| | base_model = AutoModelForCausalLM.from_pretrained(
|
| | "Qwen/Qwen2.5-VL-32B-Instruct",
|
| | torch_dtype=torch.float16,
|
| | device_map="auto"
|
| | )
|
| |
|
| |
|
| | model = PeftModel.from_pretrained(base_model, "Mark-CHAE/shezhen")
|
| |
|
| | return model, tokenizer, processor
|
| | except Exception as e:
|
| | st.error(f"Model loading failed: {e}")
|
| | return None, None, None
|
| |
|
| |
|
| | with st.sidebar:
|
| | st.header("โ๏ธ Settings")
|
| |
|
| |
|
| | max_length = st.slider("Max tokens", 100, 1024, 512)
|
| | temperature = st.slider("Temperature", 0.1, 2.0, 0.7, 0.1)
|
| | top_p = st.slider("Top-p", 0.1, 1.0, 0.9, 0.05)
|
| |
|
| |
|
| | if st.button("๐ Load Model", type="primary"):
|
| | with st.spinner("Loading ViTCM_LLM model..."):
|
| | model, tokenizer, processor = load_model()
|
| | if model is not None:
|
| | st.session_state.model = model
|
| | st.session_state.tokenizer = tokenizer
|
| | st.session_state.processor = processor
|
| | st.session_state.model_loaded = True
|
| | st.success("โ
ViTCM_LLM model loaded successfully!")
|
| |
|
| |
|
| | if not st.session_state.get('model_loaded', False):
|
| | st.info("๐ Click 'Load Model' button in the sidebar to start tongue diagnosis.")
|
| | st.stop()
|
| |
|
| |
|
| | st.header("๐ธ Tongue Image Upload")
|
| | uploaded_file = st.file_uploader(
|
| | "Upload a tongue image for TCM diagnosis",
|
| | type=['png', 'jpg', 'jpeg']
|
| | )
|
| |
|
| | if uploaded_file is not None:
|
| |
|
| | image = Image.open(uploaded_file)
|
| | st.image(image, caption="Uploaded tongue image", use_column_width=True)
|
| |
|
| |
|
| | st.header("โ Tongue Diagnosis Question")
|
| | question = st.text_area(
|
| | "Ask a question about the tongue image for TCM diagnosis",
|
| | placeholder="e.g., ๆ นๆฎๅพ็ๅคๆญ่่ฏๅ
ๅฎน",
|
| | height=100
|
| | )
|
| |
|
| |
|
| | if st.button("๐ Analyze Tongue", type="primary") and question.strip():
|
| | with st.spinner("Analyzing tongue for TCM diagnosis..."):
|
| | try:
|
| |
|
| | prompt = f"<|im_start|>user\n<image>\n{question}<|im_end|>\n<|im_start|>assistant\n"
|
| |
|
| |
|
| | inputs = st.session_state.processor(
|
| | text=prompt,
|
| | images=image,
|
| | return_tensors="pt"
|
| | )
|
| |
|
| |
|
| | with torch.no_grad():
|
| | outputs = st.session_state.model.generate(
|
| | **inputs,
|
| | max_length=max_length,
|
| | temperature=temperature,
|
| | top_p=top_p,
|
| | do_sample=True,
|
| | pad_token_id=st.session_state.tokenizer.eos_token_id
|
| | )
|
| |
|
| |
|
| | response = st.session_state.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| | answer = response.split("<|im_start|>assistant")[-1].strip()
|
| |
|
| |
|
| | st.header("๐ก TCM Tongue Diagnosis")
|
| | st.markdown(f"**Question:** {question}")
|
| | st.markdown(f"**Diagnosis:** {answer}")
|
| |
|
| | except Exception as e:
|
| | st.error(f"Error occurred during tongue analysis: {e}")
|
| |
|
| |
|
| | with st.expander("๐ Tongue Diagnosis Examples"):
|
| | st.markdown("""
|
| | ### Tongue Diagnosis Questions:
|
| | - ๆ นๆฎๅพ็ๅคๆญ่่ฏๅ
ๅฎน
|
| | - ๅๆ่ๅคด็้ข่ฒๅๅฝข็ถ
|
| | - ๅคๆญ่่็ๅ่ๅ้ข่ฒ
|
| | - ๅๆ่ๅคด็่ฃ็บนๅๆ็น
|
| | - ่ฏไผฐ่ๅคด็ๆดไฝๅฅๅบท็ถๅต
|
| | """)
|
| |
|
| |
|
| | with st.expander("โน๏ธ Model Information"):
|
| | st.markdown("""
|
| | ### ViTCM_LLM - Traditional Chinese Medicine Tongue Diagnosis Model
|
| |
|
| | - **Base Model**: Qwen/Qwen2.5-VL-32B-Instruct
|
| | - **Adapter**: Mark-CHAE/shezhen (ViTCM_LLM)
|
| | - **Language**: Chinese
|
| | - **License**: Apache-2.0
|
| | - **Specialization**: Traditional Chinese Medicine Tongue Diagnosis
|
| | """)
|
| |
|
| | st.markdown("---")
|
| | st.markdown("**ViTCM_LLM Tongue Diagnosis** | Powered by Qwen2.5-VL-32B-Instruct") |