Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,16 @@ from torchvision import models
|
|
| 4 |
from PIL import Image
|
| 5 |
import pandas as pd
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# -------------------------
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# -------------------------
|
| 11 |
weights = models.MobileNet_V2_Weights.DEFAULT
|
| 12 |
model = models.mobilenet_v2(weights=weights)
|
|
@@ -18,7 +25,23 @@ labels = weights.meta["categories"]
|
|
| 18 |
history = []
|
| 19 |
|
| 20 |
# -------------------------
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# -------------------------
|
| 23 |
def analyze(image):
|
| 24 |
img = transform(image).unsqueeze(0)
|
|
@@ -38,8 +61,8 @@ def analyze(image):
|
|
| 38 |
|
| 39 |
df = pd.DataFrame(results)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
explanation =
|
| 43 |
|
| 44 |
# Chart
|
| 45 |
plt.figure()
|
|
@@ -57,20 +80,30 @@ def analyze(image):
|
|
| 57 |
return df, explanation, chart_path, hist_df
|
| 58 |
|
| 59 |
# -------------------------
|
| 60 |
-
# UI
|
| 61 |
# -------------------------
|
| 62 |
-
with gr.Blocks() as demo:
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
btn.click(analyze, inputs=img, outputs=[output, explanation, chart, history_table])
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
demo.launch()
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import pandas as pd
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
+
import os
|
| 8 |
+
from openai import OpenAI
|
| 9 |
|
| 10 |
# -------------------------
|
| 11 |
+
# OPENAI SETUP
|
| 12 |
+
# -------------------------
|
| 13 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 14 |
+
|
| 15 |
+
# -------------------------
|
| 16 |
+
# MODEL (LIGHTWEIGHT)
|
| 17 |
# -------------------------
|
| 18 |
weights = models.MobileNet_V2_Weights.DEFAULT
|
| 19 |
model = models.mobilenet_v2(weights=weights)
|
|
|
|
| 25 |
history = []
|
| 26 |
|
| 27 |
# -------------------------
|
| 28 |
+
# CHATGPT EXPLANATION
|
| 29 |
+
# -------------------------
|
| 30 |
+
def get_ai_explanation(label):
|
| 31 |
+
try:
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="gpt-4o-mini",
|
| 34 |
+
messages=[
|
| 35 |
+
{"role": "system", "content": "You are an AI expert explaining image classification results."},
|
| 36 |
+
{"role": "user", "content": f"Explain in simple terms why this image might be classified as {label}."}
|
| 37 |
+
]
|
| 38 |
+
)
|
| 39 |
+
return response.choices[0].message.content
|
| 40 |
+
except:
|
| 41 |
+
return "AI explanation unavailable. Check API key."
|
| 42 |
+
|
| 43 |
+
# -------------------------
|
| 44 |
+
# MAIN FUNCTION
|
| 45 |
# -------------------------
|
| 46 |
def analyze(image):
|
| 47 |
img = transform(image).unsqueeze(0)
|
|
|
|
| 61 |
|
| 62 |
df = pd.DataFrame(results)
|
| 63 |
|
| 64 |
+
# AI Explanation
|
| 65 |
+
explanation = get_ai_explanation(results[0]["label"])
|
| 66 |
|
| 67 |
# Chart
|
| 68 |
plt.figure()
|
|
|
|
| 80 |
return df, explanation, chart_path, hist_df
|
| 81 |
|
| 82 |
# -------------------------
|
| 83 |
+
# UI DESIGN
|
| 84 |
# -------------------------
|
| 85 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 86 |
+
|
| 87 |
+
gr.Markdown("""
|
| 88 |
+
# π§ AI Vision Pro π
|
| 89 |
+
### Real-time Image Recognition + ChatGPT Explanation
|
| 90 |
+
""")
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
img = gr.Image(source="webcam", type="pil", label="π· Capture / Upload Image")
|
| 95 |
+
btn = gr.Button("π Analyze", variant="primary")
|
| 96 |
|
| 97 |
+
with gr.Column():
|
| 98 |
+
output = gr.Dataframe(label="π Predictions")
|
| 99 |
+
explanation = gr.Textbox(label="π§ AI Explanation", lines=6)
|
| 100 |
+
|
| 101 |
+
chart = gr.Image(label="π Confidence Chart")
|
| 102 |
+
history_table = gr.Dataframe(label="ποΈ History")
|
| 103 |
|
| 104 |
btn.click(analyze, inputs=img, outputs=[output, explanation, chart, history_table])
|
| 105 |
|
| 106 |
+
gr.Markdown("---")
|
| 107 |
+
gr.Markdown("β‘ Built with Deep Learning + OpenAI + Gradio")
|
| 108 |
+
|
| 109 |
demo.launch()
|