vivek9chavan commited on
Commit
a64527c
·
verified ·
1 Parent(s): 43a59e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -53
app.py CHANGED
@@ -1,108 +1,111 @@
1
  import gradio as gr
2
  import os
3
  import json
4
- import time # We need this to wait
5
  from dotenv import load_dotenv
6
 
7
- # Your requested imports
8
  from google import genai
9
  from google.genai import types
10
 
 
 
 
11
  # --- Configuration and Client Initialization ---
12
  load_dotenv()
13
-
14
  try:
15
  client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
16
  except KeyError:
17
  raise gr.Error("FATAL: GEMINI_API_KEY not found. Please set it in your Hugging Face Space secrets.")
18
 
19
- # --- Core Gradio Function ---
20
-
21
- def analyze_device_condition(video_file_path):
22
  if not video_file_path:
23
- return "Please upload video", "", ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
 
25
  uploaded_file = None
26
  try:
27
- # 1. Upload the file
28
- print(f"Log: Uploading file: {video_file_path}...")
29
  uploaded_file = client.files.upload(file=video_file_path)
30
- print(f"Log: File upload initiated. File name: {uploaded_file.name}, State: {uploaded_file.state.name}")
31
-
32
- # 2. --- THIS IS THE CRITICAL POLLING LOOP ---
33
- # Wait for the file to become ACTIVE.
34
  while uploaded_file.state.name == "PROCESSING":
35
- print("Log: File is processing, waiting 5 seconds...")
36
  time.sleep(5)
37
- # Get the latest status of the file.
38
  uploaded_file = client.files.get(name=uploaded_file.name)
39
- print(f"Log: Current file state: {uploaded_file.state.name}")
40
-
41
- # If the file failed processing, stop here.
42
  if uploaded_file.state.name != "ACTIVE":
43
- raise Exception(f"File processing failed. Final state: {uploaded_file.state.name}")
44
- # --- END OF CORRECTION ---
45
-
46
- print("Log: File is now ACTIVE and ready for use.")
47
 
48
- # 3. Prepare the prompt
49
  prompt = """
50
  Analyze the provided video. Respond ONLY with a valid JSON object with three keys:
51
  1. "device_type": A short string identifying the device.
52
  2. "condition": A single word: "Mint", "Excellent", "Good", "Fair", or "Poor".
53
  3. "reason": A brief string explaining the condition.
54
  """
55
-
56
- # 4. Use gemini-2.5-flash
57
- model_name = "gemini-2.5-flash"
58
- generate_content_config = types.GenerateContentConfig(
59
- temperature=0.2,
60
- response_mime_type="application/json"
61
- )
62
-
63
- # 5. Call the API with the now-active file
64
  contents = [uploaded_file, prompt]
65
 
66
- print(f"Log: Sending request to model: {model_name}...")
67
- response = client.models.generate_content(
68
- model=f"models/{model_name}",
69
- contents=contents,
70
- config=generate_content_config,
71
- )
72
-
73
- # 6. Parse the final JSON response
74
  parsed_json = json.loads(response.text)
75
  device_type = parsed_json.get("device_type", "N/A")
76
  condition = parsed_json.get("condition", "N/A")
77
  reason = parsed_json.get("reason", "N/A")
78
-
79
- return device_type, condition, reason
80
-
81
  except Exception as e:
82
- error_message = f"An error occurred: {e}"
83
- print(f"ERROR: {error_message}")
84
- return error_message, "", ""
85
-
86
  finally:
87
- # 7. Cleanup: Delete the file from Google's servers
88
  if uploaded_file:
89
- print(f"Log: Deleting uploaded file: {uploaded_file.name}")
90
  client.files.delete(name=uploaded_file.name)
91
 
 
 
 
92
  # --- Gradio Interface ---
93
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
94
- gr.Markdown("# 📱 Device Condition Analyzer")
 
 
95
  video_input = gr.Video(label="Upload or Record Video", sources=["upload", "webcam"], format="mp4")
96
- submit_button = gr.Button("Analyze Device", variant="primary")
 
 
97
  with gr.Row():
98
  device_type_output = gr.Textbox(label="Device Type")
99
  condition_output = gr.Textbox(label="Condition")
100
  reason_output = gr.Textbox(label="Reason / Details")
 
 
 
 
 
 
 
101
 
102
  submit_button.click(
103
- fn=analyze_device_condition,
104
  inputs=video_input,
105
- outputs=[device_type_output, condition_output, reason_output],
 
 
 
 
 
 
106
  show_progress='full'
107
  )
108
 
 
1
  import gradio as gr
2
  import os
3
  import json
4
+ import time
5
  from dotenv import load_dotenv
6
 
 
7
  from google import genai
8
  from google.genai import types
9
 
10
+ # Import our new DINO processing function
11
+ from dino_processor import process_video_with_dino
12
+
13
  # --- Configuration and Client Initialization ---
