File size: 18,311 Bytes
8700a34
52a7d50
d99db10
6bbd3ca
bda7361
c39e604
 
af17670
 
e36dd54
70678a5
b3ae078
4556b98
1fab2aa
76bbd8a
d99db10
 
fbe5c6d
0cd7858
6a96e5e
17a7267
ab46adf
 
836458e
86a0b7a
6bbd3ca
bda7361
 
 
 
 
 
 
 
 
 
0f0bcd2
0e96068
539adb3
686167b
a90d0cf
c14f4d2
02494dd
31d9e37
 
574f9e3
 
bda7361
574f9e3
 
 
 
 
6bbd3ca
574f9e3
6bbd3ca
574f9e3
 
6bbd3ca
 
 
 
a82199b
6bbd3ca
 
c39e604
 
 
bda7361
574f9e3
 
 
bda7361
 
574f9e3
420d3c9
bda7361
 
41d335c
574f9e3
 
41d335c
574f9e3
f198fb3
574f9e3
f8ec4b3
574f9e3
 
0ddbc70
 
574f9e3
 
 
 
 
 
 
0ddbc70
 
 
 
f660b8b
0ddbc70
 
 
 
 
1106695
0ddbc70
 
 
 
 
574f9e3
0ddbc70
 
 
574f9e3
0ddbc70
3302f65
539adb3
 
 
 
 
 
 
 
 
 
 
20f087e
539adb3
26decc6
539adb3
 
 
42a6b89
 
 
 
 
 
 
 
 
 
 
d167323
a90d0cf
 
 
 
0a12a49
11d5e31
 
 
0a12a49
 
 
11d5e31
 
 
0a12a49
 
 
11d5e31
0a12a49
 
a90d0cf
 
42a6b89
1fd89c5
 
cfd8768
1fd89c5
cfd8768
 
fbe5c6d
64e0d3f
fbe5c6d
 
50e0c0e
 
 
fbe5c6d
 
 
 
 
 
d99db10
 
 
64e0d3f
 
 
 
 
d99db10
 
1fab2aa
fbe5c6d
d99db10
 
 
 
70678a5
fbe5c6d
 
 
 
 
 
 
1fd89c5
 
 
 
 
 
cfd8768
fbe5c6d
 
 
 
 
 
 
cfd8768
d99db10
cfd8768
 
1fd89c5
cfd8768
 
 
fbe5c6d
 
cfd8768
22c526e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a4a69
22c526e
 
 
 
 
 
 
 
 
 
8601b67
e2d5a35
 
 
 
ee808b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2d5a35
ee808b2
8601b67
e2d5a35
1d4a335
2c29573
e2d5a35
2c29573
 
8601b67
e2d5a35
ee808b2
 
8601b67
ee808b2
cf71890
e2d5a35
cf71890
e2d5a35
 
 
8601b67
e2d5a35
 
6fe91f4
fe58c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fe91f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31d9e37
1a6d882
ab46adf
31d9e37
 
 
 
 
 
ab46adf
 
 
 
 
 
 
 
fd35e4e
 
1a6d882
 
 
 
 
 
31d9e37
ab46adf
 
 
31d9e37
1a6d882
 
 
 
 
 
 
694da95
 
ab46adf
694da95
f0e6e2e
694da95
 
 
 
 
 
 
 
 
 
 
 
 
 
ab46adf
694da95
ab46adf
694da95
 
 
ab46adf
31d9e37
694da95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31d9e37
 
694da95
ab46adf
31d9e37
694da95
31d9e37
 
694da95
31d9e37
22c526e
36dea63
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import fitz
import io
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.responses import JSONResponse
from transformers import pipeline
from PIL import Image
from io import BytesIO
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
from pdf2image import convert_from_bytes
from pydub import AudioSegment
import numpy as np
import json
import torchaudio
import torch
from pydub import AudioSegment
import speech_recognition as sr
import logging
import asyncio
from concurrent.futures import ThreadPoolExecutor
import re
from pydantic import BaseModel
from typing import List, Dict, Any

app = FastAPI()

# Set up CORS middleware
origins = ["*"]  # or specify your list of allowed origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

nlp_qa = pipeline("document-question-answering", model="jinhybr/OCR-DocVQA-Donut")
nlp_qa_v2 = pipeline("document-question-answering", model="faisalraza/layoutlm-invoices", ignore_mismatched_sizes=True)
nlp_qa_v3 = pipeline("question-answering", model="deepset/roberta-base-squad2")
nlp_classification = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
nlp_classification_v2 = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
nlp_speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
nlp_sequence_classification = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
nlp_main_classification = pipeline("zero-shot-classification", model="roberta-large-mnli")

