RafaelJaime commited on
Commit
ba4c1b9
·
verified ·
1 Parent(s): 437334a

Update src/App.js

Browse files
Files changed (1) hide show
  1. src/App.js +13 -9
src/App.js CHANGED
@@ -3,7 +3,6 @@ import { Home, Upload, List, ShoppingCart, TrendingUp, Calendar, Trash2, Edit2,
3
  import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, BarChart, Bar, PieChart, Pie, Cell, ResponsiveContainer } from 'recharts';
4
 
5
  const StoreContext = createContext();
6
-
7
  const StoreProvider = ({ children }) => {
8
  const [products, setProducts] = useState(() => {
9
  const stored = localStorage.getItem('grocery-tracker-storage');
@@ -57,7 +56,9 @@ const convertImageToBase64 = (file) => {
57
  });
58
  };
59
 
60
- const extractTextFromImage = async (file) => {
 
 
61
  try {
62
  const base64Image = await convertImageToBase64(file);
63
 
@@ -65,7 +66,7 @@ const extractTextFromImage = async (file) => {
65
  method: 'POST',
66
  headers: {
67
  'Content-Type': 'application/json',
68
- 'Authorization': `Bearer ${MISTRAL_API_KEY}`
69
  },
70
  body: JSON.stringify({
71
  model: 'pixtral-12b-2409',
@@ -103,13 +104,15 @@ const extractTextFromImage = async (file) => {
103
  }
104
  };
105
 
106
- const processReceiptText = async (text) => {
 
 
107
  try {
108
  const response = await fetch(MISTRAL_API_URL, {
109
  method: 'POST',
110
  headers: {
111
  'Content-Type': 'application/json',
112
- 'Authorization': `Bearer ${MISTRAL_API_KEY}`
113
  },
114
  body: JSON.stringify({
115
  model: 'mistral-large-latest',
@@ -171,6 +174,7 @@ const UploadTab = () => {
171
  const [extractedText, setExtractedText] = useState('');
172
  const [processedProducts, setProcessedProducts] = useState([]);
173
  const [error, setError] = useState(null);
 
174
 
175
  const handleFileSelect = (e) => {
176
  const selectedFile = e.target.files[0];
@@ -184,8 +188,8 @@ const UploadTab = () => {
184
  const handleUpload = async () => {
185
  if (!file) return;
186
 
187
- if (!MISTRAL_API_KEY) {
188
- setError('Mistral API key not configured. Please set REACT_APP_MISTRAL_API_KEY environment variable.');
189
  return;
190
  }
191
 
@@ -194,11 +198,11 @@ const UploadTab = () => {
194
  setStep('extracting');
195
 
196
  try {
197
- const text = await extractTextFromImage(file);
198
  setExtractedText(text);
199
  setStep('processing');
200
 
201
- const products = await processReceiptText(text);
202
  setProcessedProducts(products);
203
  setStep('review');
204
  } catch (error) {
 
3
  import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, BarChart, Bar, PieChart, Pie, Cell, ResponsiveContainer } from 'recharts';
4
 
5
  const StoreContext = createContext();
 
6
  const StoreProvider = ({ children }) => {
7
  const [products, setProducts] = useState(() => {
8
  const stored = localStorage.getItem('grocery-tracker-storage');
 
56
  });
57
  };
58
 
59
+ const extractTextFromImage = async (file, userApiKey) => {
60
+ const apiKeyToUse = userApiKey || MISTRAL_API_KEY;
61
+
62
  try {
63
  const base64Image = await convertImageToBase64(file);
64
 
 
66
  method: 'POST',
67
  headers: {
68
  'Content-Type': 'application/json',
69
+ 'Authorization': `Bearer ${apiKeyToUse}`
70
  },
71
  body: JSON.stringify({
72
  model: 'pixtral-12b-2409',
 
104
  }
105
  };
106
 
107
+ const processReceiptText = async (text, userApiKey) => {
108
+ const apiKeyToUse = userApiKey || MISTRAL_API_KEY;
109
+
110
  try {
111
  const response = await fetch(MISTRAL_API_URL, {
112
  method: 'POST',
113
  headers: {
114
  'Content-Type': 'application/json',
115
+ 'Authorization': `Bearer ${apiKeyToUse}`
116
  },
117
  body: JSON.stringify({
118
  model: 'mistral-large-latest',
 
174
  const [extractedText, setExtractedText] = useState('');
175
  const [processedProducts, setProcessedProducts] = useState([]);
176
  const [error, setError] = useState(null);
177
+ const [apiKey, setApiKey] = useState('');
178
 
179
  const handleFileSelect = (e) => {
180
  const selectedFile = e.target.files[0];
 
188
  const handleUpload = async () => {
189
  if (!file) return;
190
 
191
+ if (!apiKey && !MISTRAL_API_KEY) {
192
+ setError('Please enter your Mistral API key or configure REACT_APP_MISTRAL_API_KEY environment variable.');
193
  return;
194
  }
195
 
 
198
  setStep('extracting');
199
 
200
  try {
201
+ const text = await extractTextFromImage(file, apiKey);
202
  setExtractedText(text);
203
  setStep('processing');
204
 
205
+ const products = await processReceiptText(text, apiKey);
206
  setProcessedProducts(products);
207
  setStep('review');
208
  } catch (error) {