Spaces:
Runtime error
Runtime error
Commit
·
16e821f
1
Parent(s):
be1c45c
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, jsonify
|
| 2 |
+
|
| 3 |
+
app = Flask(__name__)
|
| 4 |
+
|
| 5 |
+
# Replace with your AssemblyAI API key
|
| 6 |
+
ASSEMBLYAI_API_KEY = "67883cd71f0d4a58a27a34e058f0d924"
|
| 7 |
+
|
| 8 |
+
# URL of the file to transcribe
|
| 9 |
+
FILE_URL = "/content/call.mp3"
|
| 10 |
+
|
| 11 |
+
# You can also transcribe a local file by passing in a file path
|
| 12 |
+
# FILE_URL = './path/to/file.mp3'
|
| 13 |
+
|
| 14 |
+
@app.route('/analyze', methods=['GET'])
|
| 15 |
+
def analyze():
|
| 16 |
+
# Transcribe audio to text
|
| 17 |
+
transcriber = aai.Transcriber(api_key=ASSEMBLYAI_API_KEY)
|
| 18 |
+
transcript = transcriber.transcribe(FILE_URL)
|
| 19 |
+
text = transcript.text
|
| 20 |
+
|
| 21 |
+
# Perform sentiment analysis
|
| 22 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 23 |
+
sentiment = sentiment_analyzer(text)
|
| 24 |
+
|
| 25 |
+
# Perform emotion analysis
|
| 26 |
+
emotion_analyzer = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
| 27 |
+
emotions = emotion_analyzer(text)
|
| 28 |
+
|
| 29 |
+
# Format results
|
| 30 |
+
result = {
|
| 31 |
+
"transcript": text,
|
| 32 |
+
"sentiment": {
|
| 33 |
+
"score": sentiment[0]['score'],
|
| 34 |
+
"label": sentiment[0]['label']
|
| 35 |
+
},
|
| 36 |
+
"emotion": [{
|
| 37 |
+
"label": emotion['label'],
|
| 38 |
+
"score": emotion['score']
|
| 39 |
+
} for emotion in emotions[0]]
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
return render_template('analyze.html', result=result)
|
| 43 |
+
|
| 44 |
+
if __name__ == '__main__':
|
| 45 |
+
app.run(debug=True)
|