mavihsrr commited on
Commit
a8db576
1 Parent(s): 557fd6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -2,8 +2,8 @@ import gradio as gr
2
  import cv2
3
  from PIL import Image
4
  import google.generativeai as genai
5
- from dotenv import load_dotenv
6
  import os
 
7
 
8
  load_dotenv()
9
  api_key = os.getenv('api_key')
@@ -17,13 +17,13 @@ def extract_frames(video_path, fps=30):
17
  raise ValueError("Error: Unable to open video file.")
18
 
19
  original_fps = cap.get(cv2.CAP_PROP_FPS)
20
- if original_fps == 0:
21
- raise ValueError("Error: Unable to retrieve FPS from video file.")
22
-
23
- frame_interval = int(original_fps // fps)
24
  frame_count = 0
25
  success, frame = cap.read()
26
-
27
  while success:
28
  if frame_count % frame_interval == 0:
29
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
@@ -31,11 +31,12 @@ def extract_frames(video_path, fps=30):
31
  frames.append(pil_image)
32
  frame_count += 1
33
  success, frame = cap.read()
34
-
35
  cap.release()
 
36
  if len(frames) == 0:
37
  raise ValueError("Error: No frames extracted from the video.")
38
-
39
  return frames
40
 
41
  def generate_prompt(button_list):
@@ -46,7 +47,7 @@ def generate_prompt(button_list):
46
  "- What the button is supposed to do.\n\n"
47
  "Preconditions\n"
48
  "- Any requirements before interaction.\n\n"
49
- " Test Steps\n"
50
  "- Concise steps for executing the test.\n\n"
51
  "Expected Results\n"
52
  "- What the expected outcome should be.\n\n"
@@ -55,31 +56,32 @@ def generate_prompt(button_list):
55
  "Format the output clearly for easy readability, using bullet points for key details."
56
  )
57
 
58
-
59
  def process_video(video_file, buttons_to_test):
60
  try:
61
  video_path = "uploaded_video.mp4"
62
  with open(video_path, "wb") as f:
63
- f.write(video_file)
64
-
65
- # Extract frames
66
- frames = extract_frames(video_path, fps=30)
 
67
  button_list = [button.strip() for button in buttons_to_test.split(",")]
68
  prompt = generate_prompt(button_list)
69
- model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest")
70
- response = model.generate_content([prompt])
71
-
 
72
  output_text = response.text if hasattr(response, 'text') else "Error: Invalid response from the model."
73
  output_text = output_text.replace("Functionality", "📋 Functionality")
74
  output_text = output_text.replace("Preconditions", "🔍 Preconditions")
75
  output_text = output_text.replace("Test Steps", "📝 Test Steps")
76
  output_text = output_text.replace("Expected Results", "✅ Expected Results")
77
  output_text = output_text.replace("Automated Testing Tools", "🛠️ Automated Testing Tools")
78
-
79
  return output_text
80
-
81
  except Exception as e:
82
- return str(e)
83
 
84
  iface = gr.Interface(
85
  fn=process_video,
@@ -93,5 +95,3 @@ iface = gr.Interface(
93
  )
94
 
95
  iface.launch(share=True)
96
-
97
-
 
2
  import cv2
3
  from PIL import Image
4
  import google.generativeai as genai
 
5
  import os
6
+ from dotenv import load_dotenv
7
 
8
  load_dotenv()
9
  api_key = os.getenv('api_key')
 
17
  raise ValueError("Error: Unable to open video file.")
18
 
19
  original_fps = cap.get(cv2.CAP_PROP_FPS)
20
+ if original_fps <= 0:
21
+ raise ValueError("Error: Unable to retrieve valid FPS from video file.")
22
+
23
+ frame_interval = max(1, int(original_fps // fps))
24
  frame_count = 0
25
  success, frame = cap.read()
26
+
27
  while success:
28
  if frame_count % frame_interval == 0:
29
  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 
31
  frames.append(pil_image)
32
  frame_count += 1
33
  success, frame = cap.read()
34
+
35
  cap.release()
36
+
37
  if len(frames) == 0:
38
  raise ValueError("Error: No frames extracted from the video.")
39
+
40
  return frames
41
 
42
  def generate_prompt(button_list):
 
47
  "- What the button is supposed to do.\n\n"
48
  "Preconditions\n"
49
  "- Any requirements before interaction.\n\n"
50
+ "Test Steps\n"
51
  "- Concise steps for executing the test.\n\n"
52
  "Expected Results\n"
53
  "- What the expected outcome should be.\n\n"
 
56
  "Format the output clearly for easy readability, using bullet points for key details."
57
  )
58
 
 
59
  def process_video(video_file, buttons_to_test):
60
  try:
61
  video_path = "uploaded_video.mp4"
62
  with open(video_path, "wb") as f:
63
+ f.write(video_file)
64
+
65
+ frames = extract_frames(video_path, fps=15)
66
+ frames_to_pass = frames[::10] # use every 10th frame
67
+
68
  button_list = [button.strip() for button in buttons_to_test.split(",")]
69
  prompt = generate_prompt(button_list)
70
+
71
+ frame_prompts = [frame for frame in frames_to_pass]
72
+ response = genai.GenerativeModel(model_name="gemini-1.5-pro-latest").generate_content([prompt] + frame_prompts)
73
+
74
  output_text = response.text if hasattr(response, 'text') else "Error: Invalid response from the model."
75
  output_text = output_text.replace("Functionality", "📋 Functionality")
76
  output_text = output_text.replace("Preconditions", "🔍 Preconditions")
77
  output_text = output_text.replace("Test Steps", "📝 Test Steps")
78
  output_text = output_text.replace("Expected Results", "✅ Expected Results")
79
  output_text = output_text.replace("Automated Testing Tools", "🛠️ Automated Testing Tools")
80
+
81
  return output_text
82
+
83
  except Exception as e:
84
+ return f"Error occurred: {str(e)}"
85
 
86
  iface = gr.Interface(
87
  fn=process_video,
 
95
  )
96
 
97
  iface.launch(share=True)