description = """
## Image-based Document QA
This API performs document question answering using a LayoutLMv2-based model.

### Endpoints:
- **POST /uploadfile/:** Upload an image file to extract text and answer provided questions.
- **POST /pdfQA/:** Provide a PDF file to extract text and answer provided questions.
"""

app = FastAPI(docs_url="/", description=description)

@app.post("/uploadfile/", description="Upload an image file to extract text and answer provided questions.")
async def perform_document_qa(
    file: UploadFile = File(...),
    questions: str = Form(...),
):
    try:
        # Read the uploaded file as bytes
        contents = await file.read()

        # Open the image using PIL
        image = Image.open(BytesIO(contents))

        # Perform document question answering for each question using LayoutLMv2-based model
        answers_dict = {}
        for question in questions.split(','):
            result = nlp_qa(
                image,
                question.strip()
            )

            # Access the 'answer' key from the first item in the result list
            answer = result[0]['answer']

            # Format the question as a string without extra characters
            formatted_question = question.strip("[]")

            answers_dict[formatted_question] = answer

        return answers_dict
    except Exception as e:
        return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)

@app.post("/uploadfilev2/", description="Upload an image file to extract text and answer provided questions.")
async def perform_document_qa(
    file: UploadFile = File(...),
    questions: str = Form(...),
):
    try:
        # Read the uploaded file as bytes
        contents = await file.read()

        # Open the image using PIL
        image = Image.open(BytesIO(contents))

        # Perform document question answering for each question using LayoutLMv2-based model
        answers_dict = {}
        for question in questions.split(','):
            result = nlp_qa_v2(
                image,
                question.strip()
            )

            # Access the 'answer' key from the first item in the result list
            answer = result[0]['answer']

            # Format the question as a string without extra characters
            formatted_question = question.strip("[]")

            answers_dict[formatted_question] = answer

        return answers_dict
    except Exception as e:
        return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)

@app.post("/uploadfilev3/", description="Upload an image file to extract text and answer provided questions.")
async def perform_document_qa(
    context: str = Form(...),
    question: str = Form(...),
):
    try:
        QA_input = {
            'question': question,
            'context': context
        }
        
        res = nlp_qa_v3(QA_input)

        return res['answer']
    except Exception as e:
        return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)

@app.post("/classify/", description="Classify the provided text.")
async def classify_text(text: str = Form(...)):
    try:
        # Perform text classification using the pipeline
        result = nlp_classification(text)

        # Return the classification result
        return result
    except Exception as e:
        return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)

@app.post("/test_classify/", description="Classify the provided text with positive, neutral, or negative sentiment.")
async def test_classify_text(text: str = Form(...)):
    try:
        # Perform text classification using the updated model that returns positive, neutral, or negative
        result = nlp_classification_v2(text)

        # Print the raw label for debugging purposes (can be removed later)
        raw_label = result[0]['label']
        print(f"Raw label from model: {raw_label}")

        # Map the model labels to human-readable format
        label_map = {
            "negative": "Negative",  
            "neutral": "Neutral",   
            "positive": "Positive"   
        }

        # Get the readable label from the map
        formatted_label = label_map.get(raw_label, "Unknown")

        return {"label": formatted_label, "score": result[0]['score']}
    except Exception as e:
        return JSONResponse(content=f"Error classifying text: {str(e)}", status_code=500)

@app.post("/transcribe_and_answer/", description="Transcribe audio and answer provided questions based on the transcription.")
async def transcribe_and_answer(
    file: UploadFile = File(...),
    questions: str = Form(...)
):
    try:
        # Ensure correct file format
        if file.content_type not in ["audio/wav", "audio/mpeg", "audio/mp3", "audio/webm"]:
            raise HTTPException(status_code=400, detail="Unsupported audio format. Please upload a WAV or MP3 file.")

        logging.info(f"Received file type: {file.content_type}")
        logging.info(f"Received questions: {questions}")

        # Convert uploaded file to WAV if needed
        audio_data = await file.read()
        audio_file = io.BytesIO(audio_data)
        
        if file.content_type in ["audio/mpeg", "audio/mp3"]:
            audio = AudioSegment.from_file(audio_file, format="mp3")
            audio_wav = io.BytesIO()
            audio.export(audio_wav, format="wav")
            audio_wav.seek(0)
        elif file.content_type == "audio/webm":
            audio = AudioSegment.from_file(audio_file, format="webm")
            audio_wav = io.BytesIO()
            audio.export(audio_wav, format="wav")
            audio_wav.seek(0)
        else:
            audio_wav = audio_file
        
        # Transcription
        recognizer = sr.Recognizer()
        with sr.AudioFile(audio_wav) as source:
            audio = recognizer.record(source)
            transcription_text = recognizer.recognize_google(audio)

        # Parse questions JSON
        try:
            questions_dict = json.loads(questions)
        except json.JSONDecodeError as e:
            raise HTTPException(status_code=400, detail="Invalid JSON format for questions")

        # Answer each question
        answers_dict = {}
        for key, question in questions_dict.items():
            QA_input = {
                'question': question,
                'context': transcription_text
            }
            
            # Add error handling here for model-based Q&A
            try:
                result = nlp_qa_v3(QA_input)  # Ensure this is defined or imported correctly
                answers_dict[key] = result['answer']
            except Exception as e:
                logging.error(f"Error in question answering model: {e}")
                answers_dict[key] = "Error in answering this question."

        # Return transcription + answers
        return {
            "transcription": transcription_text,
            "answers": answers_dict
        }

    except Exception as e:
        logging.error(f"General error: {e}")
        raise HTTPException(status_code=500, detail="Internal Server Error")

