Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -7,6 +7,7 @@ from io import BytesIO
|
|
7 |
from starlette.middleware import Middleware
|
8 |
from starlette.middleware.cors import CORSMiddleware
|
9 |
from pdf2image import convert_from_bytes
|
|
|
10 |
|
11 |
app = FastAPI()
|
12 |
|
@@ -154,28 +155,29 @@ async def test_classify_text(text: str = Form(...)):
|
|
154 |
except Exception as e:
|
155 |
return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)
|
156 |
|
157 |
-
|
158 |
@app.post("/transcribe_and_match/", description="Transcribe audio and match responses to form fields.")
|
159 |
async def transcribe_and_match(
|
160 |
file: UploadFile = File(...),
|
161 |
field_data: str = Form(...)
|
162 |
):
|
163 |
-
"""
|
164 |
-
Transcribe audio and match it to form fields.
|
165 |
-
:param file: The uploaded audio file.
|
166 |
-
:param field_data: A JSON string that contains form field information (field names and IDs).
|
167 |
-
"""
|
168 |
try:
|
169 |
-
# Step 1: Read and
|
170 |
contents = await file.read()
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
transcription_text = transcription_result['text']
|
173 |
|
174 |
# Step 2: Parse the field_data (which contains field names/IDs)
|
175 |
-
# Example: [{"field_id": "name_field", "field_label": "Name"}, {"field_id": "email_field", "field_label": "Email"}]
|
176 |
import json
|
177 |
fields = json.loads(field_data)
|
178 |
-
|
179 |
# Step 3: Find the matching field for the transcription
|
180 |
field_matches = {}
|
181 |
|
@@ -196,6 +198,7 @@ async def transcribe_and_match(
|
|
196 |
except Exception as e:
|
197 |
return JSONResponse(content=f"Error processing audio or matching fields: {str(e)}", status_code=500)
|
198 |
|
|
|
199 |
# Set up CORS middleware
|
200 |
origins = ["*"] # or specify your list of allowed origins
|
201 |
app.add_middleware(
|
|
|
7 |
from starlette.middleware import Middleware
|
8 |
from starlette.middleware.cors import CORSMiddleware
|
9 |
from pdf2image import convert_from_bytes
|
10 |
+
from pydub import AudioSegment
|
11 |
|
12 |
app = FastAPI()
|
13 |
|
|
|
155 |
except Exception as e:
|
156 |
return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)
|
157 |
|
|
|
158 |
@app.post("/transcribe_and_match/", description="Transcribe audio and match responses to form fields.")
|
159 |
async def transcribe_and_match(
|
160 |
file: UploadFile = File(...),
|
161 |
field_data: str = Form(...)
|
162 |
):
|
|
|
|
|
|
|
|
|
|
|
163 |
try:
|
164 |
+
# Step 1: Read and convert the audio file
|
165 |
contents = await file.read()
|
166 |
+
audio = AudioSegment.from_file(BytesIO(contents))
|
167 |
+
|
168 |
+
# Optionally convert to wav if needed
|
169 |
+
wav_io = BytesIO()
|
170 |
+
audio.export(wav_io, format="wav")
|
171 |
+
wav_io.seek(0)
|
172 |
+
|
173 |
+
# Transcribe the WAV audio file
|
174 |
+
transcription_result = nlp_speech_to_text(wav_io)
|
175 |
transcription_text = transcription_result['text']
|
176 |
|
177 |
# Step 2: Parse the field_data (which contains field names/IDs)
|
|
|
178 |
import json
|
179 |
fields = json.loads(field_data)
|
180 |
+
|
181 |
# Step 3: Find the matching field for the transcription
|
182 |
field_matches = {}
|
183 |
|
|
|
198 |
except Exception as e:
|
199 |
return JSONResponse(content=f"Error processing audio or matching fields: {str(e)}", status_code=500)
|
200 |
|
201 |
+
|
202 |
# Set up CORS middleware
|
203 |
origins = ["*"] # or specify your list of allowed origins
|
204 |
app.add_middleware(
|