- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 R 的新手,并试图了解 Rshiny 以构建 UI。我正在尝试为我的 python 应用程序创建一个 UI,用于转录多个 wav 文件。下面有两个部分,第一个是我的 python 应用程序,第二个是我在 R 中使用 reticulate 来调用我的 transcribe.py 应用程序的 Shiny 应用程序。但由于某种原因,我没有收到任何输出。
我的 Python 应用程序运行良好,不需要代码审查。但是,Rshiny 应用程序没有正确执行 Python 应用程序以产生所需的结果。目标是让用户从 UI 转录文件并决定他们是否要下载 csv。
我有一个用于转录文件的 python 应用程序,名为 transcribe.py-
import os
import json
import time
# import threading
from pathlib import Path
import concurrent.futures
# from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
# Replace with your api key.
my_api_key = "abc123"
# You can add a directory path to Path() if you want to run
# the project from a different folder at some point.
directory = Path().absolute()
authenticator = IAMAuthenticator(my_api_key)
service = SpeechToTextV1(authenticator=authenticator)
service.set_service_url('https://api.us-east.speech-to-text.watson.cloud.ibm.com')
# I used this URL.
# service.set_service_url('https://stream.watsonplatform.net/speech-to-text/api')
models = service.list_models().get_result()
#print(json.dumps(models, indent=2))
model = service.get_model('en-US_BroadbandModel').get_result()
#print(json.dumps(model, indent=2))
# get data to a csv
########################RUN THIS PART SECOND#####################################
def process_data(json_data, output_path):
print(f"Processing: {output_path.stem}")
cols = ["transcript", "confidence"]
dfdata = [[t[cols[0]], t[cols[1]]] for r in json_data.get('results') for t in r.get("alternatives")]
df0 = pd.DataFrame(data = dfdata, columns = cols)
df1 = pd.DataFrame(json_data.get("speaker_labels")).drop(["final", "confidence"], axis=1)
# test3 = pd.concat([df0, df1], axis=1)
test3 = pd.merge(df0, df1, left_index = True, right_index = True)
# sentiment
print(f"Getting sentiment for: {output_path.stem}")
transcript = test3["transcript"]
transcript.dropna(inplace=True)
analyzer = SentimentIntensityAnalyzer()
text = transcript
scores = [analyzer.polarity_scores(txt) for txt in text]
# data = pd.DataFrame(text, columns = ["Text"])
data = transcript.to_frame(name="Text")
data2 = pd.DataFrame(scores)
# final_dataset= pd.concat([data, data2], axis=1)
final_dataset = pd.merge(data, data2, left_index = True, right_index = True)
# test4 = pd.concat([test3, final_dataset], axis=1)
test4 = pd.merge(test3, final_dataset, left_index = True, right_index = True)
test4.drop("Text", axis=1, inplace=True)
test4.rename(columns = {
"neg": "Negative",
"pos": "Positive",
"neu": "Neutral",
}, inplace=True)
# This is the name of the output csv file
test4.to_csv(output_path, index = False)
def process_audio_file(filename, output_type = "csv"):
audio_file_path = directory.joinpath(filename)
# Update output path to consider `output_type` parameter.
out_path = directory.joinpath(f"{audio_file_path.stem}.{output_type}")
print(f"Current file: '{filename}'")
with open(audio_file_path, "rb") as audio_file:
data = service.recognize(
audio = audio_file,
speaker_labels = True,
content_type = "audio/wav",
inactivity_timeout = -1,
model = "en-US_NarrowbandModel",
continuous = True,
).get_result()
print(f"Speech-to-text complete for: '{filename}'")
# Return data and output path as collection.
return [data, out_path]
def main():
print("Running main()...")
# Default num. workers == min(32, os.cpu_count() + 4)
n_workers = os.cpu_count() + 2
# Create generator for all .wav files in folder (and subfolders).
file_gen = directory.glob("**/*.wav")
with concurrent.futures.ThreadPoolExecutor(max_workers = n_workers) as executor:
futures = {executor.submit(process_audio_file, f) for f in file_gen}
for future in concurrent.futures.as_completed(futures):
pkg = future.result()
process_data(*pkg)
if __name__ == "__main__":
print(f"Program to process audio files has started.")
t_start = time.perf_counter()
main()
t_stop = time.perf_counter()
print(f"Done! Processing completed in {t_stop - t_start} seconds.")
在 Rstudio 中,我尝试过 -
library(shiny)
library(reticulate) # for reading Python code
library(dplyr)
library(stringr)
library(formattable) # for adding color to tables
library(shinybusy) # for busy bar
library(DT) # for dataTableOutput
use_python("/usr/lib/python3")
ui <- fluidPage(
add_busy_bar(color = "#5d98ff"),
fileInput("wavFile", "SELECT .WAV FILE", accept = ".wav"),
uiOutput("downloadData"),
dataTableOutput("transcript"),
)
R.Server 文件
server <- function(input, output) {
# .WAV File Selector ------------------------------------------------------
file <- reactive({
file <- input$wavFile # Get file from user input
gsub("\\\\","/",file$datapath) # Access the file path. Convert back slashes to forward slashes.
})
# Transcribe and Clean ----------------------------------------------------
transcript <- reactive({
req(input$wavFile) # Require a file before proceeding
source_python('transcribe.py') # Load the Python function # COMMENT LINE OUT WHEN TESTING NON-TRANSCRIPTION FUNCTIONALITY
transcript <- data.frame(transcribe(file())) # Transcribe the file # COMMENT LINE OUT WHEN TESTING NON-TRANSCRIPTION FUNCTIONALITY
# load('transcript.rdata') # Loads a dummy transcript # UNCOMMENT LINE OUT WHEN TESTING NON-TRANSCRIPTION FUNCTIONALITY
transcript$transcript <- unlist(transcript$transcript) # Transcript field comes in as a list. Unlist it.
transcript <- transcript[which(!(is.na(transcript$confidence))),] # Remove empty lines
names(transcript) <- str_to_title(names(transcript)) # Capitalize column headers
transcript # Return the transcript
})
# Use a server-side download button ---------------------------------------
# ...so that the download button only appears after transcription
output$downloadData <- renderUI({
req(transcript())
downloadButton("handleDownload","Download CSV")
})
output$handleDownload <- downloadHandler(
filename = function() {
paste('Transcript ',Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(transcript(), file, row.names = FALSE)
}
)
# Transcript table --------------------------------------------------------
output$transcript <- renderDataTable({
as.datatable(formattable(
transcript() %>%
select(Transcript,
Confidence,
Negative,
Positive
),
list(Confidence = color_tile('#ffffff','#a2b3c8'),
Negative = color_tile('#ffffff', '#e74446'),
Positive = color_tile('#ffffff', "#499650")
)
), rownames = FALSE, options =list(paging = FALSE)
)
})
# END ---------------------------------------------------------------------
}
最佳答案
在 Shiny 中,您需要在 python 脚本中正确传递参数。一个简单的方法是在 python 脚本中定义一个函数并以 Shiny 的方式调用该函数。
这是您修改后的 python 脚本(编辑了 process_data 函数并添加了 run_script 函数)-
import os
import json
import time
# import threading
from pathlib import Path
import concurrent.futures
# from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
# Replace with your api key.
my_api_key = "api_key"
# You can add a directory path to Path() if you want to run
# the project from a different folder at some point.
directory = Path().absolute()
authenticator = IAMAuthenticator(my_api_key)
service = SpeechToTextV1(authenticator=authenticator)
service.set_service_url('https://api.us-east.speech-to-text.watson.cloud.ibm.com')
# I used this URL.
# service.set_service_url('https://stream.watsonplatform.net/speech-to-text/api')
models = service.list_models().get_result()
#print(json.dumps(models, indent=2))
model = service.get_model('en-US_BroadbandModel').get_result()
#print(json.dumps(model, indent=2))
# get data to a csv
########################RUN THIS PART SECOND#####################################
def process_data(json_data):
#print(f"Processing: {output_path.stem}")
cols = ["transcript", "confidence"]
dfdata = [[t[cols[0]], t[cols[1]]] for r in json_data.get('results') for t in r.get("alternatives")]
df0 = pd.DataFrame(data = dfdata, columns = cols)
df1 = pd.DataFrame(json_data.get("speaker_labels")).drop(["final", "confidence"], axis=1)
# test3 = pd.concat([df0, df1], axis=1)
test3 = pd.merge(df0, df1, left_index = True, right_index = True)
# sentiment
#print(f"Getting sentiment for: {output_path.stem}")
transcript = test3["transcript"]
transcript.dropna(inplace=True)
analyzer = SentimentIntensityAnalyzer()
text = transcript
scores = [analyzer.polarity_scores(txt) for txt in text]
# data = pd.DataFrame(text, columns = ["Text"])
data = transcript.to_frame(name="Text")
data2 = pd.DataFrame(scores)
# final_dataset= pd.concat([data, data2], axis=1)
final_dataset = pd.merge(data, data2, left_index = True, right_index = True)
# test4 = pd.concat([test3, final_dataset], axis=1)
test4 = pd.merge(test3, final_dataset, left_index = True, right_index = True)
test4.drop("Text", axis=1, inplace=True)
test4.rename(columns = {
"neg": "Negative",
"pos": "Positive",
"neu": "Neutral",
}, inplace=True)
# This is the name of the output csv file
# test4.to_csv(output_path, index = False)
return(test4)
def process_audio_file(filename, output_type = "csv"):
audio_file_path = directory.joinpath(filename)
# Update output path to consider `output_type` parameter.
out_path = directory.joinpath(f"{audio_file_path.stem}.{output_type}")
print(f"Current file: '{filename}'")
with open(audio_file_path, "rb") as audio_file:
data = service.recognize(
audio = audio_file,
speaker_labels = True,
content_type = "audio/wav",
inactivity_timeout = -1,
model = "en-US_NarrowbandModel",
continuous = True,
).get_result()
print(f"Speech-to-text complete for: '{filename}'")
# Return data and output path as collection.
return [data, out_path]
def main():
print("Running main()...")
# Default num. workers == min(32, os.cpu_count() + 4)
n_workers = os.cpu_count() + 2
# Create generator for all .wav files in folder (and subfolders).
file_gen = directory.glob("**/*.wav")
with concurrent.futures.ThreadPoolExecutor(max_workers = n_workers) as executor:
futures = {executor.submit(process_audio_file, f) for f in file_gen}
for future in concurrent.futures.as_completed(futures):
pkg = future.result()
process_data(*pkg)
def run_script (filename):
return(process_data(process_audio_file(filename)[0]))
Shiny 代码
library(shiny)
library(reticulate) # for reading Python code
library(dplyr)
library(stringr)
library(formattable) # for adding color to tables
library(shinybusy) # for busy bar
library(DT) # for dataTableOutput
use_python("C:/Users/ap396/Anaconda3/python")
ui <- fluidPage(
add_busy_bar(color = "#5d98ff"),
fileInput("wavFile", "SELECT .WAV FILE", accept = ".wav",multiple = T),
uiOutput("downloadData"),
dataTableOutput("transcript")
)
server <- function(input, output) {
# .WAV File Selector ------------------------------------------------------
file <- reactive({
req(input$wavFile) # Require a file before proceeding
files <- input$wavFile # Get file from user input
file = NULL
for (i in 1:nrow(files)){
print(file)
file = c(file,gsub("\\\\","/",files$datapath[i])) # Access the file path. Convert back slashes to forward slashes.
}
return(file)
})
# Transcribe and Clean ----------------------------------------------------
source_python('transcribe.py')
transcript <- reactive({
dft= data.frame(NULL)
for(j in 1:length(file())){
t0 = Sys.time()
transcript <- run_script(file()[j]) # Transcribe the file # COMMENT LINE OUT WHEN TESTING NON-TRANSCRIPTION FUNCTIONALITY
t1 = Sys.time() - t0
transcript$File = j; transcript$Time = t1
dft = rbind(dft,transcript)
}
return(dft) # Return the transcript
})
# Use a server-side download button ---------------------------------------
# ...so that the download button only appears after transcription
output$downloadData <- renderUI({
req(transcript())
downloadButton("handleDownload","Download CSV")
})
output$handleDownload <- downloadHandler(
filename = function() {
paste('Transcript ',Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(transcript(), file, row.names = FALSE)
}
)
# Transcript table --------------------------------------------------------
output$transcript <- renderDataTable({
as.datatable(formattable(
transcript() %>%
select(File,
Time,
transcript,
confidence,
Negative,
Positive
),
list(Confidence = color_tile('#ffffff','#a2b3c8'),
Negative = color_tile('#ffffff', '#e74446'),
Positive = color_tile('#ffffff', "#499650")
)
), rownames = FALSE, options =list(paging = FALSE)
)
})
# END ---------------------------------------------------------------------
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
请注意, Shiny 的下载仅适用于网络浏览器,因此您必须在网络浏览器中打开应用程序
关于python - 如何将我的 python 应用程序制作/转换为 R Shiny 应用程序?这是一个脑筋急转弯!无法在 R 中找到 UI 需要什么更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66769294/
情况我想使用 ui-date 在我的应用程序中设置/编辑日期。我使用最新稳定版本的 Angular、Angular-UI、JQuery-UI 等。 问题一旦使用日期选择器选择了日期,我的模型中的日期将
编辑: jQuery UI 可选择小部件内置了一个回调,stop,我需要知道如何以编程方式触发此事件。 (措辞不佳)我已将事件监听器附加到 jQuery UI Selectable Widget 。如
我正在尝试建立一个下一个JS与尾风用户界面提供的反应组件的网络应用程序。顺风用户界面是在幕后使用无头用户界面。。默认情况下,Next JS将构建服务器端组件,除非您在页面顶部添加“使用客户端”。不幸的
我正在尝试建立一个下一个JS与尾风用户界面提供的反应组件的网络应用程序。顺风用户界面是在幕后使用无头用户界面。。默认情况下,Next JS将构建服务器端组件,除非您在页面顶部添加“使用客户端”。不幸的
我正在尝试应用这个 SlickGrid 示例: http://mleibman.github.com/SlickGrid/examples/example4-model.html 到我自己的网络项目。
我想整理我的 Schemas为我的实体类生成,DTO 类位于 Springdoc ui . 我可以对 tags 进行排序和 operations通过以下配置 yml文件,但我的模式不是按排序顺序排列的
有谁知道阻止 ui-sref 重新加载状态的方法吗? 我无法通过“$stateChangeStart”事件执行此操作,因为 ui-sref 仅更改参数而不更改状态名称。 我的左边是书单,左边是书的详细
我正在 jquery ui 对话框中使用 jquery ui 自动完成小部件。当我输入搜索文本时,文本框缩进(ui-autocomplet-loading)但不显示任何建议。 var availabl
我正在尝试将 Kendo UI MVVM 框架与 Kendo UI 拖放机制结合使用;但我很难找到如何将数据从 draggable 对象中删除。 我的代码是这样的...... var viewMode
Kendo UI Web 和 Kendo UI Core 之间有什么区别 https://www.nuget.org/packages/KendoUIWeb http://www.nuget.org/
我正在尝试将 Kendo UI MVVM 框架与 Kendo UI 拖放机制结合使用;但是我很难找到如何从 draggable 对象中删除数据。 我的代码是这样的…… var viewModel =
使用 Angular JS - UI 路由器,我需要从我的父 View project.details 到我的 subview project.details.tasks 进行通信。我的 subvie
KendoUI 版本 2013.3.1119使用 Kendo MVVM 我有一个我构建的颜色选择器,它使用平面颜色选择器和使用调色板的颜色选择器。它们都可以正常运行,但平面颜色选择器的布局已关闭, s
我在非 UI 线程上,我需要创建并显示一个 SaveDialog。但是当我尝试显示它时:.ShowDialog() 我得到: "An unhandled exception of type 'Syst
我正在试验 jquery-ui 并查看和克隆一些示例。在一个示例(自动完成的组合框)中,我看到一个带有 ui-widget 类的 anchor (a) 元素,它与包含的 css 文件中的 .ui-wi
我需要返回一个 UI 列表,我用这个方法: getList(): Observable { return this.httpClient.get("/api/listui").pipe
我有 ui-grids在 angular-ui-tabs ,它们位于 ng-if 中以避免呈现问题。如果有更多数据并且网格进入滚动模式,则单击选项卡时数据会完全消失。我相信这是一个 ui-grids-
这似乎是一个通用的问题,与其他几个 React 开源框架相比,我真的很喜欢 Material ui 的可扩展性。 问题 “@material-ui/core”和“@material-ui/lab”中的
我有一个根页面(index.html),带有侧边栏(“菜单”)和主要内容 div(“主”),因此有两个 ui-view div - 一个称为“菜单”,一个称为“主”。 当主要内容区域有网站列表 (/s
有人在http://jsfiddle.net/hKYWr/上整理了一个很好的 fiddle 。关于使用 angular-ui 和 jqueryui sortable 来获得良好的可排序效果。 如何在两
我是一名优秀的程序员,十分优秀!