Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
-
import mimetypes
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# Your
|
| 8 |
from google import genai
|
| 9 |
-
from google.genai import types
|
| 10 |
|
| 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.")
|
|
@@ -23,16 +21,12 @@ def analyze_device_condition(video_file_path):
|
|
| 23 |
if not video_file_path:
|
| 24 |
return "Please upload video", "", ""
|
| 25 |
|
|
|
|
| 26 |
try:
|
| 27 |
-
# 1.
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 = """
|
|
@@ -41,35 +35,27 @@ def analyze_device_condition(video_file_path):
|
|
| 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 |
-
#
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
| 61 |
print(f"Log: Sending request to model: {model_name}...")
|
| 62 |
-
|
| 63 |
model=f"models/{model_name}",
|
| 64 |
contents=contents,
|
| 65 |
generation_config=generate_content_config,
|
| 66 |
)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 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")
|
|
@@ -80,6 +66,13 @@ def analyze_device_condition(video_file_path):
|
|
| 80 |
error_message = f"An error occurred: {e}"
|
| 81 |
print(f"ERROR: {error_message}")
|
| 82 |
return error_message, "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# --- Gradio Interface ---
|
| 85 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import json
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
# Your requested imports
|
| 7 |
from google import genai
|
| 8 |
+
from google.genai import types # Still needed for GenerationConfig
|
| 9 |
|
| 10 |
# --- Configuration and Client Initialization ---
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
try:
|
|
|
|
| 14 |
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 15 |
except KeyError:
|
| 16 |
raise gr.Error("FATAL: GEMINI_API_KEY not found. Please set it in your Hugging Face Space secrets.")
|
|
|
|
| 21 |
if not video_file_path:
|
| 22 |
return "Please upload video", "", ""
|
| 23 |
|
| 24 |
+
uploaded_file = None # Initialize to ensure it exists for the finally block
|
| 25 |
try:
|
| 26 |
+
# 1. --- UPLOAD FILE USING THE NEW, CORRECT METHOD ---
|
| 27 |
+
print(f"Log: Uploading file: {video_file_path}...")
|
| 28 |
+
uploaded_file = client.files.upload(file_path=video_file_path)
|
| 29 |
+
print("Log: File uploaded successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# 2. Prepare the prompt
|
| 32 |
prompt = """
|
|
|
|
| 35 |
2. "condition": A single word: "Mint", "Excellent", "Good", "Fair", or "Poor".
|
| 36 |
3. "reason": A brief string explaining the condition.
|
| 37 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# 3. Use gemini-2.5-flash and the specified config
|
| 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 |
+
# 4. --- CALL THE API WITH THE SIMPLIFIED CONTENTS LIST ---
|
| 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}...")
|
| 51 |
+
response = client.models.generate_content(
|
| 52 |
model=f"models/{model_name}",
|
| 53 |
contents=contents,
|
| 54 |
generation_config=generate_content_config,
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# 5. Parse the final JSON response
|
| 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")
|
| 61 |
reason = parsed_json.get("reason", "N/A")
|
|
|
|
| 66 |
error_message = f"An error occurred: {e}"
|
| 67 |
print(f"ERROR: {error_message}")
|
| 68 |
return error_message, "", ""
|
| 69 |
+
|
| 70 |
+
finally:
|
| 71 |
+
# 6. --- CLEANUP: Delete the file from Google's servers ---
|
| 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:
|