@app.post("/test-transcription/", description="Upload an audio file to test transcription using speech_recognition.")
async def test_transcription(file: UploadFile = File(...)):
    try:
        # Check if the file format is supported
        if file.content_type not in ["audio/wav", "audio/mpeg", "audio/mp3"]:
            raise HTTPException(status_code=400, detail="Unsupported audio format. Please upload a WAV or MP3 file.")

        # Convert uploaded file to WAV if necessary for compatibility with SpeechRecognition
        audio_data = await file.read()
        audio_file = io.BytesIO(audio_data)

        if file.content_type in ["audio/mpeg", "audio/mp3"]:
            # Convert MP3 to WAV
            audio = AudioSegment.from_file(audio_file, format="mp3")
            audio_wav = io.BytesIO()
            audio.export(audio_wav, format="wav")
            audio_wav.seek(0)
        else:
            audio_wav = audio_file

        # Transcribe audio using speech_recognition 
        recognizer = sr.Recognizer()
        with sr.AudioFile(audio_wav) as source:
            audio = recognizer.record(source)
            transcription = recognizer.recognize_google(audio)

        # Return the transcription
        return {"transcription": transcription}

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error during transcription: {str(e)}")

# Define the ThreadPoolExecutor globally to manage asynchronous execution
executor = ThreadPoolExecutor(max_workers=10)

# Predefined classifications
labels = [
    "All Pricing copy quote requested",
    "Change to quote",
    "Change to quote & Status Check",
    "Change to quote (Items missed?)",
    "Confirmation",
    "Copy quote requested",
    "Cost copy quote requested",
    "MRSP copy quote requested",
    "MSRP & All Pricing copy quote requested",
    "MSRP & Cost copy quote requested",
    "No narrative in email",
    "Notes not clear",
    "Retail copy quote requested",
    "Status Check (possibly)"
]

@app.post("/fast_classify/", description="Quickly classify text into predefined categories.")
async def fast_classify_text(statement: str = Form(...)):
    try:
        # Use run_in_executor to handle the synchronous model call asynchronously
        loop = asyncio.get_running_loop()
        result = await loop.run_in_executor(
            executor, 
            lambda: nlp_sequence_classification(statement, labels, multi_label=False)
        )

        # Extract the best label and score
        best_label = result["labels"][0]
        best_score = result["scores"][0]

        return {"classification": best_label, "confidence": best_score}
    except asyncio.TimeoutError:
        # Handle timeout
        return JSONResponse(content="Classification timed out. Try a shorter input or increase timeout.", status_code=504)
    except HTTPException as http_exc:
        # Handle HTTP errors
        return JSONResponse(content=f"HTTP error: {http_exc.detail}", status_code=http_exc.status_code)
    except Exception as e:
        # Handle general errors
        return JSONResponse(content=f"Error in classification pipeline: {str(e)}", status_code=500)

# Predefined classifications
labels = [
    "All Pricing copy quote requested",
    "Change to quote",
    "Change to quote & Status Check",
    "Change to quote (Items missed?)",
    "Confirmation",
    "Copy quote requested",
    "Cost copy quote requested",
    "MRSP copy quote requested",
    "MSRP & All Pricing copy quote requested",
    "MSRP & Cost copy quote requested",
    "No narrative in email",
    "Notes not clear",
    "Retail copy quote requested",
    "Status Check (possibly)"
]

