Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,46 @@
|
|
| 1 |
-
from
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import numpy as np
|
| 6 |
-
import torch
|
| 7 |
from PIL import Image
|
| 8 |
-
import io
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
import base64, os
|
| 12 |
-
from utils import check_ocr_box, get_yolo_model, get_caption_model_processor, get_som_labeled_img
|
| 13 |
import torch
|
| 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 |
-
# import pdb; pdb.set_trace()
|
| 56 |
-
image = Image.open(image_save_path)
|
| 57 |
-
box_overlay_ratio = image.size[0] / 3200
|
| 58 |
-
draw_bbox_config = {
|
| 59 |
-
'text_scale': 0.8 * box_overlay_ratio,
|
| 60 |
-
'text_thickness': max(int(2 * box_overlay_ratio), 1),
|
| 61 |
-
'text_padding': max(int(3 * box_overlay_ratio), 1),
|
| 62 |
-
'thickness': max(int(3 * box_overlay_ratio), 1),
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
ocr_bbox_rslt, is_goal_filtered = check_ocr_box(image_save_path, display_img = False, output_bb_format='xyxy', goal_filtering=None, easyocr_args={'paragraph': False, 'text_threshold':0.9}, use_paddleocr=True)
|
| 66 |
-
text, ocr_bbox = ocr_bbox_rslt
|
| 67 |
-
# print('prompt:', prompt)
|
| 68 |
-
dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(image_save_path, yolo_model, BOX_TRESHOLD = box_threshold, output_coord_in_ratio=True, ocr_bbox=ocr_bbox,draw_bbox_config=draw_bbox_config, caption_model_processor=caption_model_processor, ocr_text=text,iou_threshold=iou_threshold)
|
| 69 |
-
image = Image.open(io.BytesIO(base64.b64decode(dino_labled_img)))
|
| 70 |
-
print('finish processing')
|
| 71 |
-
parsed_content_list = '\n'.join(parsed_content_list)
|
| 72 |
-
return image, str(parsed_content_list), str(label_coordinates)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
with gr.Blocks() as demo:
|
| 77 |
-
gr.Markdown(MARKDOWN)
|
| 78 |
-
with gr.Row():
|
| 79 |
-
with gr.Column():
|
| 80 |
-
image_input_component = gr.Image(
|
| 81 |
-
type='pil', label='Upload image')
|
| 82 |
-
# set the threshold for removing the bounding boxes with low confidence, default is 0.05
|
| 83 |
-
box_threshold_component = gr.Slider(
|
| 84 |
-
label='Box Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.05)
|
| 85 |
-
# set the threshold for removing the bounding boxes with large overlap, default is 0.1
|
| 86 |
-
iou_threshold_component = gr.Slider(
|
| 87 |
-
label='IOU Threshold', minimum=0.01, maximum=1.0, step=0.01, value=0.1)
|
| 88 |
-
submit_button_component = gr.Button(
|
| 89 |
-
value='Submit', variant='primary')
|
| 90 |
-
with gr.Column():
|
| 91 |
-
image_output_component = gr.Image(type='pil', label='Image Output')
|
| 92 |
-
text_output_component = gr.Textbox(label='Parsed screen elements', placeholder='Text Output')
|
| 93 |
-
coordinates_output_component = gr.Textbox(label='Coordinates', placeholder='Coordinates Output')
|
| 94 |
-
|
| 95 |
-
submit_button_component.click(
|
| 96 |
-
fn=process,
|
| 97 |
-
inputs=[
|
| 98 |
-
image_input_component,
|
| 99 |
-
box_threshold_component,
|
| 100 |
-
iou_threshold_component
|
| 101 |
-
],
|
| 102 |
-
outputs=[image_output_component, text_output_component, coordinates_output_component]
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
# demo.launch(debug=False, show_error=True, share=True)
|
| 106 |
-
# demo.launch(share=True, server_port=7861, server_name='0.0.0.0')
|
| 107 |
-
demo.queue().launch(share=False)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, Form
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForVisualQuestionAnswering
|
|
|
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import torch
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
# Initialize FastAPI app
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# Load model and processor with trust_remote_code=True
|
| 12 |
+
processor = AutoProcessor.from_pretrained("Sanket17/hello", trust_remote_code=True)
|
| 13 |
+
model = AutoModelForVisualQuestionAnswering.from_pretrained("Sanket17/hello", trust_remote_code=True)
|
| 14 |
+
|
| 15 |
+
@app.post("/vqa/")
|
| 16 |
+
async def visual_question_answer(file: UploadFile, question: str = Form(...)):
|
| 17 |
+
"""
|
| 18 |
+
Endpoint for visual question answering.
|
| 19 |
+
- file: Upload an image file
|
| 20 |
+
- question: Textual question about the image
|
| 21 |
+
"""
|
| 22 |
+
try:
|
| 23 |
+
# Load image
|
| 24 |
+
image = Image.open(file.file).convert("RGB")
|
| 25 |
+
|
| 26 |
+
# Preprocess inputs
|
| 27 |
+
inputs = processor(images=image, text=question, return_tensors="pt")
|
| 28 |
+
|
| 29 |
+
# Get model predictions
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
|
| 32 |
+
# Decode the answer (check model output for correct handling)
|
| 33 |
+
answer = outputs.logits.argmax(dim=-1).item() # Example way to get the answer index
|
| 34 |
+
|
| 35 |
+
# If the output logits contain a mapping, we can return the answer string
|
| 36 |
+
answer_str = processor.decode([answer]) # Assuming you get the answer index from logits
|
| 37 |
+
|
| 38 |
+
# Return JSON response
|
| 39 |
+
return JSONResponse(content={"question": question, "answer": answer_str})
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 43 |
+
|
| 44 |
+
# Start the FastAPI server
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|