Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,6 +23,72 @@ import pillow_heif # --- ADDED TO SUPPORT HEIC/HEIF IMAGE FORMATS
|
|
| 23 |
# This line registers the HEIC/HEIF plugin with Pillow
|
| 24 |
pillow_heif.register_heif_opener()
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# --- Path Configuration ---
|
| 27 |
# Determine if we're running in a Hugging Face Space
|
| 28 |
IS_SPACE = os.getenv('SPACE_ID') is not None
|
|
@@ -50,9 +116,8 @@ PROMPT_FILE_RECIPE_CONVERTER = os.path.join(BASE_DIR, "prompts", "recipe_convert
|
|
| 50 |
PROMPT_FILE_WEB_SEARCH = os.path.join(BASE_DIR, "prompts", "web_search_query_generator.xml")
|
| 51 |
RECIPE_DIR = os.path.join(BASE_DIR, "recipes")
|
| 52 |
|
| 53 |
-
# Ensure
|
| 54 |
-
|
| 55 |
-
os.makedirs(RECIPE_DIR, exist_ok=True)
|
| 56 |
|
| 57 |
GPT_MODEL = "gpt-3.5-turbo"
|
| 58 |
|
|
|
|
| 23 |
# This line registers the HEIC/HEIF plugin with Pillow
|
| 24 |
pillow_heif.register_heif_opener()
|
| 25 |
|
| 26 |
+
def ensure_prompt_files_exist():
|
| 27 |
+
"""Ensures that the necessary prompt files exist, creating them with default content if they don't."""
|
| 28 |
+
default_recipe_converter = '''<prompt>
|
| 29 |
+
<role_definition>
|
| 30 |
+
<role>You are an expert recipe parsing AI model.</role>
|
| 31 |
+
<purpose>Your primary function is to meticulously analyze raw text from various sources (text files, PDFs, web scrapes) and transform it into a structured JSON object representing a single, coherent recipe.</purpose>
|
| 32 |
+
</role_definition>
|
| 33 |
+
|
| 34 |
+
<instructions>
|
| 35 |
+
<item>Your output MUST be a valid JSON array containing one or more recipe objects.</item>
|
| 36 |
+
<item>From the provided text, extract the following fields for each recipe:</item>
|
| 37 |
+
<field name="name">A descriptive and clear name for the recipe.</field>
|
| 38 |
+
<field name="description">A brief summary of the dish.</field>
|
| 39 |
+
<field name="ingredients">A list of strings, each with quantity and ingredient.</field>
|
| 40 |
+
<field name="instructions">A list of strings, each a single cooking step.</field>
|
| 41 |
+
<field name="cooking_time">Total preparation time (if available).</field>
|
| 42 |
+
<field name="servings">Number of servings (if available).</field>
|
| 43 |
+
</instructions>
|
| 44 |
+
|
| 45 |
+
<user_query_template>
|
| 46 |
+
Analyze the following recipe content and convert it into a structured JSON array.
|
| 47 |
+
|
| 48 |
+
Recipe Content:
|
| 49 |
+
{files_context}
|
| 50 |
+
|
| 51 |
+
User Preferences:
|
| 52 |
+
- Meal type: {meal_type}
|
| 53 |
+
- Restrictions: {restrictions}
|
| 54 |
+
</user_query_template>
|
| 55 |
+
</prompt>'''
|
| 56 |
+
|
| 57 |
+
default_web_search = '''<prompt>
|
| 58 |
+
<role_definition>
|
| 59 |
+
<role>You are a creative and effective search query generator for finding recipes online.</role>
|
| 60 |
+
<purpose>Your purpose is to generate a single, concise Google search query based on user preferences and existing recipes to find a new and interesting recipe.</purpose>
|
| 61 |
+
</role_definition>
|
| 62 |
+
|
| 63 |
+
<instructions>
|
| 64 |
+
<item>Your output MUST be only the search query text.</item>
|
| 65 |
+
<item>Consider all aspects of the user's preferences, including cuisine and dietary restrictions.</item>
|
| 66 |
+
<item>Avoid generating queries too similar to existing recipes.</item>
|
| 67 |
+
</instructions>
|
| 68 |
+
|
| 69 |
+
<user_query_template>
|
| 70 |
+
Based on these user preferences: {preferences}
|
| 71 |
+
And their existing saved recipes (titles): {existing_recipes}
|
| 72 |
+
|
| 73 |
+
Generate a single, concise Google search query to find a new, exciting recipe they might like.
|
| 74 |
+
</user_query_template>
|
| 75 |
+
</prompt>'''
|
| 76 |
+
|
| 77 |
+
# Create directories if they don't exist
|
| 78 |
+
os.makedirs(os.path.dirname(PROMPT_FILE_RECIPE_CONVERTER), exist_ok=True)
|
| 79 |
+
os.makedirs(RECIPE_DIR, exist_ok=True)
|
| 80 |
+
|
| 81 |
+
# Create prompt files if they don't exist
|
| 82 |
+
if not os.path.exists(PROMPT_FILE_RECIPE_CONVERTER):
|
| 83 |
+
logger.info(f"Creating default recipe converter prompt file at {PROMPT_FILE_RECIPE_CONVERTER}")
|
| 84 |
+
with open(PROMPT_FILE_RECIPE_CONVERTER, 'w') as f:
|
| 85 |
+
f.write(default_recipe_converter)
|
| 86 |
+
|
| 87 |
+
if not os.path.exists(PROMPT_FILE_WEB_SEARCH):
|
| 88 |
+
logger.info(f"Creating default web search prompt file at {PROMPT_FILE_WEB_SEARCH}")
|
| 89 |
+
with open(PROMPT_FILE_WEB_SEARCH, 'w') as f:
|
| 90 |
+
f.write(default_web_search)
|
| 91 |
+
|
| 92 |
# --- Path Configuration ---
|
| 93 |
# Determine if we're running in a Hugging Face Space
|
| 94 |
IS_SPACE = os.getenv('SPACE_ID') is not None
|
|
|
|
| 116 |
PROMPT_FILE_WEB_SEARCH = os.path.join(BASE_DIR, "prompts", "web_search_query_generator.xml")
|
| 117 |
RECIPE_DIR = os.path.join(BASE_DIR, "recipes")
|
| 118 |
|
| 119 |
+
# Ensure all necessary files exist
|
| 120 |
+
ensure_prompt_files_exist()
|
|
|
|
| 121 |
|
| 122 |
GPT_MODEL = "gpt-3.5-turbo"
|
| 123 |
|