Reggie commited on
Commit
6c7ce7e
1 Parent(s): 6aa994f

Drag and drop

Browse files
Files changed (3) hide show
  1. app.py +33 -1
  2. requirements.txt +1 -1
  3. templates/index.html +71 -2
app.py CHANGED
@@ -12,13 +12,23 @@ import os, time, sys
12
  from datetime import datetime as dt
13
  from datetime import timedelta
14
  from datetime import timezone
 
 
15
 
16
  app = Flask(__name__)
17
 
 
 
 
 
 
18
  # Initialize Qdrant Client and other required settings
19
  qdrant_api_key = os.environ.get("qdrant_api_key")
20
  qdrant_url = os.environ.get("qdrant_url")
21
 
 
 
 
22
  client = QdrantClient(url=qdrant_url, port=443, api_key=qdrant_api_key, prefer_grpc=False)
23
 
24
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -70,7 +80,7 @@ def search():
70
  else: results = client.search(collection_name=collection_name, query_vector=(qvector, sq), with_payload=True, limit=100)
71
  print('SEARCH TIME: ', time.time() - timh)
72
 
73
- print(results[0].payload['text'].split('\n'))
74
  try:
75
  results = [{"text": x.payload['text'], "date": str(int(x.payload['date'])), "id": x.id} for x in results] # Implement your Qdrant search here
76
  return jsonify(results)
@@ -84,5 +94,27 @@ def delete_joke():
84
  client.delete(collection_name="jks", points_selector=models.PointIdsList(points=[int(joke_id)],),)
85
  return jsonify({"deleted": True})
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  if __name__ == "__main__":
88
  app.run(host="0.0.0.0", debug=True, port=7860)
 
12
  from datetime import datetime as dt
13
  from datetime import timedelta
14
  from datetime import timezone
15
+ from faster_whisper import WhisperModel
16
+ import io
17
 
18
  app = Flask(__name__)
19
 
20
+ # Faster Whisper setup
21
+ # model_size = 'small'
22
+ beamsize = 2
23
+ wmodel = WhisperModel("guillaumekln/faster-whisper-small", device="cpu", compute_type="int8")
24
+
25
  # Initialize Qdrant Client and other required settings
26
  qdrant_api_key = os.environ.get("qdrant_api_key")
27
  qdrant_url = os.environ.get("qdrant_url")
28
 
29
+ qdrant_api_key = "WaGH94-bo_CzlxTNHFjBAGPvWRhbsWEKUKbMz6YQtYt4oTD1ZXTvwg"
30
+ qdrant_url = "https://c9bee7c7-2bf3-4e1b-8838-2f6f23372ab5.us-east-1-0.aws.cloud.qdrant.io"
31
+
32
  client = QdrantClient(url=qdrant_url, port=443, api_key=qdrant_api_key, prefer_grpc=False)
33
 
34
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
80
  else: results = client.search(collection_name=collection_name, query_vector=(qvector, sq), with_payload=True, limit=100)
81
  print('SEARCH TIME: ', time.time() - timh)
82
 
83
+ # print(results[0].payload['text'].split('\n'))
84
  try:
85
  results = [{"text": x.payload['text'], "date": str(int(x.payload['date'])), "id": x.id} for x in results] # Implement your Qdrant search here
86
  return jsonify(results)
 
94
  client.delete(collection_name="jks", points_selector=models.PointIdsList(points=[int(joke_id)],),)
95
  return jsonify({"deleted": True})
96
 
97
+ @app.route("/whisper_transcribe", methods=["POST"])
98
+ def whisper_transcribe():
99
+ if 'audio' not in request.files: return jsonify({'error': 'No file provided'}), 400
100
+
101
+ audio_file = request.files['audio']
102
+ allowed_extensions = {'mp3', 'wav', 'ogg', 'm4v'}
103
+ if not (audio_file and audio_file.filename.lower().split('.')[-1] in allowed_extensions): return jsonify({'error': 'Invalid file format'}), 400
104
+
105
+ print('Transcribing audio')
106
+ audio_bytes = audio_file.read()
107
+ audio_file = io.BytesIO(audio_bytes)
108
+
109
+ segments, info = wmodel.transcribe(audio_file, beam_size=beamsize) # beamsize is 2.
110
+ text = ''
111
+ starttime = time.time()
112
+ for segment in segments:
113
+ text += segment.text
114
+ print('Time to transcribe:', time.time() - starttime, 'seconds')
115
+
116
+ return jsonify({'transcription': text})
117
+
118
+
119
  if __name__ == "__main__":
120
  app.run(host="0.0.0.0", debug=True, port=7860)
requirements.txt CHANGED
@@ -2,4 +2,4 @@ transformers
2
  torch
3
  qdrant-client
4
  flask
5
- Werkzeug
 
2
  torch
3
  qdrant-client
4
  flask
