Saicharan21 commited on
Commit
8aff1c1
Β·
verified Β·
1 Parent(s): 96a1142

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +147 -1
app.py CHANGED
@@ -707,6 +707,96 @@ def tgt_manual(t,p,h,pl,tm):
707
  risk=sum([float(t)>15,float(p)>2.0,float(h)>50,float(pl)<150])
708
  return "TAT:"+str(t)+" PF1.2:"+str(p)+chr(10)+"Hemo:"+str(h)+" Plt:"+str(pl)+chr(10)+"RESULT: "+("HIGH RISK" if risk>=3 else "MODERATE" if risk>=2 else "LOW RISK")
709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710
  with gr.Blocks(title="CardioLab AI - SJSU", css=CSS) as demo:
711
  gr.HTML(HEADER)
712
 
@@ -831,7 +921,63 @@ with gr.Blocks(title="CardioLab AI - SJSU", css=CSS) as demo:
831
  t5=gr.Number(label="Time min",value=40); out2=gr.Textbox(label="Result",lines=6)
832
  gr.Button("Analyze TGT",variant="primary").click(tgt_manual,inputs=[t1,t2,t3,t4,t5],outputs=out2)
833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
834
  gr.HTML("""<div style="text-align:center;padding:10px;border-top:1px solid #e5e7eb;background:#f9fafb;">
835
- <span style="color:#9ca3af;font-size:0.75em;">CardioLab AI v37 | SJSU Biomedical Engineering | Fine-tuned on 16 SJSU Papers | RAG + Custom Model | Inspired by <a href="https://github.com/snap-stanford/Biomni" style="color:#c1121f;">Biomni Stanford</a> | <a href="https://github.com/pranatechsol/Cardio-Lab-Ai" style="color:#0057a8;">GitHub</a> | Apache 2.0 | $0 Cost</span></div>""")
836
 
837
  demo.launch()
 
707
  risk=sum([float(t)>15,float(p)>2.0,float(h)>50,float(pl)<150])
708
  return "TAT:"+str(t)+" PF1.2:"+str(p)+chr(10)+"Hemo:"+str(h)+" Plt:"+str(pl)+chr(10)+"RESULT: "+("HIGH RISK" if risk>=3 else "MODERATE" if risk>=2 else "LOW RISK")
709
 
