Ahmad-01 commited on
Commit
949fb80
Β·
verified Β·
1 Parent(s): 7a63289

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -5,28 +5,30 @@ from PIL import Image
5
  import io
6
  import os
7
 
8
- # βœ… Read the Groq API key from environment variable (set this in Hugging Face Secrets)
9
  print("ENVIRONMENT VARIABLES:", list(os.environ.keys()))
10
 
11
- GROQ_API_KEY = os.environ.get("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")
 
12
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
13
- MODEL_NAME = "llama-3.1-8b-instant" # You can change this to a different supported model if needed
14
 
15
- # Convert image to base64 (not sent, placeholder for future use or debugging)
16
  def image_to_base64(image: Image.Image) -> str:
17
  buffered = io.BytesIO()
18
  image.save(buffered, format="PNG")
19
  return base64.b64encode(buffered.getvalue()).decode()
20
 
21
- # Function to send defect info to Groq API and get analysis
22
  def analyze_defect(image, user_input):
23
  if image is None:
24
  return "⚠️ Please upload an image of the defective component."
25
 
26
  if not GROQ_API_KEY:
27
- return "❌ GROQ_API_KEY not found. Please set it in Hugging Face 'Secrets'."
28
 
29
  try:
 
30
  image_b64 = image_to_base64(image)
31
 
32
  system_prompt = (
@@ -62,12 +64,12 @@ def analyze_defect(image, user_input):
62
  reply = result['choices'][0]['message']['content']
63
  return reply.strip()
64
  else:
65
- return f"❌ Error: Groq API request failed with status code {response.status_code}."
66
 
67
  except Exception as e:
68
- return f"❌ An unexpected error occurred: {str(e)}"
69
 
70
- # Gradio UI definition
71
  with gr.Blocks() as demo:
72
  gr.Markdown("## πŸ› οΈ NDT Defect Analysis Assistant (Powered by Groq AI)")
73
 
@@ -80,6 +82,5 @@ with gr.Blocks() as demo:
80
 
81
  analyze_button.click(fn=analyze_defect, inputs=[image_input, user_input], outputs=output)
82
 
83
- # Launch the app
84
  demo.launch()
85
 
 
5
  import io
6
  import os
7
 
8
+ # πŸ” Debug print (optional - remove later)
9
  print("ENVIRONMENT VARIABLES:", list(os.environ.keys()))
10
 
11
+ # βœ… Correct way to load secret
12
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
13
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
14
+ MODEL_NAME = "llama-3.1-8b-instant"
15
 
16
+ # Convert image to base64 (not actually used in API, placeholder)
17
  def image_to_base64(image: Image.Image) -> str:
18
  buffered = io.BytesIO()
19
  image.save(buffered, format="PNG")
20
  return base64.b64encode(buffered.getvalue()).decode()
21
 
22
+ # Main function to analyze defect
23
  def analyze_defect(image, user_input):
24
  if image is None:
25
  return "⚠️ Please upload an image of the defective component."
26
 
27
  if not GROQ_API_KEY:
28
+ return "❌ Environment variable 'GROQ_API_KEY' is set in the system but empty."
29
 
30
  try:
31
+ # Optional: Keep for future image analysis
32
  image_b64 = image_to_base64(image)
33
 
34
  system_prompt = (
 
64
  reply = result['choices'][0]['message']['content']
65
  return reply.strip()
66
  else:
67
+ return f"❌ Groq API request failed. Status code: {response.status_code}"
68
 
69
  except Exception as e:
70
+ return f"❌ An error occurred: {str(e)}"
71
 
72
+ # Gradio Interface
73
  with gr.Blocks() as demo:
74
  gr.Markdown("## πŸ› οΈ NDT Defect Analysis Assistant (Powered by Groq AI)")
75
 
 
82
 
83
  analyze_button.click(fn=analyze_defect, inputs=[image_input, user_input], outputs=output)
84
 
 
85
  demo.launch()
86