Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
outputs = model.generate(**inputs)
|
| 27 |
-
all_results = [processor.decode(seq, skip_special_tokens=True) for seq in outputs]
|
| 28 |
-
print(all_results)
|
| 29 |
-
result = processor.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
-
return result
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
gr.Textbox(
|
| 40 |
-
label="Instruction / Header Text",
|
| 41 |
-
placeholder="Enter instructions like 'Convert this bar chart into a table of quarter and cost values'"
|
| 42 |
-
) # Text input
|
| 43 |
-
],
|
| 44 |
-
outputs="text",
|
| 45 |
-
title="DePlot Chart Data Extractor 1",
|
| 46 |
-
description="Upload a chart image and get its extracted data/description."
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForSeq2SeqLM
|
| 3 |
from PIL import Image
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
+
# Load DePlot (vision-to-table)
|
| 7 |
+
deplot_model = AutoModelForSeq2SeqLM.from_pretrained("google/deplot")
|
| 8 |
+
deplot_processor = AutoProcessor.from_pretrained("google/deplot")
|
| 9 |
|
| 10 |
+
def extract_chart(image):
|
| 11 |
+
# Step 1: Run DePlot
|
| 12 |
+
inputs = deplot_processor(images=image, text="Generate table from chart.", return_tensors="pt")
|
| 13 |
+
outputs = deplot_model.generate(**inputs, max_new_tokens=512)
|
| 14 |
+
table = deplot_processor.decode(outputs[0], skip_special_tokens=True)
|
| 15 |
|
| 16 |
+
# Step 2: Create dummy structured JSON (later we plug in detection+OCR)
|
| 17 |
+
structured_json = {
|
| 18 |
+
"metadata": {"title": "Demo Chart", "chart_type": "bar", "confidence": 0.5},
|
| 19 |
+
"axes": {"x_axis": {"label": "X", "ticks": []}, "y_axis": {"label": "Y", "ticks": []}},
|
| 20 |
+
"series": [],
|
| 21 |
+
"legend": {"entries": []}
|
| 22 |
+
}
|
| 23 |
|
| 24 |
+
# Step 3: Merge (for now, just attach DePlot’s output)
|
| 25 |
+
merged_output = {
|
| 26 |
+
"structured_json": structured_json,
|
| 27 |
+
"deplot_table": table,
|
| 28 |
+
"fusion_notes": "Fusion layer not implemented yet, just showing both outputs."
|
| 29 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
return json.dumps(merged_output, indent=2)
|
| 32 |
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=extract_chart,
|
| 35 |
+
inputs=gr.Image(type="pil"),
|
| 36 |
+
outputs="json",
|
| 37 |
+
title="Chart-to-JSON Extractor (Prototype)",
|
| 38 |
+
description="Uploads a chart, extracts structured JSON (dummy) and DePlot table side-by-side."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
demo.launch()
|