Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
fc5a259
1
Parent(s):
b5edbe6
working ingredient analysis with allergic and diet type
Browse files
db/models.py
CHANGED
|
@@ -15,6 +15,8 @@ class Ingredient(Base):
|
|
| 15 |
safety_rating = Column(Integer, nullable=True)
|
| 16 |
description = Column(Text, nullable=True)
|
| 17 |
health_effects = Column(JSON, nullable=True)
|
|
|
|
|
|
|
| 18 |
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
| 19 |
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
| 20 |
|
|
|
|
| 15 |
safety_rating = Column(Integer, nullable=True)
|
| 16 |
description = Column(Text, nullable=True)
|
| 17 |
health_effects = Column(JSON, nullable=True)
|
| 18 |
+
allergic_info = Column(JSON, nullable=True)
|
| 19 |
+
diet_type = Column(String, nullable=True)
|
| 20 |
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
| 21 |
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
| 22 |
|
db/repositories.py
CHANGED
|
@@ -37,7 +37,9 @@ class IngredientRepository:
|
|
| 37 |
alternate_names=ingredient_data.alternate_names,
|
| 38 |
safety_rating=ingredient_data.safety_rating,
|
| 39 |
description=ingredient_data.description,
|
| 40 |
-
health_effects=ingredient_data.health_effects
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
self.db.add(db_ingredient)
|
| 43 |
self.db.commit()
|
|
|
|
| 37 |
alternate_names=ingredient_data.alternate_names,
|
| 38 |
safety_rating=ingredient_data.safety_rating,
|
| 39 |
description=ingredient_data.description,
|
| 40 |
+
health_effects=ingredient_data.health_effects,
|
| 41 |
+
allergic_info=ingredient_data.allergic_info,
|
| 42 |
+
diet_type=ingredient_data.diet_type
|
| 43 |
)
|
| 44 |
self.db.add(db_ingredient)
|
| 45 |
self.db.commit()
|
interfaces/ingredientModels.py
CHANGED
|
@@ -10,7 +10,9 @@ class IngredientAnalysisResult(BaseModel):
|
|
| 10 |
safety_rating: int = 5
|
| 11 |
description: str = "No information found."
|
| 12 |
health_effects: List[str] = Field(default_factory=lambda: ["Unknown"])
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
class Config:
|
| 16 |
from_attributes = True # Enable ORM mode
|
|
|
|
| 10 |
safety_rating: int = 5
|
| 11 |
description: str = "No information found."
|
| 12 |
health_effects: List[str] = Field(default_factory=lambda: ["Unknown"])
|
| 13 |
+
allergic_info: Optional[List[str]] = None # New field
|
| 14 |
+
diet_type: Optional[str] = None # New field
|
| 15 |
+
details_with_source: List[Dict[str, Any]] = Field(default_factory=list)
|
| 16 |
|
| 17 |
class Config:
|
| 18 |
from_attributes = True # Enable ORM mode
|
migrations/versions/1c28462c052a_added_allergic_and_diet_type_in_.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""added allergic and diet type in ingredient
|
| 2 |
+
|
| 3 |
+
Revision ID: 1c28462c052a
|
| 4 |
+
Revises: aaceebfe9545
|
| 5 |
+
Create Date: 2025-04-12 21:40:50.748853
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from typing import Sequence, Union
|
| 9 |
+
|
| 10 |
+
from alembic import op
|
| 11 |
+
import sqlalchemy as sa
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# revision identifiers, used by Alembic.
|
| 15 |
+
revision: str = '1c28462c052a'
|
| 16 |
+
down_revision: Union[str, None] = 'aaceebfe9545'
|
| 17 |
+
branch_labels: Union[str, Sequence[str], None] = None
|
| 18 |
+
depends_on: Union[str, Sequence[str], None] = None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def upgrade() -> None:
|
| 22 |
+
"""Upgrade schema."""
|
| 23 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
+
op.add_column('ingredients', sa.Column('allergic_info', sa.JSON(), nullable=True))
|
| 25 |
+
op.add_column('ingredients', sa.Column('diet_type', sa.String(), nullable=True))
|
| 26 |
+
# ### end Alembic commands ###
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def downgrade() -> None:
|
| 30 |
+
"""Downgrade schema."""
|
| 31 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 32 |
+
op.drop_column('ingredients', 'diet_type')
|
| 33 |
+
op.drop_column('ingredients', 'allergic_info')
|
| 34 |
+
# ### end Alembic commands ###
|
services/ingredientFinderAgent.py
CHANGED
|
@@ -208,7 +208,7 @@ def search_web(ingredient: str) -> Dict[str, Any]:
|
|
| 208 |
|
| 209 |
try:
|
| 210 |
duckduckgo = DuckDuckGoSearchRun()
|
| 211 |
-
search_queries = [f"{ingredient} food ingredient safety", f"{ingredient} E-number food additive"]
|
| 212 |
all_results = []
|
| 213 |
for query in search_queries:
|
| 214 |
result = duckduckgo.run(query)
|
|
@@ -318,7 +318,7 @@ def analyze_ingredient(state: IngredientState) -> IngredientState:
|
|
| 318 |
google_api_key=api_key,
|
| 319 |
model=model_name,
|
| 320 |
temperature=0.3, # Lower temperature for more factual responses
|
| 321 |
-
convert_system_message_to_human=True
|
| 322 |
)
|
| 323 |
except Exception as e:
|
| 324 |
logger.error(f"Error initializing LLM: {e}")
|
|
@@ -399,6 +399,8 @@ def analyze_ingredient(state: IngredientState) -> IngredientState:
|
|
| 399 |
2. List of potential health effects (both positive & negative, maximum 5 points)
|
| 400 |
3. Brief description of what this ingredient is, how it's used, and its properties
|
| 401 |
4. Alternative names for this ingredient
|
|
|
|
|
|
|
| 402 |
|
| 403 |
Available data:
|
| 404 |
{combined_data}
|
|
@@ -408,9 +410,11 @@ def analyze_ingredient(state: IngredientState) -> IngredientState:
|
|
| 408 |
- "health_effects": (array of strings)
|
| 409 |
- "description": (string)
|
| 410 |
- "alternate_names": (array of strings)
|
|
|
|
|
|
|
| 411 |
|
| 412 |
Only include factual information supported by the provided data. If information is
|
| 413 |
-
unavailable for any field, use appropriate default values.
|
| 414 |
"""
|
| 415 |
|
| 416 |
# Process with LLM
|
|
@@ -437,7 +441,9 @@ def analyze_ingredient(state: IngredientState) -> IngredientState:
|
|
| 437 |
"safety_rating": analysis.get("safety_rating", 5),
|
| 438 |
"description": analysis.get("description", "No description available."),
|
| 439 |
"health_effects": analysis.get("health_effects", []),
|
| 440 |
-
"alternate_names": analysis.get("alternate_names", [])
|
|
|
|
|
|
|
| 441 |
})
|
| 442 |
logger.info(f"Analysis complete - Safety Rating: {result['safety_rating']}")
|
| 443 |
else:
|
|
@@ -551,38 +557,38 @@ class IngredientInfoAgentLangGraph:
|
|
| 551 |
|
| 552 |
# Run each tool directly in sequence and collect results
|
| 553 |
logger.info(f"Searching local database for {ingredient}")
|
| 554 |
-
result = search_local_db(ingredient)
|
| 555 |
if result.get("found", False):
|
| 556 |
sources_data.append(result)
|
| 557 |
logger.info(f"Local DB found data for {ingredient}")
|
| 558 |
|
| 559 |
logger.info(f"Searching web for {ingredient}")
|
| 560 |
-
result = search_web(ingredient)
|
| 561 |
if result.get("found", False):
|
| 562 |
sources_data.append(result)
|
| 563 |
logger.info(f"Web search found data for {ingredient}")
|
| 564 |
|
| 565 |
logger.info(f"Searching Wikipedia for {ingredient}")
|
| 566 |
-
result = search_wikipedia(ingredient)
|
| 567 |
if result.get("found", False):
|
| 568 |
sources_data.append(result)
|
| 569 |
logger.info(f"Wikipedia found data for {ingredient}")
|
| 570 |
|
| 571 |
logger.info(f"Searching Open Food Facts for {ingredient}")
|
| 572 |
-
result = search_open_food_facts(ingredient)
|
| 573 |
if result.get("found", False):
|
| 574 |
sources_data.append(result)
|
| 575 |
logger.info(f"Open Food Facts found data for {ingredient}")
|
| 576 |
|
| 577 |
# Optional - Add these if needed:
|
| 578 |
logger.info(f"Searching USDA for {ingredient}")
|
| 579 |
-
result = search_usda(ingredient)
|
| 580 |
if result.get("found", False):
|
| 581 |
sources_data.append(result)
|
| 582 |
logger.info(f"USDA found data for {ingredient}")
|
| 583 |
|
| 584 |
logger.info(f"Searching PubChem for {ingredient}")
|
| 585 |
-
result = search_pubchem(ingredient)
|
| 586 |
if result.get("found", False):
|
| 587 |
sources_data.append(result)
|
| 588 |
logger.info(f"PubChem found data for {ingredient}")
|
|
|
|
| 208 |
|
| 209 |
try:
|
| 210 |
duckduckgo = DuckDuckGoSearchRun()
|
| 211 |
+
search_queries = [f"{ingredient} food ingredient safety", f"{ingredient} E-number food additive",f"{ingredient}'s allergic information",f"is {ingredient} vegan,vegetarian or Non-vegetarian"]
|
| 212 |
all_results = []
|
| 213 |
for query in search_queries:
|
| 214 |
result = duckduckgo.run(query)
|
|
|
|
| 318 |
google_api_key=api_key,
|
| 319 |
model=model_name,
|
| 320 |
temperature=0.3, # Lower temperature for more factual responses
|
| 321 |
+
# convert_system_message_to_human=True
|
| 322 |
)
|
| 323 |
except Exception as e:
|
| 324 |
logger.error(f"Error initializing LLM: {e}")
|
|
|
|
| 399 |
2. List of potential health effects (both positive & negative, maximum 5 points)
|
| 400 |
3. Brief description of what this ingredient is, how it's used, and its properties
|
| 401 |
4. Alternative names for this ingredient
|
| 402 |
+
5. Allergic information of the ingredient like which type of allergies we can got, etc.
|
| 403 |
+
6. Diet Type of that ingredient like Vegan, Vegetarian, Non-Vegetarian
|
| 404 |
|
| 405 |
Available data:
|
| 406 |
{combined_data}
|
|
|
|
| 410 |
- "health_effects": (array of strings)
|
| 411 |
- "description": (string)
|
| 412 |
- "alternate_names": (array of strings)
|
| 413 |
+
- "allergic_info": (array of strings)
|
| 414 |
+
- "diet_type" : (string from vegan,vegetarian,non-vegetarian,unknown)
|
| 415 |
|
| 416 |
Only include factual information supported by the provided data. If information is
|
| 417 |
+
unavailable for any field, use appropriate default values. But if information is "too obvious" you can fill appropriate information.
|
| 418 |
"""
|
| 419 |
|
| 420 |
# Process with LLM
|
|
|
|
| 441 |
"safety_rating": analysis.get("safety_rating", 5),
|
| 442 |
"description": analysis.get("description", "No description available."),
|
| 443 |
"health_effects": analysis.get("health_effects", []),
|
| 444 |
+
"alternate_names": analysis.get("alternate_names", []),
|
| 445 |
+
"allergic_info": analysis.get("allergic_info", []),
|
| 446 |
+
"diet_type": analysis.get("diet_type", "unknown"),
|
| 447 |
})
|
| 448 |
logger.info(f"Analysis complete - Safety Rating: {result['safety_rating']}")
|
| 449 |
else:
|
|
|
|
| 557 |
|
| 558 |
# Run each tool directly in sequence and collect results
|
| 559 |
logger.info(f"Searching local database for {ingredient}")
|
| 560 |
+
result = search_local_db.invoke(ingredient)
|
| 561 |
if result.get("found", False):
|
| 562 |
sources_data.append(result)
|
| 563 |
logger.info(f"Local DB found data for {ingredient}")
|
| 564 |
|
| 565 |
logger.info(f"Searching web for {ingredient}")
|
| 566 |
+
result = search_web.invoke(ingredient)
|
| 567 |
if result.get("found", False):
|
| 568 |
sources_data.append(result)
|
| 569 |
logger.info(f"Web search found data for {ingredient}")
|
| 570 |
|
| 571 |
logger.info(f"Searching Wikipedia for {ingredient}")
|
| 572 |
+
result = search_wikipedia.invoke(ingredient)
|
| 573 |
if result.get("found", False):
|
| 574 |
sources_data.append(result)
|
| 575 |
logger.info(f"Wikipedia found data for {ingredient}")
|
| 576 |
|
| 577 |
logger.info(f"Searching Open Food Facts for {ingredient}")
|
| 578 |
+
result = search_open_food_facts.invoke(ingredient)
|
| 579 |
if result.get("found", False):
|
| 580 |
sources_data.append(result)
|
| 581 |
logger.info(f"Open Food Facts found data for {ingredient}")
|
| 582 |
|
| 583 |
# Optional - Add these if needed:
|
| 584 |
logger.info(f"Searching USDA for {ingredient}")
|
| 585 |
+
result = search_usda.invoke(ingredient)
|
| 586 |
if result.get("found", False):
|
| 587 |
sources_data.append(result)
|
| 588 |
logger.info(f"USDA found data for {ingredient}")
|
| 589 |
|
| 590 |
logger.info(f"Searching PubChem for {ingredient}")
|
| 591 |
+
result = search_pubchem.invoke(ingredient)
|
| 592 |
if result.get("found", False):
|
| 593 |
sources_data.append(result)
|
| 594 |
logger.info(f"PubChem found data for {ingredient}")
|