music_text / app.py
mechtnet's picture
Update app.py
7bafe72 verified
raw
history blame
1.83 kB
# 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()