Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ from dotenv import load_dotenv
|
|
| 5 |
|
| 6 |
# Your requested imports
|
| 7 |
from google import genai
|
| 8 |
-
from google.genai import types
|
| 9 |
|
| 10 |
# --- Configuration and Client Initialization ---
|
| 11 |
load_dotenv()
|
|
@@ -21,14 +21,16 @@ def analyze_device_condition(video_file_path):
|
|
| 21 |
if not video_file_path:
|
| 22 |
return "Please upload video", "", ""
|
| 23 |
|
| 24 |
-
uploaded_file = None
|
| 25 |
try:
|
| 26 |
-
#
|
|
|
|
| 27 |
print(f"Log: Uploading file: {video_file_path}...")
|
| 28 |
-
uploaded_file = client.files.upload(
|
| 29 |
print("Log: File uploaded successfully.")
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 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,15 +38,14 @@ def analyze_device_condition(video_file_path):
|
|
| 36 |
3. "reason": A brief string explaining the condition.
|
| 37 |
"""
|
| 38 |
|
| 39 |
-
#
|
| 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 |
-
#
|
| 47 |
-
# Just the uploaded file object and the prompt string.
|
| 48 |
contents = [uploaded_file, prompt]
|
| 49 |
|
| 50 |
print(f"Log: Sending request to model: {model_name}...")
|
|
@@ -54,7 +55,7 @@ def analyze_device_condition(video_file_path):
|
|
| 54 |
generation_config=generate_content_config,
|
| 55 |
)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
parsed_json = json.loads(response.text)
|
| 59 |
device_type = parsed_json.get("device_type", "N/A")
|
| 60 |
condition = parsed_json.get("condition", "N/A")
|
|
@@ -68,12 +69,11 @@ def analyze_device_condition(video_file_path):
|
|
| 68 |
return error_message, "", ""
|
| 69 |
|
| 70 |
finally:
|
| 71 |
-
#
|
| 72 |
if uploaded_file:
|
| 73 |
print(f"Log: Deleting uploaded file: {uploaded_file.name}")
|
| 74 |
client.files.delete(name=uploaded_file.name)
|
| 75 |
|
| 76 |
-
|
| 77 |
# --- Gradio Interface ---
|
| 78 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 79 |
gr.Markdown("# 📱 Device Condition Analyzer")
|
|
|
|
| 5 |
|
| 6 |
# Your requested imports
|
| 7 |
from google import genai
|
| 8 |
+
from google.genai import types
|
| 9 |
|
| 10 |
# --- Configuration and Client Initialization ---
|
| 11 |
load_dotenv()
|
|
|
|
| 21 |
if not video_file_path:
|
| 22 |
return "Please upload video", "", ""
|
| 23 |
|
| 24 |
+
uploaded_file = None
|
| 25 |
try:
|
| 26 |
+
# --- THIS IS THE CORRECTED LINE ---
|
| 27 |
+
# The keyword argument is 'file', not 'file_path'.
|
| 28 |
print(f"Log: Uploading file: {video_file_path}...")
|
| 29 |
+
uploaded_file = client.files.upload(file=video_file_path)
|
| 30 |
print("Log: File uploaded successfully.")
|
| 31 |
+
# --- END OF CORRECTION ---
|
| 32 |
|
| 33 |
+
# Prepare the prompt
|
| 34 |
prompt = """
|
| 35 |
Analyze the provided video. Respond ONLY with a valid JSON object with three keys:
|
| 36 |
1. "device_type": A short string identifying the device.
|
|
|
|
| 38 |
3. "reason": A brief string explaining the condition.
|
| 39 |
"""
|
| 40 |
|
| 41 |
+
# Use gemini-2.5-flash
|
| 42 |
model_name = "gemini-2.5-flash"
|
| 43 |
generate_content_config = types.GenerateContentConfig(
|
| 44 |
temperature=0.2,
|
| 45 |
response_mime_type="application/json"
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Call the API with the simplified contents list
|
|
|
|
| 49 |
contents = [uploaded_file, prompt]
|
| 50 |
|
| 51 |
print(f"Log: Sending request to model: {model_name}...")
|
|
|
|
| 55 |
generation_config=generate_content_config,
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Parse the final JSON response
|
| 59 |
parsed_json = json.loads(response.text)
|
| 60 |
device_type = parsed_json.get("device_type", "N/A")
|
| 61 |
condition = parsed_json.get("condition", "N/A")
|
|
|
|
| 69 |
return error_message, "", ""
|
| 70 |
|
| 71 |
finally:
|
| 72 |
+
# Cleanup: Delete the file from Google's servers
|
| 73 |
if uploaded_file:
|
| 74 |
print(f"Log: Deleting uploaded file: {uploaded_file.name}")
|
| 75 |
client.files.delete(name=uploaded_file.name)
|
| 76 |
|
|
|
|
| 77 |
# --- Gradio Interface ---
|
| 78 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 79 |
gr.Markdown("# 📱 Device Condition Analyzer")
|