feat: 文件分析支持csv excel img
This commit is contained in:
parent
e851d2ca02
commit
3f13fa4287
@ -70,7 +70,8 @@
|
|||||||
"react-echarts": "^0.1.1",
|
"react-echarts": "^0.1.1",
|
||||||
"react-helmet-async": "^1.3.0",
|
"react-helmet-async": "^1.3.0",
|
||||||
"react-intl": "^7.1.6",
|
"react-intl": "^7.1.6",
|
||||||
"react-markdown": "^10.1.0"
|
"react-markdown": "^10.1.0",
|
||||||
|
"xlsx": "^0.18.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@ant-design/pro-cli": "^3.3.0",
|
"@ant-design/pro-cli": "^3.3.0",
|
||||||
|
@ -8,6 +8,7 @@ import { marked } from 'marked';
|
|||||||
import styles from './index.less';
|
import styles from './index.less';
|
||||||
import { Document, Packer, Paragraph as DocxParagraph, TextRun } from 'docx';
|
import { Document, Packer, Paragraph as DocxParagraph, TextRun } from 'docx';
|
||||||
import { saveAs } from 'file-saver';
|
import { saveAs } from 'file-saver';
|
||||||
|
import * as XLSX from 'xlsx';
|
||||||
|
|
||||||
const { Dragger } = Upload;
|
const { Dragger } = Upload;
|
||||||
const { TextArea } = Input;
|
const { TextArea } = Input;
|
||||||
@ -49,13 +50,54 @@ const ReportPage: React.FC = () => {
|
|||||||
const handleUpload = async (file: File) => {
|
const handleUpload = async (file: File) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
// 读取文件为 base64
|
const isImage = file.type.startsWith('image/');
|
||||||
|
const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
|
||||||
|
file.type === 'application/vnd.ms-excel';
|
||||||
|
const isCsv = file.type === 'text/csv';
|
||||||
|
|
||||||
|
if (isImage) {
|
||||||
|
// 处理图片文件
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.readAsDataURL(file);
|
reader.readAsDataURL(file);
|
||||||
reader.onload = async () => {
|
reader.onload = async () => {
|
||||||
const base64Image = reader.result as string;
|
const base64Image = reader.result as string;
|
||||||
|
await analyzeData({
|
||||||
|
type: 'image_url',
|
||||||
|
image_url: {
|
||||||
|
url: base64Image
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
} else if (isExcel || isCsv) {
|
||||||
|
// 处理 Excel/CSV 文件
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = async (e) => {
|
||||||
|
const data = e.target?.result;
|
||||||
|
const workbook = XLSX.read(data, { type: 'array' });
|
||||||
|
const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||||
|
const jsonData = XLSX.utils.sheet_to_json(firstSheet, { header: 1 });
|
||||||
|
|
||||||
// 调用 NewAPI 接口
|
// 将数据转换为字符串
|
||||||
|
const textContent = jsonData.map(row => row.join('\t')).join('\n');
|
||||||
|
|
||||||
|
await analyzeData({
|
||||||
|
type: 'text',
|
||||||
|
text: textContent
|
||||||
|
});
|
||||||
|
};
|
||||||
|
reader.readAsArrayBuffer(file);
|
||||||
|
} else {
|
||||||
|
message.error('不支持的文件格式');
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
message.error('文件处理失败');
|
||||||
|
console.error('文件处理失败:', error);
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const analyzeData = async (content: any) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('https://aizex.top/v1/chat/completions', {
|
const response = await fetch('https://aizex.top/v1/chat/completions', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -71,14 +113,9 @@ const ReportPage: React.FC = () => {
|
|||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
type: 'text',
|
type: 'text',
|
||||||
text: '请分析这张图表中的数据趋势和关键信息,并给出专业的分析见解。'
|
text: '请分析这些数据的趋势和关键信息,并给出专业的分析见解。'
|
||||||
},
|
},
|
||||||
{
|
content
|
||||||
type: 'image_url',
|
|
||||||
image_url: {
|
|
||||||
url: base64Image
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -93,15 +130,11 @@ const ReportPage: React.FC = () => {
|
|||||||
data: [[data.choices[0].message.content]]
|
data: [[data.choices[0].message.content]]
|
||||||
});
|
});
|
||||||
|
|
||||||
message.success('图片分析成功');
|
message.success('数据分析成功');
|
||||||
setCurrentStep(1);
|
setCurrentStep(1);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
message.error('图片分析失败');
|
message.error('数据分析失败');
|
||||||
console.error('API调用失败:', error);
|
console.error('API调用失败:', error);
|
||||||
}
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
message.error('文件处理失败');
|
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user