vivek9chavan commited on
Commit
5a474ac
·
verified ·
1 Parent(s): 2955ae6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -36
app.py CHANGED
@@ -1,63 +1,70 @@
1
  import gradio as gr
2
- import google.generativeai as genai
3
  import os
4
- import time
5
  import json
 
6
  from dotenv import load_dotenv
7
 
8
- # --- Configuration ---
 
 
 
 
9
  load_dotenv()
10
 
11
- # This is the correct way to initialize for the 'google-genai' library
12
  try:
13
- genai.configure(api_key=os.environ["GEMINI_API_KEY"])
14
  except KeyError:
15
  raise gr.Error("FATAL: GEMINI_API_KEY not found. Please set it in your Hugging Face Space secrets.")
16
 
17
- # --- Core Function ---
18
 
19
  def analyze_device_condition(video_file_path):
20
  if not video_file_path:
21
  return "Please upload video", "", ""
22
 
23
  try:
24
- # 1. This library has a simple helper to upload and process the file
25
- print("Log: Uploading file to Google...")
26
- video_file = genai.upload_file(path=video_file_path)
27
-
28
- while video_file.state.name == "PROCESSING":
29
- print("Log: Waiting for video processing...")
30
- time.sleep(5)
31
- video_file = genai.get_file(video_file.name)
32
-
33
- if video_file.state.name == "FAILED":
34
- raise gr.Error("Video processing failed. The file might be corrupted or in an unsupported format.")
35
-
36
- print(f"Log: File processed successfully.")
37
 
38
- # 2. Prompt for JSON output
 
 
 
 
 
 
 
 
 
 
39
  prompt = """
40
- Analyze the provided video. Respond ONLY with a valid JSON object with three keys:
 
41
  1. "device_type": A short string identifying the device.
42
- 2. "condition": A single word: "Mint", "Excellent", "Good", "Fair", or "Poor".
43
- 3. "reason": A brief string explaining the condition.
44
  """
45
-
46
- # 3. Call the Gemini Model using the simpler syntax
47
- # Using a reliable flash model
48
- model = genai.GenerativeModel(model_name="gemini-1.5-flash-latest")
49
 
50
- print("Log: Sending prompt and video to Gemini...")
51
- response = model.generate_content(
52
- [prompt, video_file],
53
- generation_config=genai.types.GenerationConfig(
54
- response_mime_type="application/json",
55
- temperature=0.2
56
- )
57
  )
58
 
59
- print("Log: Analysis received from Gemini.")
60
- genai.delete_file(video_file.name)
 
 
 
 
 
 
 
 
 
 
61
 
62
  # 4. Parse the JSON response
63
  parsed_json = json.loads(response.text)
 
1
  import gradio as gr
 
2
  import os
 
3
  import json
4
+ import mimetypes
5
  from dotenv import load_dotenv
6
 
7
+ # Your exact requested imports
8
+ from google import genai
9
+ from google.genai import types
10
+
11
+ # --- Configuration and Client Initialization ---
12
  load_dotenv()
13
 
14
+ # Initializing the client exactly as in your code
15
  try:
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.")
19
 
20
+ # --- Core Gradio Function ---
21
 
22
  def analyze_device_condition(video_file_path):
23
  if not video_file_path:
24
  return "Please upload video", "", ""
25
 
26
  try:
27
+ print(f"Log: Starting analysis for video: {video_file_path}")
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # 1. Prepare video file for the client API
30
+ mime_type, _ = mimetypes.guess_type(video_file_path)
31
+ if not mime_type or not mime_type.startswith("video"):
32
+ raise ValueError("Unsupported file type. Please upload a valid video.")
33
+
34
+ with open(video_file_path, "rb") as video:
35
+ video_part = types.Part(
36
+ inline_data=types.Blob(mime_type=mime_type, data=video.read())
37
+ )
38
+
39
+ # 2. Prepare the prompt and model settings from your code
40
  prompt = """
41
+ Analyze the provided video of a device. Respond ONLY with a valid JSON object.
42
+ The JSON object must have the following three keys and nothing else:
43
  1. "device_type": A short string identifying the device.
44
+ 2. "condition": A single word describing its condition. Choose from: "Mint", "Excellent", "Good", "Fair", "Poor".
45
+ 3. "reason": A brief string explaining the condition rating.
46
  """
 
 
 
 
47
 
48
+ # USING YOUR EXACT REQUESTED MODEL NAME
49
+ model_name = "gemini-2.5-flash"
50
+
51
+ generate_content_config = types.GenerateContentConfig(
52
+ temperature=0.2,
53
+ response_mime_type="application/json"
 
54
  )
55
 
56
+ # The contents list must contain both the text prompt and the video part
57
+ contents = [prompt, video_part]
58
+
59
+ # 3. Call the Gemini API using the client.generate_content method
60
+ print(f"Log: Sending request to model: {model_name}...")
61
+ response = client.generate_content(
62
+ model=f"models/{model_name}",
63
+ contents=contents,
64
+ generation_config=generate_content_config,
65
+ )
66
+
67
+ print("Log: Analysis received.")
68
 
69
  # 4. Parse the JSON response
70
  parsed_json = json.loads(response.text)