Spaces:
Sleeping
Sleeping
File size: 3,353 Bytes
3aca00a fabaef0 3aca00a fabaef0 3aca00a fabaef0 3aca00a fabaef0 3aca00a fabaef0 3aca00a fabaef0 3aca00a fabaef0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import os
from typing import List, Optional
import gradio as gr
from dotenv import load_dotenv
import instructor
from pydantic import BaseModel
from openai import OpenAI
from groq import Groq
load_dotenv()
groq_client = instructor.from_groq(Groq(), mode=instructor.Mode.JSON)
QUERY = """
extract all singular ingredients from the provided ingredient string.
While doing so, shorten the ingredient name to only the essential part.
Do not translate the names.
"""
STIMULI = "This is very important to my career."
class Ingredient(BaseModel):
"""
Represents an ingredient with a name and an optional amount.
Attributes:
name (str): The name of the ingredient.
amount (Optional[float]): The amount of the ingredient. This can be None if the amount is not specified.
"""
name: str
amount: Optional[float]
is_allergen: Optional[bool]
class Ingredients(BaseModel):
"""
Ingredients model that contains a list of Ingredient objects.
Attributes:
contains (List[Ingredient]): A list of Ingredient objects.
"""
contains: List[Ingredient]
# client = instructor.from_openai(OpenAI(), mode=instructor.Mode.JSON)
def predict(ingredients: str, openai_key: str = "") -> Ingredients:
"""
Predicts the ingredients using a GPT-3.5-turbo model.
Args:
ingredients (str): A string containing the ingredients to be parsed.
Returns:
Ingredients: The parsed ingredients in the form of an Ingredients object.
"""
if openai_key == "":
return groq_client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=[
{
"role": "user",
"content": f"""
{QUERY}
{ingredients}
{STIMULI}
""",
},
],
response_model=Ingredients,
temperature=0.0,
).model_dump_json(exclude_unset=True, exclude_none=True)
client = OpenAI(api_key=openai_key)
return (
client.beta.chat.completions.parse(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": f"""
{QUERY}
{ingredients}
{STIMULI}
""",
},
],
response_format=Ingredients,
temperature=0.0,
)
.choices[0]
.message.parsed.model_dump_json(exclude_unset=True, exclude_none=True)
)
demo = gr.Interface(
fn=predict,
inputs=["text", gr.Text(label="OpenAI API Key", type="password")],
outputs="json",
examples=[
[
"97,2 % DINKELVOLLKORNMEHL, GERSTENMALZMEHL, Salz",
"",
],
[
"BIO Grüne Linsen. Grüne Linsen aus kontrolliert biologischem Anbau. Kühl bei unter +30°C lagern. Nicht-EU-Landwirtschaft.",
"",
],
[
"Milchschokoladenkuvertüre, Kakao: 40,5% mindestens. Zucker, Kakaobutter, VOLLMILCHPULVER, Kakaomasse (Ghana), Emulgator: SOJALECITHIN, natürliches Vanillepulver. Trocken und kühl bei +17°C bis +18°C lagern. Eigenschaften: Eiweiß aus tierischer Milch.",
"",
],
],
)
demo.launch()
|