5
+ faster-whisper
templates/index.html CHANGED
@@ -30,7 +30,7 @@
30
  border: none;
31
  padding: 10px 20px;
32
  cursor: pointer;
33
- margin-left: 10px; /* Add margin to separate the query bar and button */
34
  }
35
 
36
  #results {
@@ -51,6 +51,17 @@
51
  cursor: pointer;
52
  margin: 0 10px;
53
  }
 
 
 
 
 
 
 
 
 
 
 
54
  </style>
55
  </head>
56
  <body>
@@ -60,6 +71,11 @@
60
  <button id="search">Search</button>
61
  </div>
62
  <div id="results"></div>
 
 
 
 
 
63
 
64
  <script>
65
  $(document).ready(function () {
@@ -67,6 +83,7 @@
67
  var query = $("#query").val();
68
 
69
  $.post("/search", { query: query }, function (data) {
 
70
  var results = $("#results");
71
  results.empty();
72
 
@@ -96,14 +113,66 @@
96
  });
97
  }
98
 
 
 
 
 
 
 
 
 
 
 
99
  $("#search").on("click", function () {
100
  performSearch();
 
101
  });
102
 
103
  $("#query").on("keydown", function (event) {
104
  if (event.key === "Enter") {
105
- event.preventDefault(); // Prevent form submission
106
  performSearch();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  }
108
  });
109
  });
 
30
  border: none;
31
  padding: 10px 20px;
32
  cursor: pointer;
33
+ margin-left: 10px;
34
  }
35
 
36
  #results {
 
51
  cursor: pointer;
52
  margin: 0 10px;
53
  }
54
+
55
+ #drag-drop-container {
56
+ border: 2px dashed #4c72af;
57
+ padding: 20px;
58
+ text-align: center;
59
+ cursor: pointer;
60
+ }
61
+
62
+ .deleted-text {
63
+ color: red;
64
+ }
65
  </style>
66
  </head>
67
  <body>
 
71
  <button id="search">Search</button>
72
  </div>
73
  <div id="results"></div>
74
+
75
+ <!-- Drag and drop area -->
76
+ <div id="drag-drop-container">
77
+ Drag & Drop Audio File (mp3, wav, ogg, m4v) Here
78
+ </div>
79
 
80
  <script>
81
  $(document).ready(function () {
 
83
  var query = $("#query").val();
84
 
85
  $.post("/search", { query: query }, function (data) {
86
+ // Handle search results
87
  var results = $("#results");
88
  results.empty();
89
 
 
113
  });
114
  }
115
 
116
+ function displayTranscriptionResult(transcription) {
117
+ // Display the transcription result
118
+ var results = $("#results");
119
+ results.empty();
120
+ results.append("<div class='result'>" + "<p>" + transcription + "</p>" + "</div>");
121
+ // Hide the "Transcribing..." message
122
+ $("#drag-drop-container").text("Drag & Drop Audio File (mp3, wav, ogg, m4v) Here");
123
+ $("#drag-drop-container").css("border-color", "#4c72af");
124
+ }
125
+
126
  $("#search").on("click", function () {
127
  performSearch();
128
+ $("#drag-drop-container").css("display", "none"); // Hide drag & drop area
129
  });
130
 
131
  $("#query").on("keydown", function (event) {
132
  if (event.key === "Enter") {
133
+ event.preventDefault();
134
  performSearch();
135
+ $("#drag-drop-container").css("display", "none"); // Hide drag & drop area
136
+ }
137
+ });
138
+
139
+ // Drag and drop functionality
140
+ $("#drag-drop-container").on("dragover", function (e) {
141
+ e.preventDefault();
142
+ $(this).css("border", "2px dashed #ff0000");
143
+ });
144
+
145
+ $("#drag-drop-container").on("dragleave", function (e) {
146
+ e.preventDefault();
147
+ $(this).css("border", "2px dashed #4c72af");
148
+ });
149
+
150
+ $("#drag-drop-container").on("drop", function (e) {
151
+ e.preventDefault();
152
+ $(this).css("border", "2px dashed #4c72af");
153
+
154
+ var files = e.originalEvent.dataTransfer.files;
155
+ if (files.length > 0) {
156
+ // Display "Transcribing..." message
157
+ $("#drag-drop-container").text("Transcribing the audio file...");
158
+ $("#drag-drop-container").css("border-color", "#ccc");
159
+ // Assume there's only one file for simplicity
160
+ var file = files[0];
161
+
162
+ var formData = new FormData();
163
+ formData.append("audio", file);
164
+
165
+ $.ajax({
166
+ url: "/whisper_transcribe",
167
+ type: "POST",
168
+ data: formData,
169
+ processData: false,
170
+ contentType: false,
171
+ success: function (data) {
172
+ // Handle transcription result
173
+ displayTranscriptionResult(data.transcription);
174
+ },
175
+ });
176
  }
177
  });
178
  });