14
  load_dotenv()
 
15
  try:
16
  client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
17
  except KeyError:
18
  raise gr.Error("FATAL: GEMINI_API_KEY not found. Please set it in your Hugging Face Space secrets.")
19
 
20
+ # --- Main Gradio Function ---
21
+ def full_analysis(video_file_path):
 
22
  if not video_file_path:
23
+ # Return empty values for all components
24
+ return "Please upload a video first.", "", "", None, None
25
+
26
+ # --- DINO Analysis ---
27
+ print("--- Starting DINO Analysis ---")
28
+ try:
29
+ # This function will return a list of tuples: [(overlay1, attn1), (overlay2, attn2), ...]
30
+ dino_results = process_video_with_dino(video_file_path)
31
+ overlay_images = [res[0] for res in dino_results]
32
+ attention_maps = [res[1] for res in dino_results]
33
+ print("--- DINO Analysis Complete ---")
34
+ except Exception as e:
35
+ print(f"ERROR during DINO processing: {e}")
36
+ # Return an error message and empty galleries
37
+ return f"Error in DINO processing: {e}", "", "", None, None
38
 
39
+ # --- Gemini Analysis ---
40
+ print("--- Starting Gemini Analysis ---")
41
  uploaded_file = None
42
  try:
43
+ # Wait for DINO to finish before starting the Gemini upload
 
44
  uploaded_file = client.files.upload(file=video_file_path)
 
 
 
 
45
  while uploaded_file.state.name == "PROCESSING":
 
46
  time.sleep(5)
 
47
  uploaded_file = client.files.get(name=uploaded_file.name)
 
 
 
48
  if uploaded_file.state.name != "ACTIVE":
49
+ raise Exception(f"File processing failed for Gemini. State: {uploaded_file.state.name}")
 
 
 
50
 
 
51
  prompt = """
52
  Analyze the provided video. Respond ONLY with a valid JSON object with three keys:
53
  1. "device_type": A short string identifying the device.
54
  2. "condition": A single word: "Mint", "Excellent", "Good", "Fair", or "Poor".
55
  3. "reason": A brief string explaining the condition.
56
  """
57
+ model_name = "gemini-1.5-flash-latest" # Using a reliable public model
58
+ config = types.GenerateContentConfig(temperature=0.2, response_mime_type="application/json")
 
 
 
 
 
 
 
59
  contents = [uploaded_file, prompt]
60
 
61
+ response = client.models.generate_content(model=f"models/{model_name}", contents=contents, config=config)
 
 
 
 
 
 
 
62
  parsed_json = json.loads(response.text)
63
  device_type = parsed_json.get("device_type", "N/A")
64
  condition = parsed_json.get("condition", "N/A")
65
  reason = parsed_json.get("reason", "N/A")
66
+ print("--- Gemini Analysis Complete ---")
67
+
 
68
  except Exception as e:
69
+ print(f"ERROR during Gemini processing: {e}")
70
+ device_type, condition, reason = f"Error in Gemini processing: {e}", "", ""
 
 
71
  finally:
 
72
  if uploaded_file:
 
73
  client.files.delete(name=uploaded_file.name)
74
 
75
+ # Return all the results to the Gradio UI
76
+ return device_type, condition, reason, overlay_images, attention_maps
77
+
78
  # --- Gradio Interface ---
79
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
80
+ gr.Markdown("# 📱 Advanced Device Condition Analyzer")
81
+ gr.Markdown("Upload a video to get a condition analysis from Gemini and attention maps from DINO.")
82
+
83
  video_input = gr.Video(label="Upload or Record Video", sources=["upload", "webcam"], format="mp4")
84
+ submit_button = gr.Button("Run Full Analysis", variant="primary")
85
+
86
+ gr.Markdown("## Gemini Condition Analysis")
87
  with gr.Row():
88
  device_type_output = gr.Textbox(label="Device Type")
89
  condition_output = gr.Textbox(label="Condition")
90
  reason_output = gr.Textbox(label="Reason / Details")
91
+
92
+ gr.Markdown("## DINO: Overlayed Heatmaps on Representative Frames")
93
+ # Use gr.Gallery to display multiple images
94
+ overlay_gallery = gr.Gallery(label="Overlayed Heatmaps", elem_id="gallery")
95
+
96
+ gr.Markdown("## DINO: Self-Attention Maps")
97
+ attention_gallery = gr.Gallery(label="Self-Attention Maps", elem_id="gallery")
98
 
99
  submit_button.click(
100
+ fn=full_analysis,
101
  inputs=video_input,
102
+ outputs=[
103
+ device_type_output,
104
+ condition_output,
105
+ reason_output,
106
+ overlay_gallery,
107
+ attention_gallery
108
+ ],
109
  show_progress='full'
110
  )
111