Anand Sampat commited on
Commit
1d99a1c
·
1 Parent(s): 1179673

simplify launch

Browse files
Files changed (1) hide show
  1. app.py +237 -0
app.py CHANGED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import time
3
+ import gradio as gr
4
+ import base64
5
+ import openai
6
+ from openai import OpenAI
7
+
8
+ def describe_food_in_image(image, api_key, vision_model, progress=gr.Progress()):
9
+ start_time = time.time()
10
+ progress(0, desc="Initializing image analysis...")
11
+ sn_dev_client = OpenAI(
12
+ base_url="https://api.sambanova.ai/v1",
13
+ api_key=api_key
14
+ )
15
+
16
+ progress(0.2, desc="Converting image to base64...")
17
+ # Convert the image to base64
18
+ buffered = io.BytesIO()
19
+ image.save(buffered, format="PNG")
20
+ base64_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
21
+
22
+ progress(0.3, desc="Preparing API request...")
23
+ # Prepare the message with the image
24
+ messages = [
25
+ {
26
+ "role": "user",
27
+ "content": [
28
+ {
29
+ "type": "text",
30
+ "text": "What type of food is shown in this image? Please provide a brief description."
31
+ },
32
+ {
33
+ "type": "image_url",
34
+ "image_url": {
35
+ "url": f"data:image/png;base64,{base64_image}"
36
+ }
37
+ }
38
+ ]
39
+ }
40
+ ]
41
+
42
+ progress(0.4, desc="Analyzing image...")
43
+ # Make the API call
44
+ try:
45
+ response = sn_dev_client.chat.completions.create(
46
+ model=vision_model,
47
+ messages=messages,
48
+ max_tokens=150
49
+ )
50
+ progress(1.0, desc="Image analysis complete")
51
+ output = response.choices[0].message.content
52
+ # Return the model's description
53
+ thinking_time = time.time() - start_time
54
+ return output, thinking_time
55
+ except Exception as e:
56
+ error_message = f"Error: {str(e)}"
57
+ raise gr.Error(error_message)
58
+
59
+ def desc_to_ingredients_recipe(food_description, api_key, language_model, progress=gr.Progress()):
60
+ # Run the code through the SN API (and profile)
61
+ start_time = time.time()
62
+ progress(0.6, desc="Initializing recipe generation...")
63
+ # Sambanova API base URL
64
+ SN_API_BASE = "https://api.sambanova.ai/v1"
65
+ sn_client = openai.OpenAI(api_key=api_key, base_url=SN_API_BASE)
66
+ progress(0.7, desc="Preparing recipe prompt...")
67
+ # Create the prompt
68
+ budget = 10 # between 1-100
69
+ SYSTEM_PROMPT = f"""
70
+ You are a helpful assistant in normal conversation.
71
+ When given a problem to solve, you are an expert problem-solving assistant.
72
+ Your task is to provide a detailed, step-by-step solution to a given question with the following budget of {budget}.
73
+ Follow these instructions carefully:
74
+ 1. Read the given question carefully
75
+ 2. Generate a detailed, logical step-by-step solution.
76
+ 3. Each step should be formatted as in the example below.
77
+ 4. You are allowed to use at most {budget} steps (starting budget),
78
+ keep track of it by counting down from the budget.
79
+ STOP GENERATING MORE STEPS when hitting 0.
80
+ 5. Do a self-reflection when you are unsure about how to proceed,
81
+ based on the self-reflection and reward, decides whether you need to return
82
+ to the previous steps.
83
+ 6. After completing the solution steps, reorganize and synthesize the steps
84
+ into the final answer as in the format given below and do not mention the budget or steps you took.
85
+ 7. Provide a critical, honest and subjective self-evaluation of your reasoning
86
+ process at the end.
87
+
88
+ Output should be in Markdown format, for example if generating a recipe it would look like this:
89
+
90
+ # Recipe
91
+ Hi there! Today you'll learn about how to cook a delicious Thai delicacy, Pad Thai! For this you'll need
92
+
93
+ ## Ingredients
94
+ For the sauce:
95
+ * 1/2 cup tamarind paste
96
+ * 1/4 cup fish sauce
97
+ * 1/4 cup palm sugar (or brown sugar)
98
+ * 2 tablespoons soy sauce
99
+ * 2 tablespoons rice vinegar
100
+ * 1 tablespoon vegetable oil
101
+ * 2 cloves garlic, minced
102
+ * 1 tablespoon grated fresh ginger
103
+ * 1/4 teaspoon ground white pepper
104
+ * 2 tablespoons water
105
+ * Salt, to taste
106
+ * Fresh lime wedges, for serving
107
+
108
+ For the noodles:
109
+ * 1 cup rice stick noodles (preferably fresh)
110
+
111
+ For the protein:
112
+ * 1 cup mixed protein (shrimp, chicken, tofu, or a combination)
113
+
114
+ For the vegetables:
115
+ * 1 cup bean sprouts
116
+ * 1 cup sliced carrots
117
+ * 1 cup sliced red bell pepper
118
+ * 2 green onions, thinly sliced
119
+ * 1/4 cup chopped peanuts
120
+ * 2 tablespoons chopped fresh cilantro (optional)
121
+
122
+ ## Instructions
123
+ 1. Prepare the sauce: In a blender or food processor, combine tamarind paste, fish sauce, palm sugar, soy sauce, rice vinegar, garlic, ginger, and white pepper. Blend until smooth. Heat the sauce in a saucepan over medium heat, stirring constantly, until it thickens slightly. Remove from heat and stir in vegetable oil and water. Season with salt to taste.
124
+ 2. Cook the noodles: Soak the rice stick noodles in hot water for about 5-7 minutes, or according to package instructions. Drain and set aside.
125
+ 3. Prepare the protein: Cut the protein into bite-sized pieces and cook according to your preference (e.g., grill, sauté, or boil). Set aside.
126
+ 4. Sauté the vegetables: Heat 2 tablespoons of vegetable oil in a large wok or frying pan over medium-high heat. Add the bean sprouts, carrots, and red bell pepper. Cook, stirring constantly, until the vegetables are tender-crisp.
127
+ 5. Assemble the Pad Thai: Add the cooked noodles, protein, and sauce to the wok or frying pan. Stir-fry everything together for about 2-3 minutes, until the noodles are well coated with the sauce.
128
+ 6. Garnish and serve: Transfer the Pad Thai to a serving platter or individual plates. Sprinkle with green onions, peanuts, and cilantro (if using). Serve with fresh lime wedges on the side.
129
+
130
+ Tips and Variations:
131
+ * Use a variety of protein sources, such as shrimp, chicken, tofu, or a combination.
132
+ * Add other vegetables, such as mushrooms, zucchini, or baby corn.
133
+ * Use different types of noodles, such as rice vermicelli or egg noodles.
134
+ * Adjust the level of spiciness to your liking by adding more or less chili flakes.
135
+ * Serve with a sprinkle of toasted sesame seeds or chopped scallions for added flavor and texture.
136
+
137
+ Try it out for yourself, taste it and adjust to your liking. The best part of a good Pad Thai is making it your own!
138
+ """
139
+
140
+ PROMPT = f"Convert this description to a fixed list of ingredients and a list of steps in the ingredients: {food_description}"
141
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
142
+ messages.append({"role": "user", "content": PROMPT})
143
+
144
+ progress(0.8, desc="Generating recipe...")
145
+ try:
146
+ completion = sn_client.chat.completions.create(model=language_model, messages=messages)
147
+ response = completion.choices[0].message.content
148
+ thinking_time = time.time() - start_time
149
+ progress(0.9, desc="Recipe generation complete")
150
+ return response, thinking_time
151
+ except Exception as e:
152
+ error_message = f"Error: {str(e)}"
153
+ raise gr.Error(error_message)
154
+
155
+ def process_image(image, api_key, vision_model, language_model, progress=gr.Progress()):
156
+ if not api_key.strip():
157
+ raise ValueError("Please enter your SambaNova API key")
158
+ progress(0, desc="Starting image processing...")
159
+
160
+ progress(0.1, desc="Analyzing image...")
161
+ # Describe the food in the image
162
+ description, food_desc_time = describe_food_in_image(image, api_key, vision_model, progress)
163
+
164
+ progress(0.5, desc="Generating recipe...")
165
+ # Generate recipe from description
166
+ recipe, recipe_gen_time = desc_to_ingredients_recipe(description, api_key, language_model, progress)
167
+
168
+ progress(1.0, desc="Process complete")
169
+ return description, recipe, f"Time to response: {food_desc_time + recipe_gen_time:.2f} seconds"
170
+
171
+ # Define the Gradio interface using Blocks
172
+ with gr.Blocks() as iface:
173
+ gr.Markdown(
174
+ """
175
+ # Image to Recipe using SambaNova APIs 🚀
176
+
177
+ **Ever go to a restaurant or see some food on TV and think "I could make this at home!". Well now just upload or take a picture of your food and get a recipe to try!**
178
+
179
+ *To use this, follow the instructions below:*
180
+
181
+ 1. Navigate to <a href="https://cloud.sambanova.ai">https://cloud.sambanova.ai</a>, login and copy your API Key
182
+ 2. Paste it in the SambaNova API Key box
183
+ 3. (optional) Select a different vision or language model
184
+ 4. Click the camera to use your device's camera, or upload button and select an image file, or simply paste from clipboard
185
+ 5. It'll automatically start processing, wait for a few seconds for the LVM and LLM on SambaNova to run
186
+ 6. Read through and try your receipe 😋
187
+ """, container=True
188
+ )
189
+
190
+ with gr.Row():
191
+ with gr.Column(scale=1):
192
+ api_key_input = gr.Textbox(
193
+ type="password",
194
+ label="SambaNova API Key",
195
+ placeholder="Enter your API key here",
196
+ container=True
197
+ )
198
+ image_input = gr.Image(
199
+ type="pil",
200
+ label="Food Image",
201
+ container=True
202
+ )
203
+ vision_model = gr.Dropdown(
204
+ choices=['Llama-3.2-11B-Vision-Instruct', 'Llama-3.2-90B-Vision-Instruct'],
205
+ value='Llama-3.2-11B-Vision-Instruct',
206
+ label="Select a vision model"
207
+ )
208
+ language_model = gr.Dropdown(
209
+ choices=["Meta-Llama-3.1-405B-Instruct","Meta-Llama-3.1-70B-Instruct", "Meta-Llama-3.1-8B-Instruct"],
210
+ value='Meta-Llama-3.1-8B-Instruct',
211
+ label="Select a language model"
212
+ )
213
+
214
+ with gr.Column(scale=1):
215
+ food_description = gr.Textbox(
216
+ label="Food Description",
217
+ lines=3,
218
+ interactive=False
219
+ )
220
+ processing_time = gr.Textbox(
221
+ label="Processing Time",
222
+ interactive=False
223
+ )
224
+ recipe_output = gr.Markdown(
225
+ label="Recipe", value="Recipe", container=True
226
+ )
227
+
228
+ # Handle the submit button click
229
+ image_input.change(
230
+ fn=process_image,
231
+ inputs=[image_input, api_key_input, vision_model, language_model],
232
+ outputs=[food_description, recipe_output, processing_time],
233
+ api_name="process_image"
234
+ )
235
+
236
+ # Launch the app
237
+ iface.launch()