MalikShehram commited on
Commit
c5ca8bb
·
verified ·
1 Parent(s): b822cb2

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +13 -14
backend.py CHANGED
@@ -18,25 +18,22 @@ client = Groq()
18
 
19
  def get_spectrogram_base64(audio_path):
20
  """
21
- Generates a Mel-Spectrogram and converts it to a Base64 image string
22
- so that Llama Vision can 'look' at it.
23
  """
24
  try:
25
  y, sr = librosa.load(audio_path, sr=None)
26
- fig, ax = plt.subplots(figsize=(8, 4))
27
 
28
- # Generate the Spectrogram (Focusing on heart frequencies up to 2000Hz)
29
  S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=2000)
30
  S_dB = librosa.power_to_db(S, ref=np.max)
31
  librosa.display.specshow(S_dB, sr=sr, fmax=2000, ax=ax, cmap='magma')
32
 
33
- # Save the plot to an in-memory buffer instead of a file
34
  buf = io.BytesIO()
35
- plt.savefig(buf, format='png', bbox_inches='tight')
36
- plt.close(fig) # Prevent memory leaks
 
37
  buf.seek(0)
38
 
39
- # Convert to Base64
40
  base64_image = base64.b64encode(buf.read()).decode('utf-8')
41
  return base64_image
42
  except Exception as e:
@@ -48,14 +45,14 @@ def generate_medical_advice_from_vision(base64_img):
48
  Uses Llama 3.2 Vision (via Groq) to look at the Spectrogram and diagnose it.
49
  """
50
  if not base64_img:
51
- return "Error: Could not process the audio into a visual spectrogram for the AI."
52
 
53
  prompt = """
54
  You are an AI medical assistant specializing in cardiology. Look closely at this Mel-Spectrogram of a patient's Phonocardiogram (heart sound).
55
 
56
  Based on the visual patterns in this spectrogram:
57
  1. Does this look Normal or Abnormal?
58
- 2. What specific cardiovascular disease might this indicate (e.g., Aortic Stenosis, Mitral Regurgitation, Normal)? Give your best medical estimation.
59
  3. Recommend general lifestyle or exercise advice based on your estimation.
60
  4. Mention potential medication types usually associated with this.
61
 
@@ -64,7 +61,7 @@ def generate_medical_advice_from_vision(base64_img):
64
 
65
  try:
66
  response = client.chat.completions.create(
67
- model="llama-3.2-11b-vision-preview", # Groq's Llama Vision Model
68
  messages=[
69
  {
70
  "role": "user",
@@ -79,13 +76,15 @@ def generate_medical_advice_from_vision(base64_img):
79
  ]
80
  }
81
  ],
82
- temperature=0.2, # Low temperature to keep the AI focused
83
  max_tokens=300
84
  )
85
  return response.choices[0].message.content
86
  except Exception as e:
87
- print(f"Groq Vision API Error: {e}")
88
- return "Failed to analyze the image. Please ensure your Groq API key is valid and the Vision model is currently online."
 
 
89
 
90
  def text_to_speech(text):
91
  """
 
18
 
19
  def get_spectrogram_base64(audio_path):
20
  """
21
+ Generates a Mel-Spectrogram, aggressively compresses it, and converts it to Base64.
 
22
  """
23
  try:
24
  y, sr = librosa.load(audio_path, sr=None)
25
+ fig, ax = plt.subplots(figsize=(6, 3)) # Slightly smaller dimensions
26
 
 
27
  S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=2000)
28
  S_dB = librosa.power_to_db(S, ref=np.max)
29
  librosa.display.specshow(S_dB, sr=sr, fmax=2000, ax=ax, cmap='magma')
30
 
 
31
  buf = io.BytesIO()
32
+ # dpi=72 ensures the image file size is extremely small and well under Groq's 4MB limit
33
+ plt.savefig(buf, format='png', bbox_inches='tight', dpi=72)
34
+ plt.close(fig)
35
  buf.seek(0)
36
 
 
37
  base64_image = base64.b64encode(buf.read()).decode('utf-8')
38
  return base64_image
39
  except Exception as e:
 
45
  Uses Llama 3.2 Vision (via Groq) to look at the Spectrogram and diagnose it.
46
  """
47
  if not base64_img:
48
+ return "Error: Could not process the audio into a visual spectrogram."
49
 
50
  prompt = """
51
  You are an AI medical assistant specializing in cardiology. Look closely at this Mel-Spectrogram of a patient's Phonocardiogram (heart sound).
52
 
53
  Based on the visual patterns in this spectrogram:
54
  1. Does this look Normal or Abnormal?
55
+ 2. What specific cardiovascular disease might this indicate (e.g., Aortic Stenosis, Mitral Regurgitation, Normal)?
56
  3. Recommend general lifestyle or exercise advice based on your estimation.
57
  4. Mention potential medication types usually associated with this.
58
 
 
61
 
62
  try:
63
  response = client.chat.completions.create(
64
+ model="llama-3.2-90b-vision-preview", # Upgraded to the larger, more stable vision model
65
  messages=[
66
  {
67
  "role": "user",
 
76
  ]
77
  }
78
  ],
79
+ temperature=0.2,
80
  max_tokens=300
81
  )
82
  return response.choices[0].message.content
83
  except Exception as e:
84
+ # Instead of a generic message, we now capture the exact error from Groq
85
+ actual_error = str(e)
86
+ print(f"Groq Vision API Error: {actual_error}")
87
+ return f"API Error: {actual_error}\n\n(If you see a 404 or Model Not Found error, Groq might have temporarily rotated their vision models.)"
88
 
89
  def text_to_speech(text):
90
  """