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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -115
app.py CHANGED
@@ -1,116 +1,116 @@
1
- import gradio as gr
2
- import google.generativeai as 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(
98
- fn=analyze_device_condition,
99
- inputs=video_input,
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
+ 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(
98
+ fn=analyze_device_condition,
99
+ inputs=video_input,
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)