vivek9chavan commited on
Commit
7135735
·
verified ·
1 Parent(s): 2308ede

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import os
3
  import json
 
4
  from dotenv import load_dotenv
5
 
6
  # Your requested imports
@@ -23,12 +24,28 @@ def analyze_device_condition(video_file_path):
23
 
24
  uploaded_file = None
25
  try:
26
- # Upload the file
27
  print(f"Log: Uploading file: {video_file_path}...")
28
  uploaded_file = client.files.upload(file=video_file_path)
29
- print("Log: File uploaded successfully.")
30
 
31
- # Prepare the prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  prompt = """
33
  Analyze the provided video. Respond ONLY with a valid JSON object with three keys:
34
  1. "device_type": A short string identifying the device.
@@ -36,27 +53,24 @@ def analyze_device_condition(video_file_path):
36
  3. "reason": A brief string explaining the condition.
37
  """
38
 
39
- # Use gemini-2.5-flash
40
  model_name = "gemini-2.5-flash"
41
  generate_content_config = types.GenerateContentConfig(
42
  temperature=0.2,
43
  response_mime_type="application/json"
44
  )
45
 
46
- # Call the API with the simplified contents list
47
  contents = [uploaded_file, prompt]
48
 
49
- # --- THIS IS THE CORRECTED LINE ---
50
- # The keyword argument is 'config', not 'generation_config'.
51
  print(f"Log: Sending request to model: {model_name}...")
52
  response = client.models.generate_content(
53
  model=f"models/{model_name}",
54
  contents=contents,
55
  config=generate_content_config,
56
  )
57
- # --- END OF CORRECTION ---
58
 
59
- # Parse the final JSON response
60
  parsed_json = json.loads(response.text)
61
  device_type = parsed_json.get("device_type", "N/A")
62
  condition = parsed_json.get("condition", "N/A")
@@ -70,7 +84,7 @@ def analyze_device_condition(video_file_path):
70
  return error_message, "", ""
71
 
72
  finally:
73
- # Cleanup: Delete the file from Google's servers
74
  if uploaded_file:
75
  print(f"Log: Deleting uploaded file: {uploaded_file.name}")
76
  client.files.delete(name=uploaded_file.name)
 
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
 
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.
 
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")
 
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)