Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,28 +5,30 @@ from PIL import Image
|
|
| 5 |
import io
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
print("ENVIRONMENT VARIABLES:", list(os.environ.keys()))
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
-
MODEL_NAME = "llama-3.1-8b-instant"
|
| 14 |
|
| 15 |
-
# Convert image to base64 (not
|
| 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 |
-
#
|
| 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 "β
|
| 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"β
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
-
return f"β An
|
| 69 |
|
| 70 |
-
# Gradio
|
| 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 |
|