GoodLuckChuck commited on
Commit
af0717b
·
verified ·
1 Parent(s): cca1508

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -1,6 +1,6 @@
1
- # This version uses a multi-agent system to plan dinners, analyze local PDF recipes,
2
  # and optionally find new recipes from the internet using OpenAI's GPT-3.5-Turbo.
3
- # UPDATED FOR HUGGING FACE SPACES WITH GRADIO
4
 
5
  import os
6
  import random
@@ -15,6 +15,8 @@ import pypdf
15
  import requests
16
  from bs4 import BeautifulSoup
17
  import gradio as gr # --- Added for Hugging Face UI
 
 
18
 
19
  # --- Setup ---
20
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -48,7 +50,7 @@ class Recipe(BaseModel):
48
  servings: Optional[int] = None
49
  source_url: Optional[str] = None
50
 
51
- # --- Core Components (Unchanged) ---
52
 
53
  def load_prompt_from_xml(file_path: str) -> dict:
54
  """Loads and parses the prompt from an XML file."""
@@ -102,7 +104,15 @@ def extract_text_from_pdf(file_path: str) -> str:
102
  logger.error(f"Error reading PDF {file_path}: {e}")
103
  return ""
104
 
105
- # --- Agent Definitions (Unchanged) ---
 
 
 
 
 
 
 
 
106
 
107
  class RecipeConverterAgent:
108
  """An agent that converts raw text into structured Recipe objects using an LLM."""
@@ -149,6 +159,7 @@ class FileSystemRecipeAgent:
149
  for filename in os.listdir(self.recipe_dir):
150
  file_path = os.path.join(self.recipe_dir, filename)
151
  content = ""
 
152
  if filename.endswith('.pdf'):
153
  content = extract_text_from_pdf(file_path)
154
  elif filename.endswith(('.txt', '.json')):
@@ -157,6 +168,8 @@ class FileSystemRecipeAgent:
157
  content = f.read()
158
  except Exception as e:
159
  logger.error(f"Error reading text file {filename}: {e}")
 
 
160
 
161
  if content:
162
  recipes_content.append({
@@ -301,6 +314,7 @@ iface = gr.Interface(
301
  title="AI Dinner Planner",
302
  description="Let the AI plan your next three dinners. Check the box to have the agent search the web for one new recipe to include in the suggestions.",
303
  flagging_mode="never",
 
304
  )
305
 
306
  iface.launch()
 
1
+ # This version uses a multi-agent system to plan dinners, analyze local PDF/Image recipes,
2
  # and optionally find new recipes from the internet using OpenAI's GPT-3.5-Turbo.
3
+ # UPDATED FOR HUGGING FACE SPACES WITH GRADIO & OCR
4
 
5
  import os
6
  import random
 
15
  import requests
16
  from bs4 import BeautifulSoup
17
  import gradio as gr # --- Added for Hugging Face UI
18
+ from PIL import Image # --- Added for OCR
19
+ import pytesseract # --- Added for OCR
20
 
21
  # --- Setup ---
22
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
50
  servings: Optional[int] = None
51
  source_url: Optional[str] = None
52
 
53
+ # --- Core Components ---
54
 
55
  def load_prompt_from_xml(file_path: str) -> dict:
56
  """Loads and parses the prompt from an XML file."""
 
104
  logger.error(f"Error reading PDF {file_path}: {e}")
105
  return ""
106
 
107
+ def extract_text_from_image(file_path: str) -> str:
108
+ """Extracts text from an image file (JPEG, PNG, etc.) using OCR."""
109
+ try:
110
+ return pytesseract.image_to_string(Image.open(file_path))
111
+ except Exception as e:
112
+ logger.error(f"Error processing image {file_path} with OCR: {e}")
113
+ return ""
114
+
115
+ # --- Agent Definitions ---
116
 
117
  class RecipeConverterAgent:
118
  """An agent that converts raw text into structured Recipe objects using an LLM."""
 
159
  for filename in os.listdir(self.recipe_dir):
160
  file_path = os.path.join(self.recipe_dir, filename)
161
  content = ""
162
+ # --- UPDATED TO INCLUDE OCR ---
163
  if filename.endswith('.pdf'):
164
  content = extract_text_from_pdf(file_path)
165
  elif filename.endswith(('.txt', '.json')):
 
168
  content = f.read()
169
  except Exception as e:
170
  logger.error(f"Error reading text file {filename}: {e}")
171
+ elif filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')):
172
+ content = extract_text_from_image(file_path)
173
 
174
  if content:
175
  recipes_content.append({
 
314
  title="AI Dinner Planner",
315
  description="Let the AI plan your next three dinners. Check the box to have the agent search the web for one new recipe to include in the suggestions.",
316
  flagging_mode="never",
317
+ # examples=[[False], [True]] # Commented out to prevent startup crash
318
  )
319
 
320
  iface.launch()