Meet Radadiya commited on
Commit
a5bcd30
·
1 Parent(s): 9e4f358

Fine Tune Output!

Browse files
Files changed (1) hide show
  1. app.py +8 -101
app.py CHANGED
@@ -14,7 +14,6 @@ import re
14
  from reportlab.lib import colors
15
  import random
16
  import streamlit.components.v1 as components
17
- from openai import OpenAI
18
 
19
  # ======================
20
  # CONFIGURATION SETTINGS
@@ -270,20 +269,6 @@ def initialize_groq_client():
270
 
271
  return Groq(api_key=api_key)
272
 
273
- def initialize_openai_client():
274
- """Create and validate OpenAI API client"""
275
- load_dotenv()
276
- api_key = os.getenv("OPENAI_API_KEY")
277
-
278
- # Check if API key is in session state (from user input)
279
- if 'openai_api_key' in st.session_state and st.session_state.openai_api_key:
280
- api_key = st.session_state.openai_api_key
281
-
282
- if not api_key:
283
- # Return None if no API key - we'll handle this in the UI
284
- return None
285
-
286
- return OpenAI(api_key=api_key)
287
 
288
  def encode_logo(image_path):
289
  """Encode logo image to base64"""
@@ -594,81 +579,13 @@ Format each section with appropriate headings and use bullet points for lists. B
594
  st.error(f"Groq API error: {str(e)}")
595
  return None
596
 
597
- def generate_radiology_report_openai(uploaded_file, client):
598
- """Generate AI-powered radiology analysis using OpenAI API"""
599
- base64_image, img_format = process_image_data(uploaded_file)
600
-
601
- if not base64_image:
602
- return None
603
-
604
- try:
605
- with st.spinner("Analyzing image with OpenAI..."):
606
- # Add progress bar for visual feedback
607
- progress_bar = st.progress(0)
608
- for i in range(100):
609
- # Update progress bar
610
- progress_bar.progress(i + 1)
611
- import time
612
- time.sleep(0.025) # Simulate processing time
613
-
614
- # Updated prompt to request the detailed, structured format
615
- response = client.chat.completions.create(
616
- model="gpt-4-vision-preview", # Use OpenAI's vision model
617
- messages=[
618
- {
619
- "role": "user",
620
- "content": [
621
- {
622
- "type": "text",
623
- "text": (
624
- "As a radiologist, analyze this medical image and provide a comprehensive report with the following structure:\n\n"
625
- "1. DIAGNOSIS: Clear statement of primary diagnosis\n\n"
626
- "2. ETIOLOGY: List possible causes of the identified condition\n\n"
627
- "3. RISK FACTORS: Bullet-point list of risk factors associated with the condition\n\n"
628
- "4. PATHOPHYSIOLOGY: Brief explanation of the disease mechanism\n\n"
629
- "5. CLINICAL FEATURES: Overview of typical clinical presentation\n\n"
630
- "6. SIGNS AND SYMPTOMS: Bullet-point list of common signs and symptoms\n\n"
631
- "7. INVESTIGATIONS: Diagnostic tests typically used for confirmation\n\n"
632
- "8. MANAGEMENT: Divided into three parts:\n"
633
- " - Initial Stabilization\n"
634
- " - Medical Management\n"
635
- " - Surgical Management (if applicable)\n\n"
636
- "9. PROGNOSIS: Expected outcomes and factors affecting prognosis\n\n"
637
- "Format each section with appropriate headings and use bullet points for lists. If you identify abnormal findings, make sure to highlight these clearly."
638
- )
639
- },
640
- {
641
- "type": "image_url",
642
- "image_url": {
643
- "url": f"data:image/{img_format.lower()};base64,{base64_image}"
644
- }
645
- }
646
- ]
647
- }
648
- ],
649
- max_tokens=3000 # Increased token limit for more detailed response
650
- )
651
- return response.choices[0].message.content
652
- except Exception as e:
653
- st.error(f"OpenAI API error: {str(e)}")
654
- return None
655
-
656
  def generate_radiology_report(uploaded_file, api_choice='groq'):
657
- """Generate report using the selected API"""
658
- if api_choice == 'groq':
659
- client = initialize_groq_client()
660
- if client:
661
- return generate_radiology_report_groq(uploaded_file, client)
662
- else:
663
- st.error("Failed to initialize Groq client. Please check your API key.")
664
- return None
665
- else: # OpenAI
666
- client = initialize_openai_client()
667
- if client:
668
- return generate_radiology_report_openai(uploaded_file, client)
669
- else:
670
- st.error("OpenAI API key is required. Please provide your API key.")
671
- return None
672
 
673
  # ======================
674
  # UI COMPONENTS
@@ -779,17 +696,7 @@ def render_sidebar():
779
  options=["Groq"],
780
  index=0, # Default to Groq
781
  )
782
-
783
- # If OpenAI is selected, show API key input
784
- # if api_provider == "OpenAI":
785
- # openai_key = st.text_input(
786
- # "OpenAI API Key",
787
- # type="password",
788
- # help="Enter your OpenAI API key",
789
- # value=st.session_state.get('openai_api_key', '')
790
- # )
791
- # if openai_key:
792
- # st.session_state.openai_api_key = openai_key
793
 
794
  st.markdown('</div>', unsafe_allow_html=True)
795
 
@@ -830,7 +737,7 @@ def render_sidebar():
830
  if st.button("▶️ Initiate Analysis", use_container_width=True):
831
  with st.spinner("Processing image..."):
832
  # Use the selected API provider
833
- api_choice = 'groq' if api_provider == "Groq" else 'openai'
834
  report = generate_radiology_report(uploaded_file, api_choice)
835
 
836
  if report:
 
14
  from reportlab.lib import colors
15
  import random
16
  import streamlit.components.v1 as components
 
17
 
18
  # ======================
19
  # CONFIGURATION SETTINGS
 
269
 
270
  return Groq(api_key=api_key)
271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
  def encode_logo(image_path):
274
  """Encode logo image to base64"""
 
579
  st.error(f"Groq API error: {str(e)}")
580
  return None
581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582
  def generate_radiology_report(uploaded_file, api_choice='groq'):
583
+ client = initialize_groq_client()
584
+ if client:
585
+ return generate_radiology_report_groq(uploaded_file, client)
586
+ else:
587
+ st.error("Failed to initialize Groq client. Please check your API key.")
588
+ return None
 
 
 
 
 
 
 
 
 
589
 
590
  # ======================
591
  # UI COMPONENTS
 
696
  options=["Groq"],
697
  index=0, # Default to Groq
698
  )
699
+
 
 
 
 
 
 
 
 
 
 
700
 
701
  st.markdown('</div>', unsafe_allow_html=True)
702
 
 
737
  if st.button("▶️ Initiate Analysis", use_container_width=True):
738
  with st.spinner("Processing image..."):
739
  # Use the selected API provider
740
+ api_choice = 'groq' if api_provider == "Groq" else None
741
  report = generate_radiology_report(uploaded_file, api_choice)
742
 
743
  if report: