themanas021 commited on
Commit
3768bd5
·
verified ·
1 Parent(s): 68cc217

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import base64
3
+ from PIL import Image
4
+ import torch
5
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
6
+ from qwen_vl_utils import process_vision_info
7
+
8
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
9
+ "Qwen/Qwen2-VL-2B-Instruct",
10
+ torch_dtype="auto",
11
+ device_map="auto",
12
+ )
13
+
14
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
15
+
16
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
+ model.to(device)
18
+
19
+ # Function to encode images into base64
20
+ def encode_images(image_paths):
21
+ base64_images = []
22
+ for image_path in image_paths:
23
+ with open(image_path, "rb") as image_file:
24
+ base64_image = base64.b64encode(image_file.read()).decode("utf-8")
25
+ base64_images.append(f"data:image/jpeg;base64,{base64_image}")
26
+ return base64_images
27
+
28
+ # Function to resize images to a uniform shape
29
+ def resize_images(image_paths, target_size=(224, 224)):
30
+ resized_images = []
31
+ for image_path in image_paths:
32
+ img = Image.open(image_path)
33
+ img_resized = img.resize(target_size) # Resize image to target size
34
+ resized_images.append(img_resized)
35
+ return resized_images
36
+
37
+ def generate_testing_instructions(images, context):
38
+ # Resize all images to a uniform shape (e.g., 224x224)
39
+ resized_images = resize_images(images)
40
+
41
+ # Encode resized images to base64
42
+ base64_images = encode_images(images)
43
+
44
+ # Prepare messages with the base64-encoded images
45
+ messages = [
46
+ {
47
+ "role": "user",
48
+ "content": [
49
+ {"type": "image_url", "image_url": {"url": base64_image}},
50
+ {"type": "text", "text": '''
51
+ You're a helpful assistant that creates comprehensive testing instructions. Each test case should include the following details:
52
+
53
+ - **Description**: A brief explanation of the feature being tested.
54
+ - **Pre-conditions**: The required setup or state of the app before beginning the test.
55
+ - **Testing Steps**: A clear, step-by-step guide for performing the test, including any user interactions or actions.
56
+ - **Expected Result**: The outcome you should observe if the feature is functioning correctly.
57
+
58
+ Please demonstrate your approach using the following features of a mobile app:
59
+
60
+ 1. **Source, Destination, and Date Selection**: The user selects their departure location, destination, and the travel date.
61
+ 2. **Bus Selection**: Displays a list of available buses, allowing the user to choose a specific one.
62
+ 3. **Seat Selection**: Enables the user to select seats on the chosen bus.
63
+ 4. **Pick-up and Drop-off Point Selection**: Allows the user to specify the starting point and final stop for the trip.
64
+ 5. **Offers**: Showcases available promotions and discounts.
65
+ 6. **Filters**: Options to filter buses by time, price, or other preferences.
66
+ 7. **Bus Information**: Provides details about the selected bus, including amenities, photos, and reviews.
67
+
68
+ '''},
69
+ {"type": "text", "text": context}
70
+ ]
71
+ }
72
+ for base64_image in base64_images
73
+ ]
74
+
75
+ text_prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
76
+
77
+ # Create input tensors
78
+ inputs = processor(
79
+ text=[text_prompt],
80
+ images=resized_images, # Use resized images for model input
81
+ padding=True,
82
+ return_tensors="pt"
83
+ )
84
+
85
+ # Move tensors to GPU if available
86
+ inputs = inputs.to(device)
87
+
88
+ # Generate output
89
+ output_ids = model.generate(**inputs, max_new_tokens=1024)
90
+
91
+ generated_ids = [
92
+ output_ids[len(input_ids):]
93
+ for input_ids, output_ids in zip(inputs.input_ids, output_ids)
94
+ ]
95
+
96
+ # Decode the output text
97
+ output_text = processor.batch_decode(
98
+ generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
99
+ )
100
+
101
+ return output_text
102
+
103
+ # Create the Gradio interface
104
+ with gr.Blocks() as demo:
105
+ gr.Markdown("## App Testing Instructions Generator")
106
+
107
+ with gr.Row():
108
+ context = gr.Textbox(label="Optional Context", placeholder="Add any specific instructions or questions...")
109
+
110
+ image_upload = gr.File(label="Upload Screenshots (PNG or JPG)", type="filepath", file_count="multiple")
111
+
112
+ output = gr.Textbox(label="Generated Testing Instructions", placeholder="The instructions will appear here...")
113
+
114
+ button = gr.Button("Describe Testing Instructions")
115
+
116
+ # Action on button click
117
+ button.click(
118
+ generate_testing_instructions,
119
+ inputs=[image_upload, context],
120
+ outputs=output
121
+ )
122
+
123
+ # Launch the Gradio app
124
+ demo.launch(debug=True)