Team Ai
Apppublic

teamqa/UI-Test_Case_Generator-MLLM

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py98 linesDownload Raw Back to root
1import gradio as gr2import cv23from PIL import Image4import google.generativeai as genai5import os6from dotenv import load_dotenv7 8load_dotenv()9api_key = os.getenv('api_key')10genai.configure(api_key=api_key)11 12def extract_frames(video_path, fps=30):13    frames = []14    cap = cv2.VideoCapture(video_path)15 16    if not cap.isOpened():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 = 025    success, frame = cap.read()26 27    while success:28        if frame_count % frame_interval == 0:29            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)30            pil_image = Image.fromarray(frame)31            frames.append(pil_image)32        frame_count += 133        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 frames41 42def generate_prompt(button_list):43    button_descriptions = ", ".join(button_list)44    return (45        f"Generate structured test cases for the following UI button(s): {button_descriptions}.\n\n"46        "Functionality\n"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"54        "Automated Testing Tools\n"55        "- Recommended tools for testing.\n\n"56        "Format the output clearly for easy readability, using bullet points for key details."57    )58 59def 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 frame67 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_text82 83    except Exception as e:84        return f"Error occurred: {str(e)}"85 86iface = gr.Interface(87    fn=process_video,88    inputs=[89        gr.File(label="Upload Video", type="binary"),90        gr.Textbox(label="Buttons/Functions to Test (comma-separated)", placeholder="e.g., booking, cancel"),91    ],92    outputs="text",93    title="Video-to-Test Case Generator with Google Gemini",94    description="Upload a video and specify the buttons you want to test. The tool will generate structured test cases."95)96 97iface.launch(share=True)98