KIKERP_Demo / app.py
vivek9chavan's picture
Update app.py
afb9dad verified
raw
history blame
4.27 kB
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)