Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -312,4 +312,53 @@ def analyze_files(file_paths):
|
|
| 312 |
result_files = []
|
| 313 |
for file_path in file_paths:
|
| 314 |
try:
|
| 315 |
-
with open(file_path, 'r', encoding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
result_files = []
|
| 313 |
for file_path in file_paths:
|
| 314 |
try:
|
| 315 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 316 |
+
text = f.read()
|
| 317 |
+
file_name = os.path.basename(file_path)
|
| 318 |
+
result_path = analyze_text_file(text, file_name)
|
| 319 |
+
if result_path:
|
| 320 |
+
result_files.append(result_path)
|
| 321 |
+
except Exception as e:
|
| 322 |
+
print(f"Ошибка при обработке файла {file_name}: {str(e)}")
|
| 323 |
+
return result_files
|
| 324 |
+
|
| 325 |
+
def analyze_text_file(text, original_file_name):
|
| 326 |
+
try:
|
| 327 |
+
analyzer = TextBlockAnalyzer()
|
| 328 |
+
analysis_results = analyzer.analyze_text(text)
|
| 329 |
+
output = {
|
| 330 |
+
'file_name': original_file_name,
|
| 331 |
+
'analysis': analysis_results
|
| 332 |
+
}
|
| 333 |
+
output_file_name = f"{os.path.splitext(original_file_name)[0]}_analysis.json"
|
| 334 |
+
output_file_path = os.path.join(os.getcwd(), output_file_name)
|
| 335 |
+
with open(output_file_path, 'w', encoding='utf-8') as f:
|
| 336 |
+
json.dump(output, f, ensure_ascii=False, indent=2)
|
| 337 |
+
return output_file_path
|
| 338 |
+
except Exception as e:
|
| 339 |
+
print(f"Ошибка при обработке файла: {str(e)}")
|
| 340 |
+
return None
|
| 341 |
+
|
| 342 |
+
def analyze_emotions(text_block):
|
| 343 |
+
found_emotions = []
|
| 344 |
+
lower_text = text_block.lower()
|
| 345 |
+
for emotion, markers in EMOTIONAL_MARKERS.items():
|
| 346 |
+
for marker in markers:
|
| 347 |
+
if marker in lower_text:
|
| 348 |
+
found_emotions.append({
|
| 349 |
+
'label': emotion,
|
| 350 |
+
'score': 0.7
|
| 351 |
+
})
|
| 352 |
+
break
|
| 353 |
+
return found_emotions if found_emotions else None
|
| 354 |
+
|
| 355 |
+
demo = gr.Interface(
|
| 356 |
+
fn=analyze_files,
|
| 357 |
+
inputs=gr.File(label="Загрузите .txt файлы", file_count="multiple", type="filepath"),
|
| 358 |
+
outputs=gr.File(label="Результаты анализа"),
|
| 359 |
+
title="Анализ эмоций в тексте",
|
| 360 |
+
description="Загрузите текстовые файлы для анализа эмоций и настроения."
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
if __name__ == "__main__":
|
| 364 |
+
demo.launch()
|