Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,8 @@ from google.genai import types
|
|
| 11 |
# --- Configuration and Client Initialization ---
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 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.")
|
|
@@ -24,50 +24,52 @@ def analyze_device_condition(video_file_path):
|
|
| 24 |
return "Please upload video", "", ""
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# 1. Prepare video file for the client API
|
| 30 |
mime_type, _ = mimetypes.guess_type(video_file_path)
|
| 31 |
if not mime_type or not mime_type.startswith("video"):
|
| 32 |
-
raise ValueError("Unsupported file type.
|
| 33 |
|
| 34 |
with open(video_file_path, "rb") as video:
|
| 35 |
video_part = types.Part(
|
| 36 |
inline_data=types.Blob(mime_type=mime_type, data=video.read())
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# 2. Prepare the prompt
|
| 40 |
prompt = """
|
| 41 |
-
Analyze the provided video
|
| 42 |
-
The JSON object must have the following three keys and nothing else:
|
| 43 |
1. "device_type": A short string identifying the device.
|
| 44 |
-
2. "condition": A single word
|
| 45 |
-
3. "reason": A brief string explaining the condition
|
| 46 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 49 |
model_name = "gemini-2.5-flash"
|
| 50 |
-
|
| 51 |
generate_content_config = types.GenerateContentConfig(
|
| 52 |
temperature=0.2,
|
| 53 |
response_mime_type="application/json"
|
| 54 |
)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
contents = [prompt, video_part]
|
| 58 |
-
|
| 59 |
-
# 3. Call the Gemini API using the client.generate_content method
|
| 60 |
print(f"Log: Sending request to model: {model_name}...")
|
| 61 |
-
|
| 62 |
model=f"models/{model_name}",
|
| 63 |
contents=contents,
|
| 64 |
generation_config=generate_content_config,
|
| 65 |
)
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
parsed_json = json.loads(
|
| 71 |
device_type = parsed_json.get("device_type", "N/A")
|
| 72 |
condition = parsed_json.get("condition", "N/A")
|
| 73 |
reason = parsed_json.get("reason", "N/A")
|
|
@@ -75,8 +77,8 @@ def analyze_device_condition(video_file_path):
|
|
| 75 |
return device_type, condition, reason
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
-
print(f"!!!!!!!! AN ERROR OCCURRED !!!!!!!!\n{e}")
|
| 79 |
error_message = f"An error occurred: {e}"
|
|
|
|
| 80 |
return error_message, "", ""
|
| 81 |
|
| 82 |
# --- Gradio Interface ---
|
|
|
|
| 11 |
# --- Configuration and Client Initialization ---
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
try
|
| 15 |
+
# Initializing the client exactly as in your code
|
| 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.")
|
|
|
|
| 24 |
return "Please upload video", "", ""
|
| 25 |
|
| 26 |
try:
|
| 27 |
+
# 1. Prepare video file
|
|
|
|
|
|
|
| 28 |
mime_type, _ = mimetypes.guess_type(video_file_path)
|
| 29 |
if not mime_type or not mime_type.startswith("video"):
|
| 30 |
+
raise ValueError("Unsupported file type.")
|
| 31 |
|
| 32 |
with open(video_file_path, "rb") as video:
|
| 33 |
video_part = types.Part(
|
| 34 |
inline_data=types.Blob(mime_type=mime_type, data=video.read())
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# 2. Prepare the prompt
|
| 38 |
prompt = """
|
| 39 |
+
Analyze the provided video. Respond ONLY with a valid JSON object with three keys:
|
|
|
|
| 40 |
1. "device_type": A short string identifying the device.
|
| 41 |
+
2. "condition": A single word: "Mint", "Excellent", "Good", "Fair", or "Poor".
|
| 42 |
+
3. "reason": A brief string explaining the condition.
|
| 43 |
"""
|
| 44 |
+
prompt_part = types.Part.from_text(prompt)
|
| 45 |
+
|
| 46 |
+
# 3. --- THIS IS THE KEY CORRECTION ---
|
| 47 |
+
# Wrapping the parts inside types.Content, exactly like your reference code.
|
| 48 |
+
contents = [
|
| 49 |
+
types.Content(parts=[prompt_part, video_part])
|
| 50 |
+
]
|
| 51 |
+
# --- END OF CORRECTION ---
|
| 52 |
|
| 53 |
+
# 4. Use gemini-2.5-flash and the specified config
|
| 54 |
model_name = "gemini-2.5-flash"
|
|
|
|
| 55 |
generate_content_config = types.GenerateContentConfig(
|
| 56 |
temperature=0.2,
|
| 57 |
response_mime_type="application/json"
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# 5. Call the API using the streaming method from your code
|
|
|
|
|
|
|
|
|
|
| 61 |
print(f"Log: Sending request to model: {model_name}...")
|
| 62 |
+
stream = client.models.generate_content_stream(
|
| 63 |
model=f"models/{model_name}",
|
| 64 |
contents=contents,
|
| 65 |
generation_config=generate_content_config,
|
| 66 |
)
|
| 67 |
|
| 68 |
+
# 6. Handle the streamed response to build the complete JSON string
|
| 69 |
+
response_text = "".join(chunk.text for chunk in stream)
|
| 70 |
|
| 71 |
+
# 7. Parse the final JSON response
|
| 72 |
+
parsed_json = json.loads(response_text)
|
| 73 |
device_type = parsed_json.get("device_type", "N/A")
|
| 74 |
condition = parsed_json.get("condition", "N/A")
|
| 75 |
reason = parsed_json.get("reason", "N/A")
|
|
|
|
| 77 |
return device_type, condition, reason
|
| 78 |
|
| 79 |
except Exception as e:
|
|
|
|
| 80 |
error_message = f"An error occurred: {e}"
|
| 81 |
+
print(f"ERROR: {error_message}")
|
| 82 |
return error_message, "", ""
|
| 83 |
|
| 84 |
# --- Gradio Interface ---
|