charantejapolavarapu commited on
Commit
3ee9eb0
Β·
verified Β·
1 Parent(s): dfd71b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -13
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
- # LOAD LIGHT MODEL
 
 
 
 
 
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
- # PREDICTION FUNCTION
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Explanation (simple AI)
42
- explanation = f"This image is likely a {results[0]['label']} based on patterns like shape, color, and texture."
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
- gr.Markdown("# 🧠 AI Vision App (Single App πŸš€)")
 
 
 
 
64
 
65
  with gr.Row():
66
- img = gr.Image(source="webcam", type="pil")
67
- btn = gr.Button("Analyze")
 
68
 
69
- output = gr.Dataframe(label="Predictions")
70
- explanation = gr.Textbox(label="AI Explanation")
71
- chart = gr.Image(label="Confidence Chart")
72
- history_table = gr.Dataframe(label="History")
 
 
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()