File size: 4,265 Bytes
afb9dad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5e47f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import gradio as gr
from google import genai
import os
import time
from dotenv import load_dotenv

# --- Configuration ---
# To run this locally, create a .env file and add your GEMINI_API_KEY
# To deploy on Hugging Face Spaces, set the GEMINI_API_KEY in the Space's secrets
load_dotenv()

try:
    # Configure the Gemini API key
    genai.configure(api_key=os.environ["GEMINI_API_KEY"])
except KeyError:
    # If the key is not found, raise an error to the user
    raise gr.Error("GEMINI_API_KEY not found. Please set it in your Hugging Face Space secrets.")


# --- Core Function ---

def analyze_device_condition(video_file_path):
    """
    Analyzes a video of a device to classify it and assess its visual condition using Gemini 1.5 Pro.
    """
    if not video_file_path:
        return "Please upload or record a video first."

    # Show a message to the user that the file is being processed
    print(f"Uploading file: {video_file_path}...")
    
    # Upload the video file to Gemini
    # The API is designed to handle the file upload and processing asynchronously
    video_file = genai.upload_file(path=video_file_path)

    # Wait for the video to be processed
    while video_file.state.name == "PROCESSING":
        print("Waiting for video to be processed...")
        time.sleep(2)
        video_file = genai.get_file(video_file.name)

    if video_file.state.name == "FAILED":
        raise gr.Error("Video processing failed. Please try another video.")
        
    print(f"File uploaded successfully: {video_file.uri}")

    # --- Prompt Engineering ---
    # This is where you tell the AI exactly what you want it to do.
    prompt = """
    You are an expert in analyzing the condition of electronic devices from videos. 
    Please analyze the provided video and give me the following information in a clear, structured format:

    1. **Device Type:** Identify the specific device model (e.g., iPhone 14 Pro, Dell XPS 15, Samsung Galaxy S22 Ultra). Be as specific as possible. If you cannot be certain, provide the most likely options.
    
    2. **Visual Condition:** Describe the physical condition of the device in detail. Mention any visible scratches, dents, cracks on the screen or body, smudges, and its overall cleanliness. Grade the condition on a scale of 'Mint', 'Good', 'Fair', or 'Poor'.
    """

    # --- Model Interaction ---
    # We use gemini-1.5-pro as it's excellent with video input
    model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest")
    
    # Send the prompt and the video file to the model
    print("Generating analysis...")
    response = model.generate_content([prompt, video_file], request_options={"timeout": 600})
    
    # Clean up the uploaded file from the Gemini server
    genai.delete_file(video_file.name)
    print(f"File {video_file.name} deleted.")

    # Return the AI's response text
    return response.text


# --- Gradio Interface ---

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # 📱 Device Condition Analyzer
        Upload or record a short video of an electronic device (like a phone, tablet, or laptop).
        The AI will identify the device and analyze its visual condition.
        """
    )
    
    # The video component allows both upload and webcam recording, perfect for mobile.
    video_input = gr.Video(
        label="Upload or Record Video", 
        sources=["upload", "webcam"],
        format="mp4" # Explicitly setting format can help with compatibility
    )
    
    submit_button = gr.Button("Analyze Device", variant="primary")
    
    # The Markdown component is used for a nicely formatted output.
    output_text = gr.Markdown(label="Analysis Results")

    submit_button.click(
        fn=analyze_device_condition,
        inputs=video_input,
        outputs=output_text
    )

    gr.Examples(
        examples=[
            ["assets/sample_phone_video.mp4"],
        ],
        inputs=video_input,
        outputs=output_text,
        fn=analyze_device_condition,
        cache_examples=True # Speeds up demo for users
    )

# --- Launch the App ---
# The launch() command will start the web server.
# On Hugging Face Spaces, it's automatically exposed to the public.
demo.launch(debug=True)