Spaces:
Sleeping
Sleeping
File size: 1,829 Bytes
7bafe72 f0fbc32 05b2720 7bafe72 2262eb2 f0fbc32 7bafe72 f0fbc32 7bafe72 f0fbc32 7bafe72 f0fbc32 7bafe72 f0fbc32 7bafe72 f0fbc32 7bafe72 8760ebe 7bafe72 f0fbc32 7bafe72 f0fbc32 662d23c 7bafe72 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# app.py
import gradio as gr
from transformers import pipeline
import json
import os
def analyze_lyrics():
try:
# Инициализируем классификатор
classifier = pipeline("sentiment-analysis", model="blanchefort/rubert-base-cased-sentiment")
# Путь к файлу в пространстве Spaces
# Файл должен находиться в корневой директории вашего Space
file_path = os.path.join(os.getcwd(), "test.txt")
print(f"Пытаемся прочитать файл: {file_path}")
print(f"Файл существует: {os.path.exists(file_path)}")
if not os.path.exists(file_path):
return "Файл не найден"
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
print(f"Прочитано символов: {len(text)}")
results = []
lines = [line.strip() for line in text.split('\n') if line.strip()]
for line in lines:
prediction = classifier(line)
results.append({
'text': line,
'emotion': prediction[0]['label'],
'score': float(prediction[0]['score'])
})
return json.dumps(results, ensure_ascii=False, indent=2)
except Exception as e:
return f"Ошибка: {str(e)}"
# Создаем интерфейс
demo = gr.Interface(
fn=analyze_lyrics,
inputs=None, # Без входных параметров
outputs=gr.JSON(),
title="Анализ эмоций в тексте песни",
description="Нажмите кнопку для анализа файла test.txt"
)
demo.launch() |