Spaces:
Running
Running
File size: 11,031 Bytes
5d30640 |
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 |
import streamlit as st
import torch
from PIL import Image
import io
import requests
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
import matplotlib.pyplot as plt
import os
import pandas as pd
import re
import base64
# Set page config
st.set_page_config(
page_title="Chart Q&A ",
page_icon="π",
layout="wide"
)
# Initialize session state variables
if 'paligemma_model' not in st.session_state:
st.session_state.paligemma_model = None
if 'paligemma_processor' not in st.session_state:
st.session_state.paligemma_processor = None
if 'device' not in st.session_state:
st.session_state.device = None
if 'current_image' not in st.session_state:
st.session_state.current_image = None
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'extracted_data' not in st.session_state:
st.session_state.extracted_data = None
# Initialize PaliGemma Model
@st.cache_resource
def load_paligemma_model():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = PaliGemmaForConditionalGeneration.from_pretrained(
"ahmed-masry/chartgemma",
torch_dtype=torch.float16
)
processor = AutoProcessor.from_pretrained("ahmed-masry/chartgemma")
model = model.to(device)
return model, processor, device
# Function to download sample chart
def download_sample_chart(url, filename):
try:
if not os.path.exists(filename):
response = requests.get(url)
if response.status_code == 200:
with open(filename, 'wb') as f:
f.write(response.content)
return True
else:
st.error(f"Failed to download sample chart: {response.status_code}")
return False
return True
except Exception as e:
st.error(f"Error downloading sample chart: {str(e)}")
return False
# Function to clean model output from print statements and other artifacts
def clean_model_output(text):
# Check if the entire response is a print statement and extract its content
print_match = re.search(r'^print\(["\'](.+?)["\']\)$', text.strip())
if print_match:
return print_match.group(1)
# Remove all print statements
text = re.sub(r'print\(.+?\)', '', text, flags=re.DOTALL)
# Remove Python code formatting artifacts
text = re.sub(r'```python|```', '', text)
return text.strip()
# Function to analyze chart with PaliGemma
def analyze_chart_with_paligemma(model, processor, device, image, query, use_cot=False):
try:
# Add program of thought prefix if CoT is enabled
if use_cot and not query.startswith("program of thought:"):
modified_query = f"program of thought: {query}"
else:
modified_query = query
inputs = processor(text=modified_query, images=image, return_tensors="pt")
prompt_length = inputs['input_ids'].shape[1]
inputs = {k: v.to(device) for k, v in inputs.items()}
# Generate with progress bar
progress_bar = st.progress(0)
with torch.no_grad():
generate_ids = model.generate(
**inputs,
num_beams=4,
max_new_tokens=512,
output_scores=True,
return_dict_in_generate=True
)
progress_bar.progress(100)
output_text = processor.batch_decode(
generate_ids.sequences[:, prompt_length:],
skip_special_tokens=True,
clean_up_tokenization_spaces=False
)[0]
# Clean output from print statements and other artifacts
output_text = clean_model_output(output_text)
return output_text
except Exception as e:
st.error(f"Error analyzing chart : {str(e)}")
return f"Error: {str(e)}"
# Function to extract data points from chart
def extract_data_points(model, processor, device, image):
try:
# Special query to extract data points
extraction_query = "program of thought: Extract all data points from this chart. List each category or series and all its corresponding values in a structured format."
with st.spinner("Extracting data points from chart..."):
result = analyze_chart_with_paligemma(model, processor, device, image, extraction_query)
# Parse the result into a DataFrame
df = parse_chart_data(result)
return df
except Exception as e:
st.error(f"Error extracting data points: {str(e)}")
return None
# Function to parse chart data from model response
def parse_chart_data(text):
try:
# Clean the text from print statements first
text = clean_model_output(text)
data = {}
lines = text.split('\n')
current_category = None
for line in lines:
if not line.strip():
continue
if ':' in line and not re.search(r'\d+\.\d+', line):
current_category = line.split(':')[0].strip()
data[current_category] = []
elif current_category and (re.search(r'\d+', line) or ',' in line):
value_match = re.findall(r'[-+]?\d*\.\d+|\d+', line)
if value_match:
data[current_category].extend(value_match)
if not data:
table_pattern = r'(\w+(?:\s\w+)*)\s*[:|]\s*((?:\d+(?:\.\d+)?(?:\s*,\s*\d+(?:\.\d+)?)*)|(?:\d+(?:\.\d+)?))'
matches = re.findall(table_pattern, text)
for category, values in matches:
category = category.strip()
if category not in data:
data[category] = []
if ',' in values:
values = [v.strip() for v in values.split(',')]
else:
values = [values.strip()]
data[category].extend(values)
df = pd.DataFrame(data)
if df.empty:
df = pd.DataFrame({'Extracted_Text': [text]})
return df
except Exception as e:
st.error(f"Error parsing chart data: {str(e)}")
return pd.DataFrame({'Raw_Text': [text]})
# Function to create a download link for dataframe
def get_csv_download_link(df, filename="chart_data.csv"):
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode()
href = f'<a href="data:file/csv;base64,{b64}" download="{filename}">Download CSV File</a>'
return href
# Main UI
st.title("π Chart Analysis ")
# Sidebar for model loading and options
with st.sidebar:
st.header("Model Setup")
if st.button("Load Model"):
with st.spinner("Loading model... This may take a moment"):
model, processor, device = load_paligemma_model()
st.session_state.paligemma_model = model
st.session_state.paligemma_processor = processor
st.session_state.device = device
st.success(f"β
Model loaded successfully on {device}!")
st.header("Options")
use_cot = st.checkbox("Enable Chain-of-Thought reasoning", value=True,
help="Adds 'program of thought:' prefix to prompts for better reasoning")
st.header("Sample Charts")
if st.button("Load Sample Chart"):
sample_url = "https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/multi_col_1229.png"
sample_filename = "chart_example_1.png"
if download_sample_chart(sample_url, sample_filename):
st.session_state.current_image = Image.open(sample_filename).convert('RGB')
st.success("Sample chart loaded!")
# Main content area
col1, col2 = st.columns([3, 2])
with col1:
st.header("Upload Chart")
uploaded_file = st.file_uploader("Choose a chart image", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
try:
image = Image.open(uploaded_file).convert('RGB')
st.session_state.current_image = image
# Reset extracted data when new image is uploaded
st.session_state.extracted_data = None
except Exception as e:
st.error(f"Error opening image: {str(e)}")
# Display current image
if st.session_state.current_image is not None:
st.image(st.session_state.current_image, caption="Current Chart", use_column_width=True)
# Add extract data points button
if st.session_state.paligemma_model is not None:
if st.button("Extract Data Points from Chart"):
df = extract_data_points(
st.session_state.paligemma_model,
st.session_state.paligemma_processor,
st.session_state.device,
st.session_state.current_image
)
if df is not None:
st.session_state.extracted_data = df
st.success("Data points extracted successfully!")
with col2:
st.header("Ask Questions")
if st.session_state.paligemma_model is None:
st.warning("Please load the model first from the sidebar.")
elif st.session_state.current_image is None:
st.warning("Please upload a chart image or load a sample chart.")
else:
# Query input
query = st.text_input("Ask a question about the chart:",
placeholder="E.g., What is the highest value in the chart?")
if query:
if st.button("Analyze Chart"):
with st.spinner("Analyzing chart "):
answer = analyze_chart_with_paligemma(
st.session_state.paligemma_model,
st.session_state.paligemma_processor,
st.session_state.device,
st.session_state.current_image,
query,
use_cot
)
# Add to chat history
st.session_state.chat_history.append({
"question": query,
"answer": answer
})
# Display answer
st.subheader("Answer")
st.write(answer)
# Display extracted data if available
if st.session_state.extracted_data is not None:
st.header("Extracted Data Points")
st.dataframe(st.session_state.extracted_data)
# Download button for CSV
st.markdown(get_csv_download_link(st.session_state.extracted_data), unsafe_allow_html=True)
# Display chat history
if st.session_state.chat_history:
st.header("Question History")
for i, qa in enumerate(reversed(st.session_state.chat_history)):
with st.expander(f"Q: {qa['question']}", expanded=(i==0)):
st.markdown(f"**A:** {qa['answer']}") |