dev2607 commited on
Commit
53710bd
·
verified ·
1 Parent(s): 88c3fd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +394 -60
app.py CHANGED
@@ -1,64 +1,398 @@
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ import os
2
+ import subprocess
3
+ import sys
4
+ import re
5
+ import numpy as np
6
+ from PIL import Image
7
  import gradio as gr
8
+ import requests
9
+ import json
10
+ from dotenv import load_dotenv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Attempt to install pytesseract if not found
13
+ try:
14
+ import pytesseract
15
+ except ImportError:
16
+ subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pytesseract'])
17
+ import pytesseract
18
 
19
+ # AFTER importing pytesseract, then set the path
20
+ try:
21
+ # First try the default path
22
+ if os.path.exists('/usr/bin/tesseract'):
23
+ pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
24
+ # Try to find it on the PATH
25
+ else:
26
+ tesseract_path = subprocess.check_output(['which', 'tesseract']).decode().strip()
27
+ if tesseract_path:
28
+ pytesseract.pytesseract.tesseract_cmd = tesseract_path
29
+ except:
30
+ # If all else fails, try the default installation path
31
+ pytesseract.pytesseract.tesseract_cmd = 'tesseract'
32
+
33
+
34
+ # Load environment variables
35
+ load_dotenv()
36
+
37
+ # Import and configure Gemini API
38
+ try:
39
+ import google.generativeai as genai
40
+ # Configure Gemini API
41
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") or GEMINI_API_KEY
42
+ if GEMINI_API_KEY:
43
+ genai.configure(api_key=GEMINI_API_KEY)
44
+ print("Gemini API configured successfully")
45
+ else:
46
+ print("Warning: No Gemini API key found. Will use fallback analysis.")
47
+ except ImportError:
48
+ print("Google Generative AI package not found, using dummy implementation")
49
+ genai = None
50
+
51
+ # Function to extract text from images using OCR
52
+ def extract_text_from_image(image):
53
+ try:
54
+ if image is None:
55
+ return "No image captured. Please try again."
56
+
57
+ # Verify Tesseract executable is accessible
58
+ try:
59
+ subprocess.run([pytesseract.pytesseract.tesseract_cmd, "--version"],
60
+ check=True, capture_output=True, text=True)
61
+ except (subprocess.SubprocessError, FileNotFoundError):
62
+ return "Tesseract OCR is not installed or not properly configured. Please check installation."
63
+
64
+ # Image preprocessing for better OCR
65
+ import cv2
66
+ import numpy as np
67
+
68
+ # Convert PIL image to OpenCV format
69
+ img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
70
+
71
+ # Convert to grayscale
72
+ gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
73
+
74
+ # Apply thresholding to get black and white image
75
+ _, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
76
+
77
+ # Noise removal
78
+ kernel = np.ones((1, 1), np.uint8)
79
+ binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
80
+
81
+ # Dilate to connect text
82
+ binary = cv2.dilate(binary, kernel, iterations=1)
83
+
84
+ # Convert back to PIL image for tesseract
85
+ binary_pil = Image.fromarray(cv2.bitwise_not(binary))
86
+
87
+ # Run OCR with improved configuration
88
+ custom_config = r'--oem 3 --psm 6 -l eng'
89
+ text = pytesseract.image_to_string(binary_pil, config=custom_config)
90
+
91
+ if not text.strip():
92
+ # Try original image as fallback
93
+ text = pytesseract.image_to_string(image, config=custom_config)
94
+
95
+ if not text.strip():
96
+ return "No text could be extracted. Ensure image is clear and readable."
97
+
98
+ return text
99
+ except Exception as e:
100
+ return f"Error extracting text: {str(e)}"
101
+ # Function to parse ingredients from text
102
+ def parse_ingredients(text):
103
+ if not text:
104
+ return []
105
+
106
+ # Clean up the text
107
+ text = re.sub(r'^ingredients:?\s*', '', text.lower(), flags=re.IGNORECASE)
108
+
109
+ # Remove common OCR errors and extraneous characters
110
+ text = re.sub(r'[|\\/@#$%^&*()_+=]', '', text)
111
+
112
+ # Replace common OCR errors
113
+ text = re.sub(r'\bngredients\b', 'ingredients', text)
114
+
115
+ # Handle common OCR misreads
116
+ replacements = {
117
+ '0': 'o', 'l': 'i', '1': 'i',
118
+ '5': 's', '8': 'b', 'Q': 'g',
119
+ }
120
+
121
+ for error, correction in replacements.items():
122
+ text = text.replace(error, correction)
123
+
124
+ # Split by common ingredient separators
125
+ ingredients = re.split(r',|;|\n', text)
126
+
127
+ # Clean up each ingredient
128
+ cleaned_ingredients = []
129
+ for i in ingredients:
130
+ i = i.strip().lower()
131
+ if i and len(i) > 1: # Ignore single characters which are likely OCR errors
132
+ cleaned_ingredients.append(i)
133
+
134
+ return cleaned_ingredients
135
+
136
+ # Function to analyze ingredients with Gemini
137
+ # Function to analyze ingredients with Gemini
138
+ def analyze_ingredients_with_gemini(ingredients_list, health_conditions=None):
139
+ """
140
+ Use Gemini to analyze ingredients and provide health insights
141
+ """
142
+ if not ingredients_list:
143
+ return "No ingredients detected or provided."
144
+
145
+ # Prepare the list of ingredients for the prompt
146
+ ingredients_text = ", ".join(ingredients_list)
147
+
148
+ # Check if Gemini API is available
149
+ if not genai or not os.getenv("GEMINI_API_KEY"):
150
+ return dummy_analyze(ingredients_list, health_conditions)
151
+
152
+ # Create a prompt for Gemini
153
+ if health_conditions and health_conditions.strip():
154
+ prompt = f"""
155
+ Analyze the following food ingredients for a person with these health conditions: {health_conditions}
156
+ Ingredients: {ingredients_text}
157
+ For each ingredient:
158
+ 1. Provide its potential health benefits
159
+ 2. Identify any potential risks
160
+ 3. Note if it may affect the specified health conditions
161
+ Then provide an overall assessment of the product's suitability for someone with the specified health conditions.
162
+ Format your response in markdown with clear headings and sections.
163
+ """
164
+ else:
165
+ prompt = f"""
166
+ Analyze the following food ingredients:
167
+ Ingredients: {ingredients_text}
168
+ For each ingredient:
169
+ 1. Provide its potential health benefits
170
+ 2. Identify any potential risks or common allergens associated with it
171
+ Then provide an overall assessment of the product's general health profile.
172
+ Format your response in markdown with clear headings and sections.
173
+ """
174
+
175
+ try:
176
+ # First, check available models
177
+ try:
178
+ models = genai.list_models()
179
+ available_models = [m.name for m in models]
180
+
181
+ # Try models in order of preference
182
+ model_names = ['gemini-pro', 'gemini-1.5-pro', 'gemini-1.0-pro']
183
+
184
+ # Find first available model from our preference list
185
+ model_name = None
186
+ for name in model_names:
187
+ if any(name in m for m in available_models):
188
+ model_name = name
189
+ break
190
+
191
+ # If none of our preferred models are available, use the first available model
192
+ if not model_name and available_models:
193
+ model_name = available_models[0]
194
+
195
+ if not model_name:
196
+ return dummy_analyze(ingredients_list, health_conditions) + "\n\n(Using fallback analysis: No available models found)"
197
+
198
+ model = genai.GenerativeModel(model_name)
199
+ response = model.generate_content(prompt)
200
+
201
+ # Check if response is valid
202
+ if hasattr(response, 'text') and response.text:
203
+ analysis = response.text
204
+ else:
205
+ return dummy_analyze(ingredients_list, health_conditions) + "\n\n(Using fallback analysis: Empty API response)"
206
+
207
+ except Exception as e:
208
+ return dummy_analyze(ingredients_list, health_conditions) + f"\n\n(Using fallback analysis: {str(e)})"
209
+
210
+ # Add disclaimer
211
+ disclaimer = """
212
+ ## Disclaimer
213
+ This analysis is provided for informational purposes only and should not replace professional medical advice.
214
+ Always consult with a healthcare provider regarding dietary restrictions, allergies, or health conditions.
215
+ """
216
+
217
+ return analysis + disclaimer
218
+
219
+ except Exception as e:
220
+ # Fallback to basic analysis if API call fails
221
+ return dummy_analyze(ingredients_list, health_conditions) + f"\n\n(Using fallback analysis: {str(e)})"
222
+ # Dummy analysis function for when API is not available
223
+ def dummy_analyze(ingredients_list, health_conditions=None):
224
+ ingredients_text = ", ".join(ingredients_list)
225
+
226
+ report = f"""
227
+ # Ingredient Analysis Report
228
+ ## Detected Ingredients
229
+ {", ".join([i.title() for i in ingredients_list])}
230
+ ## Overview
231
+ This is a simulated analysis since no API key was provided. In the actual application,
232
+ the ingredients would be analyzed by an LLM for their health implications.
233
+ ## Health Considerations
234
+ """
235
+
236
+ if health_conditions:
237
+ report += f"""
238
+ The analysis would specifically consider these health concerns: {health_conditions}
239
+ """
240
+ else:
241
+ report += """
242
+ No specific health concerns were provided, so a general analysis would be performed.
243
+ """
244
+
245
+ report += """
246
+ ## Disclaimer
247
+ This analysis is provided for informational purposes only and should not replace professional medical advice.
248
+ Always consult with a healthcare provider regarding dietary restrictions, allergies, or health conditions.
249
+ """
250
+
251
+ return report
252
+
253
+ # Function to process input based on method (camera, upload, or manual entry)
254
+ def process_input(input_method, text_input, camera_input, upload_input, health_conditions):
255
+ if input_method == "Camera":
256
+ if camera_input is not None:
257
+ extracted_text = extract_text_from_image(camera_input)
258
+ # If OCR fails, inform the user they can try manual entry
259
+ if "Error" in extracted_text or "No text could be extracted" in extracted_text:
260
+ return extracted_text + "\n\nPlease try using the 'Manual Entry' option instead."
261
+
262
+ ingredients = parse_ingredients(extracted_text)
263
+ return analyze_ingredients_with_gemini(ingredients, health_conditions)
264
+ else:
265
+ return "No camera image captured. Please try again."
266
+
267
+ elif input_method == "Image Upload":
268
+ if upload_input is not None:
269
+ extracted_text = extract_text_from_image(upload_input)
270
+ # If OCR fails, inform the user they can try manual entry
271
+ if "Error" in extracted_text or "No text could be extracted" in extracted_text:
272
+ return extracted_text + "\n\nPlease try using the 'Manual Entry' option instead."
273
+
274
+ ingredients = parse_ingredients(extracted_text)
275
+ return analyze_ingredients_with_gemini(ingredients, health_conditions)
276
+ else:
277
+ return "No image uploaded. Please try again."
278
+
279
+ elif input_method == "Manual Entry":
280
+ if text_input and text_input.strip():
281
+ ingredients = parse_ingredients(text_input)
282
+ return analyze_ingredients_with_gemini(ingredients, health_conditions)
283
+ else:
284
+ return "No ingredients entered. Please try again."
285
+
286
+ return "Please provide input using one of the available methods."
287
+
288
+ # Create the Gradio interface
289
+ with gr.Blocks(title="AI Ingredient Scanner") as app:
290
+ gr.Markdown("# AI Ingredient Scanner")
291
+ gr.Markdown("Scan product ingredients and analyze them for health benefits, risks, and potential allergens.")
292
+
293
+ with gr.Row():
294
+ with gr.Column():
295
+ input_method = gr.Radio(
296
+ ["Camera", "Image Upload", "Manual Entry"],
297
+ label="Input Method",
298
+ value="Camera"
299
+ )
300
+
301
+ # Camera input
302
+ camera_input = gr.Image(label="Capture ingredients with camera", type="pil", visible=True)
303
+
304
+ # Image upload
305
+ upload_input = gr.Image(label="Upload image of ingredients label", type="pil", visible=False)
306
+
307
+ # Text input
308
+ text_input = gr.Textbox(
309
+ label="Enter ingredients list (comma separated)",
310
+ placeholder="milk, sugar, flour, eggs, vanilla extract",
311
+ lines=3,
312
+ visible=False
313
+ )
314
+
315
+ # Health conditions input - now optional and more flexible
316
+ health_conditions = gr.Textbox(
317
+ label="Enter your health concerns (optional)",
318
+ placeholder="diabetes, high blood pressure, peanut allergy, etc.",
319
+ lines=2,
320
+ info="The AI will automatically analyze ingredients for these conditions"
321
+ )
322
+
323
+ analyze_button = gr.Button("Analyze Ingredients")
324
+
325
+ with gr.Column():
326
+ output = gr.Markdown(label="Analysis Results")
327
+ extracted_text_output = gr.Textbox(label="Extracted Text (for verification)", lines=3)
328
+
329
+ # Show/hide inputs based on selection
330
+ def update_visible_inputs(choice):
331
+ return {
332
+ upload_input: gr.update(visible=(choice == "Image Upload")),
333
+ camera_input: gr.update(visible=(choice == "Camera")),
334
+ text_input: gr.update(visible=(choice == "Manual Entry"))
335
+ }
336
+
337
+ input_method.change(update_visible_inputs, input_method, [upload_input, camera_input, text_input])
338
+
339
+ # Extract and display the raw text (for verification purposes)
340
+ def show_extracted_text(input_method, text_input, camera_input, upload_input):
341
+ if input_method == "Camera" and camera_input is not None:
342
+ return extract_text_from_image(camera_input)
343
+ elif input_method == "Image Upload" and upload_input is not None:
344
+ return extract_text_from_image(upload_input)
345
+ elif input_method == "Manual Entry":
346
+ return text_input
347
+ return "No input detected"
348
+
349
+ # Set up event handlers
350
+ analyze_button.click(
351
+ fn=process_input,
352
+ inputs=[input_method, text_input, camera_input, upload_input, health_conditions],
353
+ outputs=output
354
+ )
355
+
356
+ analyze_button.click(
357
+ fn=show_extracted_text,
358
+ inputs=[input_method, text_input, camera_input, upload_input],
359
+ outputs=extracted_text_output
360
+ )
361
+
362
+ gr.Markdown("### How to use")
363
+ gr.Markdown("""
364
+ 1. Choose your input method (Camera, Image Upload, or Manual Entry)
365
+ 2. Take a photo of the ingredients label or enter ingredients manually
366
+ 3. Optionally enter your health concerns
367
+ 4. Click "Analyze Ingredients" to get your personalized analysis
368
+ The AI will automatically analyze the ingredients, their health implications, and their potential impact on your specific health concerns.
369
+ """)
370
+
371
+ gr.Markdown("### Examples of what you can ask")
372
+ gr.Markdown("""
373
+ The system can handle a wide range of health concerns, such as:
374
+ - General health goals: "trying to reduce sugar intake" or "watching sodium levels"
375
+ - Medical conditions: "diabetes" or "hypertension"
376
+ - Allergies: "peanut allergy" or "shellfish allergy"
377
+ - Dietary restrictions: "vegetarian" or "gluten-free diet"
378
+ - Multiple conditions: "diabetes, high cholesterol, and lactose intolerance"
379
+ The AI will tailor its analysis to your specific needs.
380
+ """)
381
+
382
+ gr.Markdown("### Tips for best results")
383
+ gr.Markdown("""
384
+ - Hold the camera steady and ensure good lighting
385
+ - Focus directly on the ingredients list
386
+ - Make sure the text is clear and readable
387
+ - Be specific about your health concerns for more targeted analysis
388
+ """)
389
+
390
+ gr.Markdown("### Disclaimer")
391
+ gr.Markdown("""
392
+ This tool is for informational purposes only and should not replace professional medical advice.
393
+ Always consult with a healthcare provider regarding dietary restrictions, allergies, or health conditions.
394
+ """)
395
+
396
+ # Launch the app
397
  if __name__ == "__main__":
398
+ app.launch()