Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,7 @@ from llama_index.core import (
|
|
14 |
Settings,
|
15 |
set_global_handler
|
16 |
)
|
|
|
17 |
from llama_index.llms.openai import OpenAI
|
18 |
from openai import OpenAI as OpenAIClient
|
19 |
|
@@ -22,6 +23,8 @@ import base64
|
|
22 |
import json
|
23 |
from PIL import Image
|
24 |
from io import BytesIO
|
|
|
|
|
25 |
|
26 |
|
27 |
set_global_handler("simple") # imposta un handler semplice per il logging
|
@@ -57,6 +60,12 @@ class BasicAgent:
|
|
57 |
}
|
58 |
}
|
59 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
|
62 |
# Crea un'istanza di OpenAI
|
@@ -71,6 +80,7 @@ class BasicAgent:
|
|
71 |
|
72 |
# Imposta le impostazioni tramite Settings
|
73 |
Settings.llm=llm
|
|
|
74 |
|
75 |
self.client = OpenAIClient(api_key=openai_api_key)
|
76 |
|
@@ -356,6 +366,19 @@ def transcribe_audio(file_name: str) -> str:
|
|
356 |
print_coso(f"transcribe_audio tool result: {result['text']}")
|
357 |
return result["text"]
|
358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
359 |
|
360 |
|
361 |
|
|
|
14 |
Settings,
|
15 |
set_global_handler
|
16 |
)
|
17 |
+
from llama_index.tools.function_tool import FunctionTool
|
18 |
from llama_index.llms.openai import OpenAI
|
19 |
from openai import OpenAI as OpenAIClient
|
20 |
|
|
|
23 |
import json
|
24 |
from PIL import Image
|
25 |
from io import BytesIO
|
26 |
+
from typing import List
|
27 |
+
import re
|
28 |
|
29 |
|
30 |
set_global_handler("simple") # imposta un handler semplice per il logging
|
|
|
60 |
}
|
61 |
}
|
62 |
]
|
63 |
+
|
64 |
+
ingredient_tool = FunctionTool.from_defaults(
|
65 |
+
name="extract_ingredients",
|
66 |
+
fn=extract_ingredients,
|
67 |
+
description="Extracts and returns a comma-separated, alphabetized list of ingredients for a pie filling from a transcription string."
|
68 |
+
)
|
69 |
|
70 |
|
71 |
# Crea un'istanza di OpenAI
|
|
|
80 |
|
81 |
# Imposta le impostazioni tramite Settings
|
82 |
Settings.llm=llm
|
83 |
+
Settings.tools = [ingredient_tool]
|
84 |
|
85 |
self.client = OpenAIClient(api_key=openai_api_key)
|
86 |
|
|
|
366 |
print_coso(f"transcribe_audio tool result: {result['text']}")
|
367 |
return result["text"]
|
368 |
|
369 |
+
|
370 |
+
def extract_ingredients(transcription: str) -> str:
|
371 |
+
"""
|
372 |
+
Estrae una lista alfabetica, separata da virgole, di ingredienti dal testo fornito,
|
373 |
+
mantenendo le descrizioni (es. 'freshly squeezed lemon juice').
|
374 |
+
"""
|
375 |
+
# pattern semplice per ingredienti comuni e le loro descrizioni
|
376 |
+
pattern = r"\b(?:a dash of |a pinch of |freshly squeezed |pure )?[a-zA-Z ]+?(?:strawberries|sugar|lemon juice|cornstarch|vanilla extract)\b"
|
377 |
+
matches = re.findall(pattern, transcription.lower())
|
378 |
+
|
379 |
+
# normalizza, rimuove duplicati e ordina
|
380 |
+
unique_ingredients = sorted(set(match.strip() for match in matches))
|
381 |
+
return ", ".join(unique_ingredients)
|
382 |
|
383 |
|
384 |
|