-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_plot_test.py
More file actions
executable file
·182 lines (149 loc) · 6.04 KB
/
agent_plot_test.py
File metadata and controls
executable file
·182 lines (149 loc) · 6.04 KB
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
import os
import torch
from PIL import Image
from transformers import (
CLIPProcessor,
CLIPModel,
DonutProcessor,
VisionEncoderDecoderModel,
BlipForConditionalGeneration,
BlipProcessor
)
from llama_cpp import Llama
import textwrap
import re
from typing import Optional
class ScientificPlotAnalyzer:
def __init__(self, device: str = "cuda"):
self.device = device
# CLIP model - keep in float32 for stability
self.clip_model = CLIPModel.from_pretrained(
"openai/clip-vit-large-patch14"
).to(self.device)
self.clip_processor = CLIPProcessor.from_pretrained(
"openai/clip-vit-large-patch14"
)
# Donut model - keep in float32
self.donut_processor = DonutProcessor.from_pretrained(
"naver-clova-ix/donut-base-finetuned-docvqa"
)
self.donut_model = VisionEncoderDecoderModel.from_pretrained(
"naver-clova-ix/donut-base-finetuned-docvqa"
).to(self.device)
# BLIP model - keep in float32
self.blip_processor = BlipProcessor.from_pretrained(
"Salesforce/blip-image-captioning-large"
)
self.blip_model = BlipForConditionalGeneration.from_pretrained(
"Salesforce/blip-image-captioning-large"
).to(self.device)
# LLM setup
self.llm = Llama(
model_path="mistral-7b-instruct-v0.1.Q4_K_M.gguf",
n_ctx=8192,
n_gpu_layers=40,
n_threads=16
)
def analyze_plot(self, image_path: str) -> str:
try:
image = Image.open(image_path).convert("RGB")
# CLIP analysis
clip_inputs = self.clip_processor(
text=["scientific plot", "data visualization", "research chart"],
images=image,
return_tensors="pt",
padding=True
).to(self.device)
with torch.no_grad():
clip_outputs = self.clip_model(**clip_inputs)
clip_probs = clip_outputs.logits_per_image.softmax(dim=1)
# Donut analysis
donut_inputs = self.donut_processor(
image,
return_tensors="pt"
).to(self.device)
pixel_values = donut_inputs.pixel_values
# Create decoder inputs safely
decoder_input_ids = torch.tensor(
[[self.donut_processor.tokenizer.pad_token_id]],
dtype=torch.long,
device=self.device
)
with torch.no_grad():
donut_output = self.donut_model.generate(
pixel_values,
decoder_input_ids=decoder_input_ids,
max_length=512,
early_stopping=True,
pad_token_id=self.donut_processor.tokenizer.pad_token_id,
eos_token_id=self.donut_processor.tokenizer.eos_token_id,
num_beams=3
)
donut_result = self.donut_processor.batch_decode(
donut_output, skip_special_tokens=True
)[0]
# BLIP analysis
blip_inputs = self.blip_processor(
image,
"A detailed scientific analysis of this plot including: "
"1. Axis labels and units 2. Data trends 3. Statistical annotations",
return_tensors="pt"
).to(self.device)
with torch.no_grad():
blip_output = self.blip_model.generate(
**blip_inputs,
max_new_tokens=512,
num_beams=5
)
blip_result = self.blip_processor.decode(
blip_output[0], skip_special_tokens=True
)
# LLM synthesis
analysis_prompt = f"""
SCIENTIFIC PLOT ANALYSIS TASK
[VISUAL ANALYSIS RESULTS]
{donut_result}
[DETAILED CAPTION]
{blip_result}
Generate a comprehensive technical report with these sections:
1. Plot Identification
2. Axis Specifications
3. Data Series Analysis
4. Statistical Findings
5. Scientific Interpretation
6. Quality Assessment
Use precise technical language and include all quantitative details.
"""
llm_response = self.llm.create_completion(
analysis_prompt,
max_tokens=4096,
temperature=0.1,
top_p=0.9,
repeat_penalty=1.1
)
return llm_response['choices'][0]['text']
except Exception as e:
return f"Analysis failed: {str(e)}"
if __name__ == "__main__":
try:
analyzer = ScientificPlotAnalyzer(device="cuda")
images = [f for f in os.listdir() if f.lower().endswith(
('.png','.jpg','.jpeg','.tiff','.bmp')
)]
if not images:
raise FileNotFoundError("No scientific plots found in directory")
print("\nAvailable plots:")
for i, img in enumerate(images):
print(f"{i+1}. {img}")
selection = int(input("\nSelect plot to analyze (number): ")) - 1
image_path = images[selection]
print(f"\n🚀 Analyzing {image_path} with GPU acceleration...")
analysis = analyzer.analyze_plot(image_path)
output_file = f"analysis_{os.path.splitext(image_path)[0]}.txt"
with open(output_file, "w") as f:
f.write(analysis)
print(f"\n✅ Analysis complete. Results saved to {output_file}")
print("\n=== ANALYSIS RESULTS ===\n")
print(analysis)
except Exception as e:
print(f"\n❌ Error: {str(e)}")