710
+
711
+ # ── PHASE D: PROTOCOL GENERATOR + REPORT WRITER + HYPOTHESIS ──────
712
+
713
+ def generate_protocol(experiment_type, specific_params=""):
714
+ if not GROQ_KEY: return "Error: Add GROQ_API_KEY to Space Settings."
715
+ try:
716
+ client = Groq(api_key=GROQ_KEY)
717
+ paper_context, _ = search_papers(experiment_type, n=4)
718
+ lab_context = {
719
+ "MCL": "Sylgard 184 PDMS 10:1 ratio 48hr cure. Tygon tubing. 70bpm 5L/min 80-120mmHg. Blood analog glycerin water.",
720
+ "PIV": "Green laser 532nm time-resolved. Seeding particles. Velocity 0.5-2.0 m/s normal. Shear below 5 Pa normal.",
721
+ "TGT": "Arduino Uno stepper motor 48V. 150mL fresh blood. Sample 0 20 40 60 min. Heska HT5 analyzer.",
722
+ "uPAD": "Whatman filter paper. Wax printer 120C baking. Picric acid alkaline solution. Jaffe reaction orange-red color.",
723
+ "FSI": "COMSOL Multiphysics ALE mesh. Blood 1060 kg/m3 0.0035 Pa.s. SJM bileaflet geometry.",
724
+ }
725
+ extra = next((v for k,v in lab_context.items() if k.lower() in experiment_type.lower()), "")
726
+ system = """You are CardioLab AI protocol generator for SJSU Biomedical Engineering.
727
+ Generate a COMPLETE detailed lab protocol. Include these sections:
728
+ 1. OBJECTIVE
729
+ 2. MATERIALS AND EQUIPMENT (with exact quantities)
730
+ 3. SAFETY CONSIDERATIONS
731
+ 4. STEP-BY-STEP PROCEDURE (numbered, detailed)
732
+ 5. DATA COLLECTION
733
+ 6. ANALYSIS METHOD
734
+ 7. EXPECTED RESULTS (with normal ranges)
735
+ 8. TROUBLESHOOTING
736
+ Use exact SJSU CardioLab values and equipment."""
737
+ user_msg = f"Generate complete protocol for: {experiment_type}"
738
+ if specific_params: user_msg += f"
739
+ Parameters: {specific_params}"
740
+ if extra: user_msg += f"
741
+ CardioLab context: {extra}"
742
+ if paper_context: user_msg += f"
743
+ From SJSU papers: {paper_context[:600]}"
744
+ resp = client.chat.completions.create(
745
+ model="llama-3.3-70b-versatile",
746
+ messages=[{"role":"system","content":system},{"role":"user","content":user_msg}],
747
+ max_tokens=1200)
748
+ return resp.choices[0].message.content
749
+ except Exception as e: return "Error: "+str(e)
750
+
751
+ def generate_report(data_description, experiment_type, results=""):
752
+ if not GROQ_KEY: return "Error: Add GROQ_API_KEY to Space Settings."
753
+ try:
754
+ client = Groq(api_key=GROQ_KEY)
755
+ paper_context, _ = search_papers(experiment_type, n=3)
756
+ system = """You are CardioLab AI report writer for SJSU Biomedical Engineering.
757
+ Generate a professional research report with these sections:
758
+ 1. ABSTRACT (150 words)
759
+ 2. INTRODUCTION (background and objectives)
760
+ 3. MATERIALS AND METHODS
761
+ 4. RESULTS AND DISCUSSION
762
+ 5. CONCLUSION
763
+ 6. RECOMMENDATIONS
764
+ 7. REFERENCES (cite SJSU CardioLab papers)
765
+ Use specific values. Write in professional academic style."""
766
+ user_msg = f"Write research report for: {experiment_type}"+chr(10)+f"Description: {data_description}"
767
+ if results: user_msg += chr(10)+f"Results: {results}"
768
+ if paper_context: user_msg += chr(10)+f"SJSU papers: {paper_context[:600]}"
769
+ resp = client.chat.completions.create(
770
+ model="llama-3.3-70b-versatile",
771
+ messages=[{"role":"system","content":system},{"role":"user","content":user_msg}],
772
+ max_tokens=1500)
773
+ return resp.choices[0].message.content
774
+ except Exception as e: return "Error: "+str(e)
775
+
776
+ def generate_hypothesis(research_area, current_findings=""):
777
+ if not GROQ_KEY: return "Error: Add GROQ_API_KEY to Space Settings."
778
+ try:
779
+ client = Groq(api_key=GROQ_KEY)
780
+ paper_context, _ = search_papers(research_area, n=3)
781
+ system = """You are CardioLab AI research assistant for SJSU Biomedical Engineering.
782
+ Generate 3 specific testable research hypotheses. For each:
783
+ - H0 (null hypothesis)
784
+ - H1 (alternative hypothesis)
785
+ - Scientific rationale
786
+ - Suggested experiment
787
+ - Expected outcome and measurable metrics
788
+ Base on SJSU CardioLab research."""
789
+ user_msg = f"Generate hypotheses for: {research_area}"
790
+ if current_findings: user_msg += chr(10)+f"Current findings: {current_findings}"
791
+ if paper_context: user_msg += chr(10)+f"SJSU papers: {paper_context[:500]}"
792
+ resp = client.chat.completions.create(
793
+ model="llama-3.3-70b-versatile",
794
+ messages=[{"role":"system","content":system},{"role":"user","content":user_msg}],
795
+ max_tokens=1000)
796
+ return resp.choices[0].message.content
797
+ except Exception as e: return "Error: "+str(e)
798
+
799
+
800
  with gr.Blocks(title="CardioLab AI - SJSU", css=CSS) as demo:
801
  gr.HTML(HEADER)
802
 
 
921
  t5=gr.Number(label="Time min",value=40); out2=gr.Textbox(label="Result",lines=6)
922
  gr.Button("Analyze TGT",variant="primary").click(tgt_manual,inputs=[t1,t2,t3,t4,t5],outputs=out2)
923
 
