Spaces:
Runtime error
Runtime error
Upload app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline# Load your model from Hugging Face HubMODEL_ID = "Muhammadidrees/MedicalInsights"tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained(MODEL_ID,device_map="auto",trust_remote_code=True,local_files_only=False, # prevent local override)pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)# Function to build structured input and query the LLMdef analyze(albumin, creatinine, glucose, crp, mcv, rdw, alp,wbc, lymph, age, gender, height, weight):# System-style instructionsystem_prompt = ("You are an advanced AI medical assistant. ""Analyze the patientβs biomarkers and demographics. ""Provide a structured assessment including: ""patient_profile, lab_results, risk_assessment, clinical_impression, recommendations. ")# Construct patient profile inputpatient_input = f"""Patient Profile:- Age: {age}- Gender: {gender}- Height: {height} cm- Weight: {weight} kgLab Values:- Albumin: {albumin} g/dL- Creatinine: {creatinine} mg/dL- Glucose: {glucose} mg/dL- C-Reactive Protein: {crp} mg/L- Mean Cell Volume: {mcv} fL- Red Cell Distribution Width: {rdw} %- Alkaline Phosphatase: {alp} U/L- White Blood Cell Count: {wbc} K/uL- Lymphocyte Percentage: {lymph} %"""prompt = system_prompt + "\n" + patient_input# Call LLMresult = pipe(prompt, max_new_tokens=400, do_sample=True, temperature=0.6)return result[0]["generated_text"]# Build Gradio UIwith gr.Blocks() as demo:gr.Markdown("## π§ͺ Medical Insights AI β Enter Patient Data")with gr.Row():albumin = gr.Number(label="Albumin (g/dL)")wbc = gr.Number(label="White Blood Cell Count (K/uL)")with gr.Row():creatinine = gr.Number(label="Creatinine (mg/dL)")lymph = gr.Number(label="Lymphocyte Percentage (%)")with gr.Row():glucose = gr.Number(label="Glucose (mg/dL)")age = gr.Number(label="Age (years)")with gr.Row():crp = gr.Number(label="C-Reactive Protein (mg/L)")gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")with gr.Row():mcv = gr.Number(label="Mean Cell Volume (fL)")height = gr.Number(label="Height (cm)")with gr.Row():rdw = gr.Number(label="Red Cell Distribution Width (%)")weight = gr.Number(label="Weight (kg)")with gr.Row():alp = gr.Number(label="Alkaline Phosphatase (U/L)")analyze_btn = gr.Button("π Analyze")output = gr.Textbox(label="AI Medical Assessment", lines=12)analyze_btn.click(fn=analyze,inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,wbc, lymph, age, gender, height, weight],outputs=output)demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline# Load your model from Hugging Face HubMODEL_ID = "Muhammadidrees/MedicalInsights"tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map="auto")pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)# Function to build structured input and query the LLMdef analyze(albumin, creatinine, glucose, crp, mcv, rdw, alp,wbc, lymph, age, gender, height, weight, bmi):# System-style instructionsystem_prompt = ("You are an advanced AI medical assistant. ""Analyze the patientβs biomarkers and demographics. ""Provide a structured assessment including: ""patient_profile, lab_results, risk_assessment, clinical_impression, recommendations. ")# Construct patient profile inputpatient_input = f"""Patient Profile:- Age: {age}- Gender: {gender}- Height: {height} cm- Weight: {weight} kg- BMI: {bmi}Lab Values:- Albumin: {albumin} g/dL- Creatinine: {creatinine} mg/dL- Glucose: {glucose} mg/dL- C-Reactive Protein: {crp} mg/L- Mean Cell Volume: {mcv} fL- Red Cell Distribution Width: {rdw} %- Alkaline Phosphatase: {alp} U/L- White Blood Cell Count: {wbc} K/uL- Lymphocyte Percentage: {lymph} %"""prompt = system_prompt + "\n" + patient_input# Call LLMresult = pipe(prompt, max_new_tokens=400, do_sample=True, temperature=0.6)return result[0]["generated_text"]# Build Gradio UIwith gr.Blocks() as demo:gr.Markdown("## π§ͺ Medical Insights AI β Enter Patient Data")with gr.Row():albumin = gr.Number(label="Albumin (g/dL)")wbc = gr.Number(label="White Blood Cell Count (K/uL)")with gr.Row():creatinine = gr.Number(label="Creatinine (mg/dL)")lymph = gr.Number(label="Lymphocyte Percentage (%)")with gr.Row():glucose = gr.Number(label="Glucose (mg/dL)")age = gr.Number(label="Age (years)")with gr.Row():crp = gr.Number(label="C-Reactive Protein (mg/L)")gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")with gr.Row():mcv = gr.Number(label="Mean Cell Volume (fL)")height = gr.Number(label="Height (cm)")with gr.Row():rdw = gr.Number(label="Red Cell Distribution Width (%)")weight = gr.Number(label="Weight (kg)")with gr.Row():alp = gr.Number(label="Alkaline Phosphatase (U/L)")bmi = gr.Number(label="BMI")analyze_btn = gr.Button("π Analyze")output = gr.Textbox(label="AI Medical Assessment", lines=12)analyze_btn.click(fn=analyze,inputs=[albumin, creatinine, glucose, crp, mcv, rdw, alp,wbc, lymph, age, gender, height, weight, bmi],outputs=output)demo.launch()
app.py+94-98