mavihsrr's picture
Update app.py
a8db576 verified
import gradio as gr
import cv2
from PIL import Image
import google.generativeai as genai
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv('api_key')
genai.configure(api_key=api_key)
def extract_frames(video_path, fps=30):
frames = []
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise ValueError("Error: Unable to open video file.")
original_fps = cap.get(cv2.CAP_PROP_FPS)
if original_fps <= 0:
raise ValueError("Error: Unable to retrieve valid FPS from video file.")
frame_interval = max(1, int(original_fps // fps))
frame_count = 0
success, frame = cap.read()
while success:
if frame_count % frame_interval == 0:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(frame)
frames.append(pil_image)
frame_count += 1
success, frame = cap.read()
cap.release()
if len(frames) == 0:
raise ValueError("Error: No frames extracted from the video.")
return frames
def generate_prompt(button_list):
button_descriptions = ", ".join(button_list)
return (
f"Generate structured test cases for the following UI button(s): {button_descriptions}.\n\n"
"Functionality\n"
"- What the button is supposed to do.\n\n"
"Preconditions\n"
"- Any requirements before interaction.\n\n"
"Test Steps\n"
"- Concise steps for executing the test.\n\n"
"Expected Results\n"
"- What the expected outcome should be.\n\n"
"Automated Testing Tools\n"
"- Recommended tools for testing.\n\n"
"Format the output clearly for easy readability, using bullet points for key details."
)
def process_video(video_file, buttons_to_test):
try:
video_path = "uploaded_video.mp4"
with open(video_path, "wb") as f:
f.write(video_file)
frames = extract_frames(video_path, fps=15)
frames_to_pass = frames[::10] # use every 10th frame
button_list = [button.strip() for button in buttons_to_test.split(",")]
prompt = generate_prompt(button_list)
frame_prompts = [frame for frame in frames_to_pass]
response = genai.GenerativeModel(model_name="gemini-1.5-pro-latest").generate_content([prompt] + frame_prompts)
output_text = response.text if hasattr(response, 'text') else "Error: Invalid response from the model."
output_text = output_text.replace("Functionality", "📋 Functionality")
output_text = output_text.replace("Preconditions", "🔍 Preconditions")
output_text = output_text.replace("Test Steps", "📝 Test Steps")
output_text = output_text.replace("Expected Results", "✅ Expected Results")
output_text = output_text.replace("Automated Testing Tools", "🛠️ Automated Testing Tools")
return output_text
except Exception as e:
return f"Error occurred: {str(e)}"
iface = gr.Interface(
fn=process_video,
inputs=[
gr.File(label="Upload Video", type="binary"),
gr.Textbox(label="Buttons/Functions to Test (comma-separated)", placeholder="e.g., booking, cancel"),
],
outputs="text",
title="Video-to-Test Case Generator with Google Gemini",
description="Upload a video and specify the buttons you want to test. The tool will generate structured test cases."
)
iface.launch(share=True)