rmayormartins commited on
Commit
f029287
1 Parent(s): a9558a5

Adicionando arquivos app.py e requirements.txt

Browse files
Files changed (2) hide show
  1. app.py +300 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from zipfile import ZipFile, BadZipFile
3
+ import tempfile
4
+ import os
5
+ import re
6
+ import pandas as pd
7
+ import collections
8
+ import json
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"]
22
+ social_patterns = ["ContactPicker", "EmailPicker", "PhoneCall", "PhoneNumberPicker", "Texting", "Twitter"]
23
+ storage_patterns = ["File", "CloudDB", "DataFile", "Spreadsheet", "FusiontablesControl", "TinyDB", "TinyWebDB"]
24
+ connectivity_patterns = ["BluetoothClient", "ActivityStarter", "Serial", "BluetoothServer", "Web"]
25
+
26
+
27
+ def extract_components_using_regex(scm_content):
28
+ pattern = r'"\$Type":"(.*?)"'
29
+ components = re.findall(pattern, scm_content)
30
+ if 'roboflow' in scm_content.lower():
31
+ components.append("Using Roboflow")
32
+ return components
33
+
34
+
35
+ def extract_category_components(components, patterns):
36
+ category_components = []
37
+ for component in components:
38
+ for pattern in patterns:
39
+ if component.startswith(pattern):
40
+ category_components.append(component)
41
+ return category_components
42
+
43
+
44
+ def extract_extensions_from_aia(file_path: str):
45
+ extensions = []
46
+ with ZipFile(file_path, 'r') as zip_ref:
47
+ for file_path in zip_ref.namelist():
48
+ if file_path.endswith('components.json') and 'assets/external_comps/' in file_path:
49
+ with zip_ref.open(file_path) as file:
50
+ components_json_content = file.read().decode('utf-8', errors='ignore')
51
+ components_data = json.loads(components_json_content)
52
+ for component in components_data:
53
+ extension_type = component.get("type", "")
54
+ if extension_type:
55
+ extensions.append(extension_type)
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):
63
+ scm_files = glob.glob(f"{temp_dir}/src/appinventor/*/*/*.scm")
64
+ for scm_file in scm_files:
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:
75
+ app_name_match = re.search(pattern, content)
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:
109
+ app_name = app_name_match.group(1)
110
+
111
+ app_version_match = re.match(r'versionname=(.*)', line)
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)
119
+ print(f"Nome do App encontrado nos arquivos .scm: {app_name}")
120
+
121
+ # ...
122
+
123
+
124
+ return {
125
+ 'timestamp': timestamp,
126
+ 'app_name': app_name,
127
+ 'app_version': app_version,
128
+ 'authURL': authURL
129
+ }
130
+
131
+
132
+
133
+
134
+
135
+ def extract_ai_components(components):
136
+ ai_components = []
137
+ for component in components:
138
+ for pattern in ai_patterns:
139
+ if '*' in pattern and component.startswith(pattern[:-1]):
140
+ ai_components.append(component)
141
+ elif component == pattern:
142
+ ai_components.append(component)
143
+ if "roboflow" in ' '.join(components).lower():
144
+ ai_components.append("Using Roboflow")
145
+ return ai_components
146
+
147
+ def extract_media_files(file_path: str):
148
+ media_files = []
149
+ with ZipFile(file_path, 'r') as zip_ref:
150
+ for file_path in zip_ref.namelist():
151
+ if 'assets/' in file_path and not file_path.endswith('/'):
152
+ media_files.append(os.path.basename(file_path))
153
+ return media_files
154
+
155
+ def list_components_in_aia_file(file_path):
156
+ results_df = pd.DataFrame(columns=[
157
+ 'aia_file', 'project_info', 'components', 'IA components', 'screens', 'operators',
158
+ 'variables', 'events', 'extensions', 'Media',
159
+ 'Drawing and Animation', 'Maps', 'Sensors', 'Social', 'Storage', 'Connectivity'])
160
+ # ...
161
+ pd.set_option('display.max_colwidth', None)
162
+ file_name = os.path.basename(file_path)
163
+ # ... restante do código ...
164
+
165
+ components_list = []
166
+ number_of_screens = 0
167
+ operators_count = 0
168
+ variables_count = 0
169
+ events_count = 0 # Adicione esta variável para contar os eventos
170
+ # Extrair arquivos de mídia
171
+ media_files = extract_media_files(file_path)
172
+ media_summary = ', '.join(media_files)
173
+ # Extracting project information
174
+ project_info = extract_project_info_from_properties(file_path)
175
+ project_info_str = f"Timestamp: {project_info['timestamp']}, App Name: {project_info['app_name']}, Version: {project_info['app_version']}, AuthURL: {project_info['authURL']}"
176
+
177
+
178
+ with tempfile.TemporaryDirectory() as temp_dir:
179
+ with ZipFile(file_path, 'r') as zip_ref:
180
+ zip_ref.extractall(temp_dir)
181
+ scm_files = glob.glob(temp_dir + '/src/appinventor/*/*/*.scm')
182
+ bky_files = glob.glob(temp_dir + '/src/appinventor/*/*/*.bky') # Esta linha define bky_files
183
+
184
+ number_of_screens = len(scm_files)
185
+ for scm_file in scm_files:
186
+ with open(scm_file, 'r', encoding='utf-8', errors='ignore') as file:
187
+ content = file.read()
188
+ components = extract_components_using_regex(content)
189
+ components_list.extend(components)
190
+ operators_count += len(re.findall(r'[+\-*/<>!=&|]', content))
191
+ variables_count += len(re.findall(r'"\$Name":"(.*?)"', content))
192
+
193
+ # Extração dos componentes de cada categoria
194
+ drawing_and_animation_summary = ', '.join(extract_category_components(components_list, drawing_and_animation_patterns))
195
+ maps_summary = ', '.join(extract_category_components(components_list, maps_patterns))
196
+ sensors_summary = ', '.join(extract_category_components(components_list, sensors_patterns))
197
+ social_summary = ', '.join(extract_category_components(components_list, social_patterns))
198
+ storage_summary = ', '.join(extract_category_components(components_list, storage_patterns))
199
+ connectivity_summary = ', '.join(extract_category_components(components_list, connectivity_patterns))
200
+
201
+
202
+ # Adicione este bloco para contar os eventos nos arquivos .bky
203
+ # Extracting extensions from .bky files
204
+ extensions_list = []
205
+ extensions_list = extract_extensions_from_aia(file_path)
206
+
207
+ for bky_file in bky_files:
208
+ with open(bky_file, 'r', encoding='utf-8', errors='ignore') as file:
209
+ bky_content = file.read()
210
+ events_count += count_events_in_bky_file(bky_content)
211
+ #extensions_list.extend(extract_extensions_from_bky(bky_content))
212
+
213
+
214
+ # Creating a summary of the extracted extensions
215
+ extensions_summary = ', '.join(list(set(extensions_list)))
216
+
217
+ components_count = collections.Counter(components_list)
218
+ components_summary = [f'{comp} ({count} x)' if count > 1 else comp for comp, count in components_count.items()]
219
+ ai_components_summary = extract_ai_components(components_list)
220
+ results_df = results_df.append({
221
+ 'aia_file': file_name,
222
+ 'project_info': project_info_str,
223
+ 'components': ', '.join(components_summary),
224
+ 'IA components': ', '.join(ai_components_summary),
225
+ 'screens': number_of_screens,
226
+ 'operators': operators_count,
227
+ 'variables': variables_count,
228
+ 'events': events_count,
229
+ 'extensions': extensions_summary,
230
+ 'Media': media_summary,
231
+ 'Drawing and Animation': drawing_and_animation_summary,
232
+ 'Maps': maps_summary,
233
+ 'Sensors': sensors_summary,
234
+ 'Social': social_summary,
235
+ 'Storage': storage_summary,
236
+ 'Connectivity': connectivity_summary
237
+ }, ignore_index=True)
238
+
239
+ return results_df
240
+
241
+ # Define a altura máxima para o output e habilite a rolagem
242
+ # Adicione a variável de estilo CSS ao início do seu código, fora da função analyze_aia
243
+ output_style = """
244
+ <style>
245
+ .output-container {
246
+ max-height: 500px; /* Ajuste a altura máxima conforme necessário */
247
+ overflow: auto; /* Isso permite a rolagem vertical e horizontal se necessário */
248
+ display: block; /* Isso garante que o container seja renderizado abaixo do botão submit */
249
+ }
250
+ .output-container table {
251
+ width: 100%; /* Isso faz com que a tabela utilize toda a largura do container */
252
+ border-collapse: collapse;
253
+ }
254
+ .output-container th, .output-container td {
255
+ border: 1px solid #ddd; /* Isso adiciona bordas às células para melhor visualização */
256
+ text-align: left;
257
+ padding: 8px;
258
+ }
259
+ </style>
260
+ """
261
+
262
+ # Esta será a função que a interface do Gradio irá chamar
263
+ def analyze_aia(uploaded_file):
264
+ try:
265
+ # Obtendo o caminho do arquivo a partir do objeto uploaded_file
266
+ file_path = uploaded_file.name if hasattr(uploaded_file, 'name') else None
267
+
268
+ if file_path and os.path.exists(file_path):
269
+ with ZipFile(file_path, 'r') as zip_ref:
270
+ with tempfile.TemporaryDirectory() as temp_dir:
271
+ zip_ref.extractall(temp_dir)
272
+ results_df = list_components_in_aia_file(file_path)
273
+ # Inclua o estilo CSS na resposta HTML
274
+ html_result = results_df.to_html(escape=False, classes="output-html")
275
+ return output_style + f'<div class="output-container">{html_result}</div>'
276
+
277
+
278
+ else:
279
+ return output_style + "Não foi possível localizar o arquivo .aia."
280
+
281
+ except BadZipFile:
282
+ return output_style + "Falha ao abrir o arquivo .aia como um arquivo zip. Ele pode estar corrompido ou não é um arquivo .aia válido."
283
+
284
+ except Exception as e:
285
+ return output_style + f"Erro ao processar o arquivo: {str(e)}"
286
+
287
+ iface = gr.Interface(
288
+ fn=analyze_aia,
289
+ inputs=gr.File(),
290
+ outputs=gr.HTML(),
291
+ title="AIA-Scope",
292
+ description="Upload an .aia file to analyze its components.",
293
+ live=False # Isso garante que o botão Submit seja necessário
294
+ )
295
+
296
+ if __name__ == "__main__":
297
+ iface.launch(debug=True)
298
+
299
+
300
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ pandas
3
+ glob2 # se você estiver usando glob2