Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -34,14 +34,6 @@ app.add_middleware(
|
|
34 |
allow_headers=["*"],
|
35 |
)
|
36 |
|
37 |
-
nlp_qa = pipeline("document-question-answering", model="jinhybr/OCR-DocVQA-Donut")
|
38 |
-
nlp_qa_v2 = pipeline("document-question-answering", model="faisalraza/layoutlm-invoices", ignore_mismatched_sizes=True)
|
39 |
-
nlp_qa_v3 = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
40 |
-
nlp_classification = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
41 |
-
nlp_classification_v2 = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
42 |
-
nlp_speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
|
43 |
-
nlp_sequence_classification = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
44 |
-
nlp_main_classification = pipeline("zero-shot-classification", model="roberta-large-mnli")
|
45 |
code_generation_model = pipeline('text-generation', model='codeparrot/codeparrot-small')
|
46 |
|
47 |
description = """
|
@@ -55,38 +47,6 @@ This API performs document question answering using a LayoutLMv2-based model.
|
|
55 |
|
56 |
app = FastAPI(docs_url="/", description=description)
|
57 |
|
58 |
-
@app.post("/uploadfile/", description="Upload an image file to extract text and answer provided questions.")
|
59 |
-
async def perform_document_qa(
|
60 |
-
file: UploadFile = File(...),
|
61 |
-
questions: str = Form(...),
|
62 |
-
):
|
63 |
-
try:
|
64 |
-
# Read the uploaded file as bytes
|
65 |
-
contents = await file.read()
|
66 |
-
|
67 |
-
# Open the image using PIL
|
68 |
-
image = Image.open(BytesIO(contents))
|
69 |
-
|
70 |
-
# Perform document question answering for each question using LayoutLMv2-based model
|
71 |
-
answers_dict = {}
|
72 |
-
for question in questions.split(','):
|
73 |
-
result = nlp_qa(
|
74 |
-
image,
|
75 |
-
question.strip()
|
76 |
-
)
|
77 |
-
|
78 |
-
# Access the 'answer' key from the first item in the result list
|
79 |
-
answer = result[0]['answer']
|
80 |
-
|
81 |
-
# Format the question as a string without extra characters
|
82 |
-
formatted_question = question.strip("[]")
|
83 |
-
|
84 |
-
answers_dict[formatted_question] = answer
|
85 |
-
|
86 |
-
return answers_dict
|
87 |
-
except Exception as e:
|
88 |
-
return JSONResponse(content=f"Error processing file: {str(e)}", status_code=500)
|
89 |
-
|
90 |
@app.post("/generate_code/", description="Generate code based on the provided prompt.")
|
91 |
async def generate_code(prompt: str = Form(...)):
|
92 |
try:
|
|
|
34 |
allow_headers=["*"],
|
35 |
)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
code_generation_model = pipeline('text-generation', model='codeparrot/codeparrot-small')
|
38 |
|
39 |
description = """
|
|
|
47 |
|
48 |
app = FastAPI(docs_url="/", description=description)
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
@app.post("/generate_code/", description="Generate code based on the provided prompt.")
|
51 |
async def generate_code(prompt: str = Form(...)):
|
52 |
try:
|