jedeland commited on
Commit
1f04f0d
·
1 Parent(s): db8f193
Files changed (1) hide show
  1. mistral_inference.py +107 -0
mistral_inference.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import InferenceClient
3
+
4
+ import gradio as gr
5
+
6
+ client = InferenceClient(api_key=os.getenv("hf_token"))
7
+
8
+ # Data storage
9
+
10
+ ingredients_list = []
11
+
12
+
13
+ # Function to add ingredient
14
+ def add_ingredient(ingredient, quantity):
15
+ if ingredient and quantity > 0:
16
+ ingredients_list.append(f"{ingredient}, {quantity} grams")
17
+ return (
18
+ "\n".join(ingredients_list),
19
+ gr.update(value="", interactive=True),
20
+ gr.update(value=None, interactive=True),
21
+ )
22
+
23
+
24
+ # Function to enable/disable add button
25
+ def validate_inputs(ingredient, quantity):
26
+ if ingredient and quantity is not None and quantity > 0:
27
+ return gr.update(interactive=True)
28
+ return gr.update(interactive=False)
29
+
30
+
31
+ def submit_to_model():
32
+ if not ingredients_list:
33
+ yield "Ingredients list is empty! Please add ingredients first."
34
+ return
35
+
36
+ prompt = f"Using the following ingredients, suggest a recipe:\n\n" + "\n".join(
37
+ ingredients_list
38
+ )
39
+ messages = [
40
+ {
41
+ "role": "system",
42
+ "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.",
43
+ },
44
+ {"role": "user", "content": prompt},
45
+ ]
46
+
47
+ stream = client.chat.completions.create(
48
+ model="mistralai/Mistral-7B-Instruct-v0.3",
49
+ messages=messages,
50
+ max_tokens=500,
51
+ stream=True,
52
+ )
53
+
54
+ content = ""
55
+
56
+ for chunk in stream:
57
+ content += chunk.choices[0].delta.content
58
+ yield content
59
+
60
+ ingredients_list.clear()
61
+
62
+
63
+ # App
64
+ def app():
65
+ with gr.Blocks() as demo:
66
+ with gr.Row():
67
+ ingredient_input = gr.Textbox(
68
+ label="Ingredient", placeholder="Enter ingredient name"
69
+ )
70
+ quantity_input = gr.Number(label="Quantity (grams)", value=None)
71
+
72
+ add_button = gr.Button("Add Ingredient", interactive=False)
73
+ output = gr.Textbox(label="Ingredients List", lines=10, interactive=False)
74
+
75
+ with gr.Row():
76
+ submit_button = gr.Button("Submit")
77
+ model_output = gr.Textbox(
78
+ label="Recipe Suggestion", lines=25, interactive=False
79
+ )
80
+
81
+ # Validate inputs
82
+ ingredient_input.change(
83
+ validate_inputs, [ingredient_input, quantity_input], add_button
84
+ )
85
+ quantity_input.change(
86
+ validate_inputs, [ingredient_input, quantity_input], add_button
87
+ )
88
+
89
+ # Add ingredient logic
90
+ add_button.click(
91
+ add_ingredient,
92
+ [ingredient_input, quantity_input],
93
+ [output, ingredient_input, quantity_input],
94
+ )
95
+
96
+ # Submit to model logic
97
+ submit_button.click(
98
+ submit_to_model,
99
+ inputs=None, # No inputs required as it uses the global ingredients_list
100
+ outputs=model_output,
101
+ )
102
+
103
+ return demo
104
+
105
+
106
+ demo = app()
107
+ demo.launch()