gpt4 book ai didi

python - 如何通过超链接或Web界面从中运行.py文件?

转载 作者:行者123 更新时间:2023-12-03 17:25:09 29 4
gpt4 key购买 nike

我是python Web界面的新手。我有一个将语音转换为文本然后进行情感分析的应用程序。

import json
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource
import threading
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import pandas as pd
authenticator = IAMAuthenticator('rXXX')
service = SpeechToTextV1(authenticator=authenticator)
service.set_service_url('https://api.us-east.speech-to-text.watson.cloud.ibm.com')

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))

# This is the name of the file u need to change below
with open(join(dirname('__file__'), 'Swearing_Service.wav'),
'rb') as audio_file:
# print(json.dumps(
output = service.recognize(
audio=audio_file,
speaker_labels=True,
content_type='audio/wav',
#timestamps=True,
#word_confidence=True,
model='en-US_NarrowbandModel',
continuous=True).get_result(),
indent=2
df0 = pd.DataFrame([i for elts in output for alts in elts['results'] for i in alts['alternatives']])
list(df0.columns)
list(df1.columns)
df0 = df0.drop(["timestamps"], axis=1)
df1 = df1.drop(["final"], axis=1)
df1 = df1.drop(['confidence'],axis=1)
test3 = pd.concat([df0, df1], axis=1)
#sentiment
transcript = test3['transcript']
transcript = transcript.dropna()
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
text = transcript
scores = []
for txt in text:
vs = analyzer.polarity_scores(txt)
scores.append(vs)
data = pd.DataFrame(text, columns= ['Text'])
data2 = pd.DataFrame(scores)
final_dataset= pd.concat([data,data2], axis=1)
test4 = pd.concat([test3,final_dataset], axis=1)
test4 = test4.drop(['Text'],axis=1)
test4.rename(columns={'neg':'Negative'},
inplace=True)
test4.rename(columns={'pos':'Positive'},
inplace=True)
test4.rename(columns={'neu':'Neutral'},
inplace=True)

# This is the name of the output csv file u need to change below
test4.to_csv("sentiment.csv")

我想为某人创建一个Web界面,只需单击一个按钮并在他们上传wav文件后获取csv输出,而不是运行代码。我该怎么做呢?

最佳答案

以下是一般流程:

  • 您将在Flask应用程序中定义一个API端点。例如,/speech-to-text
  • 单击按钮后,您将通过wav文件数据(通过JavaScript/JQuery)向该端点发送AJAX请求
  • 在Flask应用程序中,您将通过运行脚本(从脚本中为此创建函数)来检索“语音数据”并将其转换为csv文件。
  • 将带有csv数据的响应发送到您的AJAX请求,该请求将在您的JavaScript脚本中接收。
  • 用户将可以下载此文件。

  • 相关链接:
  • Accessing Request Data in Flask
  • APIs with JSON in Flask
  • Handling File Uploads in Flask
  • AJAX Introduction
  • 关于python - 如何通过超链接或Web界面从中运行.py文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960066/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com