@app.post("/fast_classify_v2/", description="Quickly classify text into predefined categories.")
async def fast_classify_text(statement: str = Form(...)):
    try:
        # Use run_in_executor to handle the synchronous model call asynchronously
        loop = asyncio.get_running_loop()
        result = await loop.run_in_executor(
            executor, 
            lambda: nlp_sequence_classification(statement, labels, multi_label=False)
        )

        # Extract all labels and their scores
        all_labels = result["labels"]
        all_scores = result["scores"]

        # Extract the best label and score
        best_label = all_labels[0]
        best_score = all_scores[0]

        # Prepare the response
        full_response = {
            "classification": best_label,
            "confidence": best_score,
            "all_labels": {label: score for label, score in zip(all_labels, all_scores)}
        }

        return full_response
    except asyncio.TimeoutError:
        # Handle timeout
        return JSONResponse(content="Classification timed out. Try a shorter input or increase timeout.", status_code=504)
    except HTTPException as http_exc:
        # Handle HTTP errors
        return JSONResponse(content=f"HTTP error: {http_exc.detail}", status_code=http_exc.status_code)
    except Exception as e:
        # Handle general errors
        return JSONResponse(content=f"Error in classification pipeline: {str(e)}", status_code=500)

# Labels for main classifications
main_labels = [
    "Change to quote", 
    "Copy quote requested", 
    "Expired Quote", 
    "Notes not clear"
]

# Define a model for the response
class ClassificationResponse(BaseModel):
    classification: str
    sub_classification: str
    confidence: float
    scores: Dict[str, float]

# Keyword dictionaries for overriding classifications
change_to_quote_keywords = ["Per ATP", "Add", "Revised", "Remove", "Advise"]
copy_quote_requested_keywords = ["MSRP", "Send Quote", "Copy", "All pricing", "Retail"]
sub_classification_keywords = {
    "MRSP": ["MSRP"],
    "Direct": ["Direct"],
    "All": ["All pricing"],
    "MRSP & All": ["MSRP", "All pricing"]
}

# Helper function to check for keywords in a case-insensitive way
def check_keywords(statement: str, keywords: List[str]) -> bool:
    return any(re.search(rf"\b{keyword}\b", statement, re.IGNORECASE) for keyword in keywords)

# Function to determine sub-classification based on keywords
def get_sub_classification(statement: str) -> str:
    for sub_label, keywords in sub_classification_keywords.items():
        if all(check_keywords(statement, [keyword]) for keyword in keywords):
            return sub_label
    return "None"  # Default to "None" if no keywords match

@app.post("/classify_with_subcategory/", response_model=ClassificationResponse, description="Classify text into main categories with subcategories.")
async def classify_with_subcategory(statement: str = Form(...)) -> ClassificationResponse:
    try:
        # Check if the statement is empty or "N/A"
        if not statement or statement.strip().lower() == "n/a":
            return ClassificationResponse(
                classification="Notes not clear",
                sub_classification="None",
                confidence=1.0,
                scores={"main": 1.0}
            )

        # Keyword-based classification override
        if check_keywords(statement, change_to_quote_keywords):
            main_best_label = "Change to quote"
            main_best_score = 1.0  # High confidence since it's a direct match
        elif check_keywords(statement, copy_quote_requested_keywords):
            main_best_label = "Copy quote requested"
            main_best_score = 1.0
        else:
            # If no keywords matched, perform the main classification using the model
            loop = asyncio.get_running_loop()
            main_classification_result = await loop.run_in_executor(
                None, 
                lambda: nlp_sequence_classification(statement, main_labels, multi_label=False)
            )

            # Extract the best main classification label and confidence score
            main_best_label = main_classification_result["labels"][0]
            main_best_score = main_classification_result["scores"][0]
        
        # Perform sub-classification only if the main classification is "Copy quote requested"
        if main_best_label == "Copy quote requested":
            best_sub_label = get_sub_classification(statement)
        else:
            best_sub_label = "None"

        # Gather the scores for response
        scores = {"main": main_best_score}
        if best_sub_label != "None":
            scores[best_sub_label] = 1.0  # Assign full confidence to sub-classification matches

        return ClassificationResponse(
            classification=main_best_label,
            sub_classification=best_sub_label,
            confidence=main_best_score,
            scores=scores
        )

    except asyncio.TimeoutError:
        # Handle timeout errors
        return JSONResponse(content="Classification timed out. Try a shorter input or increase timeout.", status_code=504)
    except HTTPException as http_exc:
        # Handle HTTP errors
        return JSONResponse(content=f"HTTP error: {http_exc.detail}", status_code=http_exc.status_code)
    except Exception as e:
        # Handle any other errors
        return JSONResponse(content=f"Error in classification pipeline: {str(e)}", status_code=500)
        
# Set up CORS middleware
origins = ["*"]  # or specify your list of allowed origins
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)