vivek9chavan commited on
Commit
0069ecb
·
verified ·
1 Parent(s): afb9dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -57
app.py CHANGED
@@ -1,97 +1,70 @@
1
  import gradio as gr
2
- from google import genai
3
  import os
4
  import time
5
  from dotenv import load_dotenv
6
 
7
  # --- Configuration ---
8
- # To run this locally, create a .env file and add your GEMINI_API_KEY
9
  # To deploy on Hugging Face Spaces, set the GEMINI_API_KEY in the Space's secrets
10
  load_dotenv()
11
 
12
  try:
13
- # Configure the Gemini API key
14
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
15
  except KeyError:
16
- # If the key is not found, raise an error to the user
17
  raise gr.Error("GEMINI_API_KEY not found. Please set it in your Hugging Face Space secrets.")
18
 
19
 
20
- # --- Core Function ---
21
 
22
  def analyze_device_condition(video_file_path):
23
- """
24
- Analyzes a video of a device to classify it and assess its visual condition using Gemini 1.5 Pro.
25
- """
26
  if not video_file_path:
27
  return "Please upload or record a video first."
28
 
29
- # Show a message to the user that the file is being processed
30
  print(f"Uploading file: {video_file_path}...")
31
-
32
- # Upload the video file to Gemini
33
- # The API is designed to handle the file upload and processing asynchronously
34
  video_file = genai.upload_file(path=video_file_path)
35
 
36
- # Wait for the video to be processed
37
  while video_file.state.name == "PROCESSING":
38
  print("Waiting for video to be processed...")
39
- time.sleep(2)
40
  video_file = genai.get_file(video_file.name)
41
 
42
  if video_file.state.name == "FAILED":
43
  raise gr.Error("Video processing failed. Please try another video.")
44
 
45
- print(f"File uploaded successfully: {video_file.uri}")
46
 
47
- # --- Prompt Engineering ---
48
- # This is where you tell the AI exactly what you want it to do.
49
  prompt = """
50
  You are an expert in analyzing the condition of electronic devices from videos.
51
  Please analyze the provided video and give me the following information in a clear, structured format:
52
 
53
- 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.
54
-
55
- 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'.
56
  """
57
 
58
  # --- Model Interaction ---
59
- # We use gemini-1.5-pro as it's excellent with video input
60
  model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest")
61
 
62
- # Send the prompt and the video file to the model
63
  print("Generating analysis...")
64
  response = model.generate_content([prompt, video_file], request_options={"timeout": 600})
65
 
66
- # Clean up the uploaded file from the Gemini server
67
  genai.delete_file(video_file.name)
68
  print(f"File {video_file.name} deleted.")
69
 
70
- # Return the AI's response text
71
  return response.text
72
 
73
 
74
- # --- Gradio Interface ---
75
-
76
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
77
- gr.Markdown(
78
- """
79
- # 📱 Device Condition Analyzer
80
- Upload or record a short video of an electronic device (like a phone, tablet, or laptop).
81
- The AI will identify the device and analyze its visual condition.
82
- """
83
- )
84
-
85
- # The video component allows both upload and webcam recording, perfect for mobile.
86
- video_input = gr.Video(
87
- label="Upload or Record Video",
88
- sources=["upload", "webcam"],
89
- format="mp4" # Explicitly setting format can help with compatibility
90
- )
91
-
92
  submit_button = gr.Button("Analyze Device", variant="primary")
93
-
94
- # The Markdown component is used for a nicely formatted output.
95
  output_text = gr.Markdown(label="Analysis Results")
96
 
97
  submit_button.click(
@@ -100,17 +73,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
100
  outputs=output_text
101
  )
102
 
103
- gr.Examples(
104
- examples=[
105
- ["assets/sample_phone_video.mp4"],
106
- ],
107
- inputs=video_input,
108
- outputs=output_text,
109
- fn=analyze_device_condition,
110
- cache_examples=True # Speeds up demo for users
111
- )
112
-
113
- # --- Launch the App ---
114
- # The launch() command will start the web server.
115
- # On Hugging Face Spaces, it's automatically exposed to the public.
116
- demo.launch(debug=True)
 
1
  import gradio as gr
2
+ import google.generativeai as genai # This is the correct, modern import
3
  import os
4
  import time
5
  from dotenv import load_dotenv
6
 
7
  # --- Configuration ---
 
8
  # To deploy on Hugging Face Spaces, set the GEMINI_API_KEY in the Space's secrets
9
  load_dotenv()
10
 
11
  try:
12
+ # Configure the Gemini API key using the modern SDK's method
13
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
14
  except KeyError:
 
15
  raise gr.Error("GEMINI_API_KEY not found. Please set it in your Hugging Face Space secrets.")
16
 
17
 
18
+ # --- Core Function using the Modern SDK ---
19
 
20
  def analyze_device_condition(video_file_path):
 
 
 
21
  if not video_file_path:
22
  return "Please upload or record a video first."
23
 
 
24
  print(f"Uploading file: {video_file_path}...")
25
+ # The new SDK provides a clean file upload API, which is what we need for video
 
 
26
  video_file = genai.upload_file(path=video_file_path)
27
 
28
+ # Wait for the video to be processed by Google's servers
29
  while video_file.state.name == "PROCESSING":
30
  print("Waiting for video to be processed...")
31
+ time.sleep(2) # Wait 2 seconds before checking again
32
  video_file = genai.get_file(video_file.name)
33
 
34
  if video_file.state.name == "FAILED":
35
  raise gr.Error("Video processing failed. Please try another video.")
36
 
37
+ print(f"File uploaded successfully: {video_file.name}")
38
 
39
+ # --- Prompt for the Model ---
 
40
  prompt = """
41
  You are an expert in analyzing the condition of electronic devices from videos.
42
  Please analyze the provided video and give me the following information in a clear, structured format:
43
 
44
+ 1. **Device Type:** Identify the specific device model (e.g., iPhone 14 Pro, Dell XPS 15). Be as specific as possible.
45
+ 2. **Visual Condition:** Describe the physical condition in detail. Mention any visible scratches, dents, or cracks on the screen or body. Grade the condition on a scale of 'Mint', 'Good', 'Fair', or 'Poor'.
 
46
  """
47
 
48
  # --- Model Interaction ---
49
+ # Use the latest available model that's best for video
50
  model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest")
51
 
52
+ # Send the prompt and the processed video file to the model
53
  print("Generating analysis...")
54
  response = model.generate_content([prompt, video_file], request_options={"timeout": 600})
55
 
56
+ # Clean up the file from Google's servers after we're done
57
  genai.delete_file(video_file.name)
58
  print(f"File {video_file.name} deleted.")
59
 
 
60
  return response.text
61
 
62
 
63
+ # --- Gradio Interface (No changes here) ---
 
64
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
65
+ gr.Markdown("# 📱 Device Condition Analyzer")
66
+ video_input = gr.Video(label="Upload or Record Video", sources=["upload", "webcam"])
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  submit_button = gr.Button("Analyze Device", variant="primary")
 
 
68
  output_text = gr.Markdown(label="Analysis Results")
69
 
70
  submit_button.click(
 
73
  outputs=output_text
74
  )
75
 
76
+ demo.launch()