924
+ with gr.Tab("Protocol Generator"):
925
+ gr.Markdown("### Generate complete lab protocols from SJSU CardioLab knowledge")
926
+ with gr.Row():
927
+ with gr.Column(scale=1):
928
+ proto_type = gr.Dropdown(
929
+ choices=["MCL Setup","PIV Experiment","TGT Blood Testing",
930
+ "uPAD Fabrication","uPAD Creatinine Test",
931
+ "FSI COMSOL Simulation","Valve Testing"],
932
+ value="TGT Blood Testing", label="Experiment Type")
933
+ proto_params = gr.Textbox(
934
+ placeholder="e.g. 27mm SJM valve 70bpm porcine blood",
935
+ label="Specific Parameters", lines=2)
936
+ proto_btn = gr.Button("Generate Protocol", variant="primary")
937
+ with gr.Column(scale=2):
938
+ proto_output = gr.Textbox(label="Generated Protocol", lines=28, show_copy_button=True)
939
+ proto_btn.click(generate_protocol, inputs=[proto_type, proto_params], outputs=proto_output)
940
+
941
+ with gr.Tab("Report Writer"):
942
+ gr.Markdown("### Generate professional research reports from your data")
943
+ with gr.Row():
944
+ with gr.Column(scale=1):
945
+ report_exp = gr.Dropdown(
946
+ choices=["MCL PIV Flow Analysis","TGT Thrombogenicity Study",
947
+ "uPAD CKD Detection","FSI Simulation Study",
948
+ "Heart Valve Comparison"],
949
+ value="TGT Thrombogenicity Study", label="Study Type")
950
+ report_desc = gr.Textbox(
951
+ placeholder="Describe your experiment e.g. TGT with 27mm SJM bileaflet at 70bpm 150mL porcine blood",
952
+ label="Experiment Description", lines=3)
953
+ report_results = gr.Textbox(
954
+ placeholder="e.g. TAT=12.3 ng/mL PF1.2=2.8 Hemo=45 Plt=142",
955
+ label="Your Results", lines=2)
956
+ report_btn = gr.Button("Generate Report", variant="primary")
957
+ with gr.Column(scale=2):
958
+ report_output = gr.Textbox(label="Generated Report", lines=28, show_copy_button=True)
959
+ report_btn.click(generate_report, inputs=[report_desc, report_exp, report_results], outputs=report_output)
960
+
961
+ with gr.Tab("Hypothesis Generator"):
962
+ gr.Markdown("### Generate testable research hypotheses for CardioLab projects")
963
+ with gr.Row():
964
+ with gr.Column(scale=1):
965
+ hyp_area = gr.Dropdown(
966
+ choices=["Bileaflet MHV Thrombogenicity",
967
+ "uPAD CKD Detection Accuracy",
968
+ "PIV Flow Characterization",
969
+ "FSI Simulation Validation",
970
+ "Valve Design Comparison"],
971
+ value="Bileaflet MHV Thrombogenicity", label="Research Area")
972
+ hyp_findings = gr.Textbox(
973
+ placeholder="Current observations from your experiments",
974
+ label="Current Findings", lines=3)
975
+ hyp_btn = gr.Button("Generate Hypotheses", variant="primary")
976
+ with gr.Column(scale=2):
977
+ hyp_output = gr.Textbox(label="Research Hypotheses", lines=25, show_copy_button=True)
978
+ hyp_btn.click(generate_hypothesis, inputs=[hyp_area, hyp_findings], outputs=hyp_output)
979
+
980
  gr.HTML("""<div style="text-align:center;padding:10px;border-top:1px solid #e5e7eb;background:#f9fafb;">
981
+ <span style="color:#9ca3af;font-size:0.75em;">CardioLab AI v38 | SJSU Biomedical Engineering | Fine-tuned on 16 SJSU Papers | RAG + Custom Model | Inspired by <a href="https://github.com/snap-stanford/Biomni" style="color:#c1121f;">Biomni Stanford</a> | <a href="https://github.com/pranatechsol/Cardio-Lab-Ai" style="color:#0057a8;">GitHub</a> | Apache 2.0 | $0 Cost</span></div>""")
982
 
983
  demo.launch()