|
import gradio as gr |
|
|
|
from llama_cpp import Llama |
|
|
|
llm = Llama.from_pretrained( |
|
repo_id="ID2223JR/gguf_model_q4", |
|
filename="unsloth.Q4_K_M.gguf", |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ingredients_list = [] |
|
|
|
|
|
|
|
def add_ingredient(ingredient, quantity): |
|
if ingredient and quantity > 0: |
|
ingredients_list.append(f"{ingredient}, {quantity} grams") |
|
return ( |
|
"\n".join(ingredients_list), |
|
gr.update(value="", interactive=True), |
|
gr.update(value=None, interactive=True), |
|
) |
|
|
|
|
|
|
|
def validate_inputs(ingredient, quantity): |
|
if ingredient and quantity is not None and quantity > 0: |
|
return gr.update(interactive=True) |
|
return gr.update(interactive=False) |
|
|
|
|
|
def submit_to_model(): |
|
if not ingredients_list: |
|
yield "Ingredients list is empty! Please add ingredients first." |
|
return |
|
|
|
prompt = f"Using the following ingredients, suggest a recipe:\n\n" + "\n".join( |
|
ingredients_list |
|
) |
|
|
|
try: |
|
response = llm.create_chat_completion( |
|
messages=[ |
|
{ |
|
"role": "system", |
|
"content": ( |
|
"You are a world-renowned chef, celebrated for your expertise in creating delectable dishes from diverse cuisines. You have a vast knowledge of ingredients, cooking techniques, and dietary preferences. Your role is to suggest personalized recipes based on the ingredients available, dietary restrictions, or specific meal requests. Please provide clear, step-by-step instructions and any useful tips to enhance the dish's flavor or presentation. Begin by introducing the recipe and why it’s a great choice." |
|
), |
|
}, |
|
{"role": "user", "content": prompt}, |
|
], |
|
stream=True, |
|
) |
|
|
|
content = "" |
|
|
|
for partial_response in response: |
|
|
|
content += partial_response["choices"][0]["delta"].get("content", "") |
|
|
|
if content: |
|
yield content |
|
|
|
ingredients_list.clear() |
|
|
|
except Exception as e: |
|
yield f"An error occurred: {str(e)}" |
|
|
|
|
|
|
|
def app(): |
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
ingredient_input = gr.Textbox( |
|
label="Ingredient", placeholder="Enter ingredient name" |
|
) |
|
quantity_input = gr.Number(label="Quantity (grams)", value=None) |
|
|
|
add_button = gr.Button("Add Ingredient", interactive=False) |
|
output = gr.Textbox(label="Ingredients List", lines=10, interactive=False) |
|
|
|
with gr.Row(): |
|
submit_button = gr.Button("Submit") |
|
model_output = gr.Textbox( |
|
label="Recipe Suggestion", lines=25, interactive=False |
|
) |
|
|
|
|
|
ingredient_input.change( |
|
validate_inputs, [ingredient_input, quantity_input], add_button |
|
) |
|
quantity_input.change( |
|
validate_inputs, [ingredient_input, quantity_input], add_button |
|
) |
|
|
|
|
|
add_button.click( |
|
add_ingredient, |
|
[ingredient_input, quantity_input], |
|
[output, ingredient_input, quantity_input], |
|
) |
|
|
|
|
|
submit_button.click( |
|
submit_to_model, |
|
inputs=None, |
|
outputs=model_output, |
|
) |
|
|
|
return demo |
|
|
|
|
|
demo = app() |
|
demo.launch() |
|
|