Spaces:
Sleeping
Sleeping
Commit
·
2da2916
1
Parent(s):
0fa9f83
update app.py1
Browse files
app.py
CHANGED
@@ -9,14 +9,13 @@ import json
|
|
9 |
import glob
|
10 |
from io import BytesIO
|
11 |
|
12 |
-
|
13 |
-
|
14 |
ai_patterns = [
|
15 |
"PIC*", "PersonalImageClassifier*", "Look*", "LookExtension*", "ChatBot", "ImageBot", "TMIC","TeachableMachine*",
|
16 |
"TeachableMachineImageClassifier*", "SpeechRecognizer*", "FaceExtension*","Pose*","Posenet","PosenetExtension", "Eliza*", "Alexa*"
|
17 |
]
|
18 |
|
19 |
-
|
20 |
drawing_and_animation_patterns = ["Ball", "Canvas", "ImageSprite"]
|
21 |
maps_patterns = ["Map", "Marker", "Circle", "FeatureCollection", "LineString", "Navigation","Polygon", "Retangle" ]
|
22 |
sensors_patterns = ["AccelerometerSensor", "BarcodeScanner", "Barometer", "Clock", "GyroscopeSensor", "Hygrometer", "LightSensor", "LocationSensor", "MagneticFieldSensor", "NearField","OrientationSensor", "ProximitySensor","Thermometer", "Pedometer"]
|
@@ -57,7 +56,7 @@ def extract_extensions_from_aia(file_path: str):
|
|
57 |
return extensions
|
58 |
|
59 |
def count_events_in_bky_file(bky_content):
|
60 |
-
|
61 |
return bky_content.count('<block type="component_event"')
|
62 |
|
63 |
def extract_app_name_from_scm_files(temp_dir):
|
@@ -66,10 +65,10 @@ def extract_app_name_from_scm_files(temp_dir):
|
|
66 |
with open(scm_file, 'r', encoding='utf-8', errors='ignore') as file:
|
67 |
content = file.read()
|
68 |
|
69 |
-
|
70 |
regex_patterns = [
|
71 |
r'"AppName"\s*:\s*"([^"]+)"',
|
72 |
-
r'"AppName"\s*:\s*\'([^\']+)\''
|
73 |
]
|
74 |
|
75 |
for pattern in regex_patterns:
|
@@ -77,33 +76,33 @@ def extract_app_name_from_scm_files(temp_dir):
|
|
77 |
if app_name_match:
|
78 |
return app_name_match.group(1)
|
79 |
|
80 |
-
|
81 |
print(f"Aviso: Nome do aplicativo não encontrado no diretório {temp_dir}")
|
82 |
return "N/A"
|
83 |
|
84 |
def extract_project_info_from_properties(file_path):
|
85 |
-
|
86 |
timestamp = "N/A"
|
87 |
app_name = "N/A"
|
88 |
app_version = "N/A"
|
89 |
authURL = "ai2.appinventor.mit.edu"
|
90 |
|
91 |
-
|
92 |
with tempfile.TemporaryDirectory() as temp_dir:
|
93 |
with ZipFile(file_path, 'r') as zip_ref:
|
94 |
zip_ref.extractall(temp_dir)
|
95 |
-
|
96 |
project_properties_file_path = 'youngandroidproject/project.properties'
|
97 |
|
98 |
-
|
99 |
if project_properties_file_path in zip_ref.namelist():
|
100 |
with zip_ref.open(project_properties_file_path) as file:
|
101 |
project_properties_lines = file.read().decode('utf-8').splitlines()
|
102 |
|
103 |
-
|
104 |
timestamp = project_properties_lines[1] if len(project_properties_lines) > 1 else "N/A"
|
105 |
|
106 |
-
|
107 |
for line in project_properties_lines:
|
108 |
app_name_match = re.match(r'aname=(.*)', line)
|
109 |
if app_name_match:
|
@@ -113,7 +112,7 @@ def extract_project_info_from_properties(file_path):
|
|
113 |
if app_version_match:
|
114 |
app_version = app_version_match.group(1)
|
115 |
|
116 |
-
|
117 |
if app_name == "N/A":
|
118 |
print("O campo App Name não foi encontrado em project.properties. Tentando encontrar em arquivos .scm...")
|
119 |
app_name = extract_app_name_from_scm_files(temp_dir)
|
@@ -154,26 +153,26 @@ def extract_media_files(file_path: str):
|
|
154 |
return media_files
|
155 |
|
156 |
def list_components_in_aia_file(file_path):
|
157 |
-
|
158 |
results_df = pd.DataFrame(columns=[
|
159 |
'aia_file', 'project_info', 'components', 'IA components', 'screens', 'operators',
|
160 |
'variables', 'events', 'extensions', 'Media',
|
161 |
'Drawing and Animation', 'Maps', 'Sensors', 'Social', 'Storage', 'Connectivity'])
|
162 |
|
163 |
-
|
164 |
pd.set_option('display.max_colwidth', None)
|
165 |
file_name = os.path.basename(file_path)
|
166 |
-
|
167 |
|
168 |
components_list = []
|
169 |
number_of_screens = 0
|
170 |
operators_count = 0
|
171 |
variables_count = 0
|
172 |
-
events_count = 0
|
173 |
-
|
174 |
media_files = extract_media_files(file_path)
|
175 |
media_summary = ', '.join(media_files)
|
176 |
-
|
177 |
project_info = extract_project_info_from_properties(file_path)
|
178 |
project_info_str = f"Timestamp: {project_info['timestamp']}, App Name: {project_info['app_name']}, Version: {project_info['app_version']}, AuthURL: {project_info['authURL']}"
|
179 |
|
@@ -182,7 +181,7 @@ def list_components_in_aia_file(file_path):
|
|
182 |
with ZipFile(file_path, 'r') as zip_ref:
|
183 |
zip_ref.extractall(temp_dir)
|
184 |
scm_files = glob.glob(temp_dir + '/src/appinventor/*/*/*.scm')
|
185 |
-
bky_files = glob.glob(temp_dir + '/src/appinventor/*/*/*.bky')
|
186 |
|
187 |
number_of_screens = len(scm_files)
|
188 |
for scm_file in scm_files:
|
@@ -193,7 +192,7 @@ def list_components_in_aia_file(file_path):
|
|
193 |
operators_count += len(re.findall(r'[+\-*/<>!=&|]', content))
|
194 |
variables_count += len(re.findall(r'"\$Name":"(.*?)"', content))
|
195 |
|
196 |
-
|
197 |
drawing_and_animation_summary = ', '.join(extract_category_components(components_list, drawing_and_animation_patterns))
|
198 |
maps_summary = ', '.join(extract_category_components(components_list, maps_patterns))
|
199 |
sensors_summary = ', '.join(extract_category_components(components_list, sensors_patterns))
|
@@ -202,8 +201,8 @@ def list_components_in_aia_file(file_path):
|
|
202 |
connectivity_summary = ', '.join(extract_category_components(components_list, connectivity_patterns))
|
203 |
|
204 |
|
205 |
-
|
206 |
-
|
207 |
extensions_list = []
|
208 |
extensions_list = extract_extensions_from_aia(file_path)
|
209 |
|
@@ -211,10 +210,10 @@ def list_components_in_aia_file(file_path):
|
|
211 |
with open(bky_file, 'r', encoding='utf-8', errors='ignore') as file:
|
212 |
bky_content = file.read()
|
213 |
events_count += count_events_in_bky_file(bky_content)
|
214 |
-
|
215 |
|
216 |
|
217 |
-
|
218 |
extensions_summary = ', '.join(list(set(extensions_list)))
|
219 |
|
220 |
components_count = collections.Counter(components_list)
|
@@ -239,31 +238,33 @@ def list_components_in_aia_file(file_path):
|
|
239 |
'Connectivity': connectivity_summary
|
240 |
}])
|
241 |
|
242 |
-
|
243 |
results_df = pd.concat([results_df, new_row], ignore_index=True)
|
244 |
return results_df
|
245 |
#
|
246 |
|
247 |
-
|
|
|
248 |
output_style = """
|
249 |
<style>
|
250 |
.output-container {
|
251 |
-
max-height: 500px;
|
252 |
-
overflow: auto;
|
253 |
-
display: block;
|
254 |
}
|
255 |
.output-container table {
|
256 |
-
width: 100%;
|
257 |
border-collapse: collapse;
|
258 |
}
|
259 |
.output-container th, .output-container td {
|
260 |
-
border: 1px solid #ddd;
|
261 |
text-align: left;
|
262 |
padding: 8px;
|
263 |
}
|
264 |
</style>
|
265 |
"""
|
266 |
|
|
|
267 |
def analyze_aia(uploaded_files):
|
268 |
all_results = []
|
269 |
for uploaded_file in uploaded_files:
|
@@ -282,21 +283,20 @@ def analyze_aia(uploaded_files):
|
|
282 |
except Exception as e:
|
283 |
all_results.append(f"Erro ao processar o arquivo {file_path}: {str(e)}")
|
284 |
|
285 |
-
#
|
286 |
combined_results_df = pd.concat(all_results, ignore_index=True)
|
287 |
html_result = combined_results_df.to_html(escape=False, classes="output-html")
|
288 |
return output_style + f'<div class="output-container">{html_result}</div>'
|
289 |
|
290 |
iface = gr.Interface(
|
291 |
fn=analyze_aia,
|
292 |
-
inputs=gr.
|
293 |
outputs=gr.HTML(),
|
294 |
title="AIA-Scope",
|
295 |
description="Upload .aia (or multiples .aia) files to analyze/dissect their components. An .aia file from MIT App Inventor is a project file format that contains all the necessary information for an App Inventor project.",
|
296 |
-
live=False
|
297 |
)
|
298 |
|
299 |
-
|
300 |
if __name__ == "__main__":
|
301 |
iface.launch(debug=True)
|
302 |
|
|
|
9 |
import glob
|
10 |
from io import BytesIO
|
11 |
|
12 |
+
# Padrões de IA
|
|
|
13 |
ai_patterns = [
|
14 |
"PIC*", "PersonalImageClassifier*", "Look*", "LookExtension*", "ChatBot", "ImageBot", "TMIC","TeachableMachine*",
|
15 |
"TeachableMachineImageClassifier*", "SpeechRecognizer*", "FaceExtension*","Pose*","Posenet","PosenetExtension", "Eliza*", "Alexa*"
|
16 |
]
|
17 |
|
18 |
+
# Padrões para cada categoria
|
19 |
drawing_and_animation_patterns = ["Ball", "Canvas", "ImageSprite"]
|
20 |
maps_patterns = ["Map", "Marker", "Circle", "FeatureCollection", "LineString", "Navigation","Polygon", "Retangle" ]
|
21 |
sensors_patterns = ["AccelerometerSensor", "BarcodeScanner", "Barometer", "Clock", "GyroscopeSensor", "Hygrometer", "LightSensor", "LocationSensor", "MagneticFieldSensor", "NearField","OrientationSensor", "ProximitySensor","Thermometer", "Pedometer"]
|
|
|
56 |
return extensions
|
57 |
|
58 |
def count_events_in_bky_file(bky_content):
|
59 |
+
# Counting the number of occurrences of the "component_event" blocks
|
60 |
return bky_content.count('<block type="component_event"')
|
61 |
|
62 |
def extract_app_name_from_scm_files(temp_dir):
|
|
|
65 |
with open(scm_file, 'r', encoding='utf-8', errors='ignore') as file:
|
66 |
content = file.read()
|
67 |
|
68 |
+
# Tenta várias expressões regulares para encontrar o nome do aplicativo
|
69 |
regex_patterns = [
|
70 |
r'"AppName"\s*:\s*"([^"]+)"',
|
71 |
+
r'"AppName"\s*:\s*\'([^\']+)\'' # Exemplo de outra possível expressão regular
|
72 |
]
|
73 |
|
74 |
for pattern in regex_patterns:
|
|
|
76 |
if app_name_match:
|
77 |
return app_name_match.group(1)
|
78 |
|
79 |
+
# Log de erros ou avisos
|
80 |
print(f"Aviso: Nome do aplicativo não encontrado no diretório {temp_dir}")
|
81 |
return "N/A"
|
82 |
|
83 |
def extract_project_info_from_properties(file_path):
|
84 |
+
# Initialize variables
|
85 |
timestamp = "N/A"
|
86 |
app_name = "N/A"
|
87 |
app_version = "N/A"
|
88 |
authURL = "ai2.appinventor.mit.edu"
|
89 |
|
90 |
+
# Create a temporary directory
|
91 |
with tempfile.TemporaryDirectory() as temp_dir:
|
92 |
with ZipFile(file_path, 'r') as zip_ref:
|
93 |
zip_ref.extractall(temp_dir)
|
94 |
+
# Define the path to the 'project.properties' file
|
95 |
project_properties_file_path = 'youngandroidproject/project.properties'
|
96 |
|
97 |
+
# Check if the file exists in the .aia file
|
98 |
if project_properties_file_path in zip_ref.namelist():
|
99 |
with zip_ref.open(project_properties_file_path) as file:
|
100 |
project_properties_lines = file.read().decode('utf-8').splitlines()
|
101 |
|
102 |
+
# Extracting timestamp
|
103 |
timestamp = project_properties_lines[1] if len(project_properties_lines) > 1 else "N/A"
|
104 |
|
105 |
+
# Extracting app name and version using regular expressions
|
106 |
for line in project_properties_lines:
|
107 |
app_name_match = re.match(r'aname=(.*)', line)
|
108 |
if app_name_match:
|
|
|
112 |
if app_version_match:
|
113 |
app_version = app_version_match.group(1)
|
114 |
|
115 |
+
# Complementary method for extracting the app name from .scm files
|
116 |
if app_name == "N/A":
|
117 |
print("O campo App Name não foi encontrado em project.properties. Tentando encontrar em arquivos .scm...")
|
118 |
app_name = extract_app_name_from_scm_files(temp_dir)
|
|
|
153 |
return media_files
|
154 |
|
155 |
def list_components_in_aia_file(file_path):
|
156 |
+
#
|
157 |
results_df = pd.DataFrame(columns=[
|
158 |
'aia_file', 'project_info', 'components', 'IA components', 'screens', 'operators',
|
159 |
'variables', 'events', 'extensions', 'Media',
|
160 |
'Drawing and Animation', 'Maps', 'Sensors', 'Social', 'Storage', 'Connectivity'])
|
161 |
|
162 |
+
|
163 |
pd.set_option('display.max_colwidth', None)
|
164 |
file_name = os.path.basename(file_path)
|
165 |
+
#
|
166 |
|
167 |
components_list = []
|
168 |
number_of_screens = 0
|
169 |
operators_count = 0
|
170 |
variables_count = 0
|
171 |
+
events_count = 0 #
|
172 |
+
#
|
173 |
media_files = extract_media_files(file_path)
|
174 |
media_summary = ', '.join(media_files)
|
175 |
+
#
|
176 |
project_info = extract_project_info_from_properties(file_path)
|
177 |
project_info_str = f"Timestamp: {project_info['timestamp']}, App Name: {project_info['app_name']}, Version: {project_info['app_version']}, AuthURL: {project_info['authURL']}"
|
178 |
|
|
|
181 |
with ZipFile(file_path, 'r') as zip_ref:
|
182 |
zip_ref.extractall(temp_dir)
|
183 |
scm_files = glob.glob(temp_dir + '/src/appinventor/*/*/*.scm')
|
184 |
+
bky_files = glob.glob(temp_dir + '/src/appinventor/*/*/*.bky') # Esta linha define bky_files
|
185 |
|
186 |
number_of_screens = len(scm_files)
|
187 |
for scm_file in scm_files:
|
|
|
192 |
operators_count += len(re.findall(r'[+\-*/<>!=&|]', content))
|
193 |
variables_count += len(re.findall(r'"\$Name":"(.*?)"', content))
|
194 |
|
195 |
+
#
|
196 |
drawing_and_animation_summary = ', '.join(extract_category_components(components_list, drawing_and_animation_patterns))
|
197 |
maps_summary = ', '.join(extract_category_components(components_list, maps_patterns))
|
198 |
sensors_summary = ', '.join(extract_category_components(components_list, sensors_patterns))
|
|
|
201 |
connectivity_summary = ', '.join(extract_category_components(components_list, connectivity_patterns))
|
202 |
|
203 |
|
204 |
+
#
|
205 |
+
#
|
206 |
extensions_list = []
|
207 |
extensions_list = extract_extensions_from_aia(file_path)
|
208 |
|
|
|
210 |
with open(bky_file, 'r', encoding='utf-8', errors='ignore') as file:
|
211 |
bky_content = file.read()
|
212 |
events_count += count_events_in_bky_file(bky_content)
|
213 |
+
#
|
214 |
|
215 |
|
216 |
+
#
|
217 |
extensions_summary = ', '.join(list(set(extensions_list)))
|
218 |
|
219 |
components_count = collections.Counter(components_list)
|
|
|
238 |
'Connectivity': connectivity_summary
|
239 |
}])
|
240 |
|
241 |
+
#
|
242 |
results_df = pd.concat([results_df, new_row], ignore_index=True)
|
243 |
return results_df
|
244 |
#
|
245 |
|
246 |
+
#
|
247 |
+
#
|
248 |
output_style = """
|
249 |
<style>
|
250 |
.output-container {
|
251 |
+
max-height: 500px; /* a */
|
252 |
+
overflow: auto; /* a */
|
253 |
+
display: block; /* a */
|
254 |
}
|
255 |
.output-container table {
|
256 |
+
width: 100%; /* a */
|
257 |
border-collapse: collapse;
|
258 |
}
|
259 |
.output-container th, .output-container td {
|
260 |
+
border: 1px solid #ddd; /* a */
|
261 |
text-align: left;
|
262 |
padding: 8px;
|
263 |
}
|
264 |
</style>
|
265 |
"""
|
266 |
|
267 |
+
#
|
268 |
def analyze_aia(uploaded_files):
|
269 |
all_results = []
|
270 |
for uploaded_file in uploaded_files:
|
|
|
283 |
except Exception as e:
|
284 |
all_results.append(f"Erro ao processar o arquivo {file_path}: {str(e)}")
|
285 |
|
286 |
+
#
|
287 |
combined_results_df = pd.concat(all_results, ignore_index=True)
|
288 |
html_result = combined_results_df.to_html(escape=False, classes="output-html")
|
289 |
return output_style + f'<div class="output-container">{html_result}</div>'
|
290 |
|
291 |
iface = gr.Interface(
|
292 |
fn=analyze_aia,
|
293 |
+
inputs=gr.Files(label="Upload .aia Files"), #
|
294 |
outputs=gr.HTML(),
|
295 |
title="AIA-Scope",
|
296 |
description="Upload .aia (or multiples .aia) files to analyze/dissect their components. An .aia file from MIT App Inventor is a project file format that contains all the necessary information for an App Inventor project.",
|
297 |
+
live=False
|
298 |
)
|
299 |
|
|
|
300 |
if __name__ == "__main__":
|
301 |
iface.launch(debug=True)
|
302 |
|