Spaces:
Running
Running
darsoarafa
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -168,228 +168,7 @@ async def post_chat(
|
|
168 |
print("---",result.new_messages_json(),"---")
|
169 |
#print("***",prompt,"***")
|
170 |
#await database.add_messages(result.new_messages_json())
|
171 |
-
|
172 |
-
try:
|
173 |
-
prompt2=f"{prompt1}. Hari ini adalah tanggal {date.today()}"
|
174 |
-
yield (
|
175 |
-
json.dumps(
|
176 |
-
{
|
177 |
-
'role': 'user',
|
178 |
-
'timestamp': datetime.now(tz=timezone.utc).isoformat(),
|
179 |
-
'content': prompt1,
|
180 |
-
}
|
181 |
-
).encode('utf-8')
|
182 |
-
+ b'\n'
|
183 |
-
)
|
184 |
-
#messages = await database.get_messages()
|
185 |
-
async with agent.run_stream(prompt2) as result:
|
186 |
-
async for text in result.stream(debounce_by=0.1):
|
187 |
-
m = ModelResponse.from_text(content=text, timestamp=result.timestamp())
|
188 |
-
yield json.dumps(to_ds_message(m)).encode('utf-8') + b'\n'
|
189 |
-
|
190 |
-
##print(result.usage())
|
191 |
-
#await database.add_messages(result.new_messages_json())
|
192 |
-
darso = json.loads(result.new_messages_json())
|
193 |
-
#print(darso)
|
194 |
-
darso0= darso[0]
|
195 |
-
#darso0.pop(darso0['parts'][0])
|
196 |
-
#print("00|------------------")
|
197 |
-
#print("01|", darso0 )
|
198 |
-
#print("01a|", darso0['parts'][0] )
|
199 |
-
#print("01b|", darso0['parts'][1] )
|
200 |
-
darso0['parts'][0] = darso0['parts'][1]
|
201 |
-
#print("01?|", darso0 )
|
202 |
-
#print("01??|", darso )
|
203 |
-
#print("02|------------------")
|
204 |
-
darso1= darso[1]
|
205 |
-
#print("1|", darso1)
|
206 |
-
darso2= json.loads(json.dumps(darso1))
|
207 |
-
#print("2|",darso2['parts'][0])
|
208 |
-
darso3= darso2['parts'][0]
|
209 |
-
darso4= json.loads(json.dumps(darso3))
|
210 |
-
#print("4|",darso4['content'])
|
211 |
-
darso5= darso4['content']
|
212 |
-
darso5=darso5.split('```', 2)
|
213 |
-
darso5=darso5[1]
|
214 |
-
#print("5a|",darso5)
|
215 |
-
darso5=darso5.replace('json', '')
|
216 |
-
#print("5|",darso5,"|")
|
217 |
-
try:
|
218 |
-
darso6= json.loads(darso5) #json
|
219 |
-
darso7= SalesOrder.model_validate(darso6)
|
220 |
-
except:
|
221 |
-
darso6= "ERR"
|
222 |
-
#print("6|",darso6,"|")
|
223 |
-
if "ds_items" in darso5:
|
224 |
-
cek_str="ds_items"
|
225 |
-
else:
|
226 |
-
cek_str="--"
|
227 |
-
if darso6=="ERR":
|
228 |
-
ds_id = time.time()
|
229 |
-
ds_salesOrderId = "ERR"
|
230 |
-
ds_salesDate = 'ERR'
|
231 |
-
ds_customerName="-"
|
232 |
-
ds_customerAddress="-"
|
233 |
-
|
234 |
-
ds_productName1 = "Produk1 --- "
|
235 |
-
ds_quantity1 = 1
|
236 |
-
ds_unitPrice1 = 0
|
237 |
-
ds_itemAmt1 = 0
|
238 |
-
|
239 |
-
ds_productName2 = "Produk2 --- "
|
240 |
-
ds_quantity2 = 0
|
241 |
-
ds_unitPrice2 = 0
|
242 |
-
ds_itemAmt2 = 0
|
243 |
-
|
244 |
-
ds_productName3 = "Produk3 --- "
|
245 |
-
ds_quantity3 = 0
|
246 |
-
ds_unitPrice3 = 0
|
247 |
-
ds_itemAmt3 = 0
|
248 |
-
ds_shippingAddress=""
|
249 |
-
ds_shippingCost=0
|
250 |
-
ds_totalAmount=0
|
251 |
-
else:
|
252 |
-
ds_id = time.time()
|
253 |
-
ds_salesOrderId = "OK"
|
254 |
-
ds_salesDate = 'OK'
|
255 |
-
try:
|
256 |
-
ds_salesOrderId = darso7.ds_salesOrderId
|
257 |
-
#print("7|ds_salesOrderId")
|
258 |
-
ds_salesDate = darso7.ds_salesDate
|
259 |
-
#print("7|ds_salesDate")
|
260 |
-
ds_customerName=f"""{darso7.ds_customerName}"""
|
261 |
-
#print("7|ds_customerName:",ds_customerName)
|
262 |
-
ds_customerAddress=f"""{darso7.ds_customerAddress}"""
|
263 |
-
#print("7|ds_customerAddress:", len(darso7.ds_items))
|
264 |
-
ds_productName1 = darso7.ds_items[0].ds_productName
|
265 |
-
#print("7|ds_productName1")
|
266 |
-
ds_quantity1 = darso7.ds_items[0].ds_quantity
|
267 |
-
#print("7|ds_quantity1")
|
268 |
-
ds_unitPrice1 = darso7.ds_items[0].ds_unitPrice
|
269 |
-
#print("7|ds_unitPrice1")
|
270 |
-
ds_itemAmt1 = darso7.ds_items[0].ds_itemAmt
|
271 |
-
#print("7|ds_itemAmt1")
|
272 |
-
ds_productName2 = "-"
|
273 |
-
ds_quantity2 = 0
|
274 |
-
ds_unitPrice2 = 0
|
275 |
-
ds_itemAmt2 = 0
|
276 |
-
ds_productName3 = "-"
|
277 |
-
ds_quantity3 = 0
|
278 |
-
ds_unitPrice3 = 0
|
279 |
-
ds_itemAmt3 = 0
|
280 |
-
|
281 |
-
if len(darso7.ds_items)>1:
|
282 |
-
ds_productName2 = darso7.ds_items[1].ds_productName
|
283 |
-
ds_quantity2 = darso7.ds_items[1].ds_quantity
|
284 |
-
ds_unitPrice2 = darso7.ds_items[1].ds_unitPrice
|
285 |
-
ds_itemAmt2 = darso7.ds_items[1].ds_itemAmt
|
286 |
-
if len(darso7.ds_items)>2:
|
287 |
-
ds_productName3 = darso7.ds_items[2].ds_productName
|
288 |
-
ds_quantity3 = darso7.ds_items[2].ds_quantity
|
289 |
-
ds_unitPrice3 = darso7.ds_items[2].ds_unitPrice
|
290 |
-
ds_itemAmt3 = darso7.ds_items[2].ds_itemAmt
|
291 |
-
|
292 |
-
ds_shippingCost=darso7.ds_shippingCost
|
293 |
-
#print("7|ds_shippingCost")
|
294 |
-
ds_shippingAddress=f"""{darso7.ds_shippingAddress}"""
|
295 |
-
#print("7|ds_shippingAddress")
|
296 |
-
ds_totalAmount=darso7.ds_totalAmount
|
297 |
-
#print("7|ds_totalAmount")
|
298 |
-
except:
|
299 |
-
ds_salesOrderId = "OK2"
|
300 |
-
ds_salesDate = 'OK2'
|
301 |
-
ds_customerName="-"
|
302 |
-
ds_customerAddress="-"
|
303 |
-
|
304 |
-
ds_productName1 = "Produk1"
|
305 |
-
ds_quantity1 = 0
|
306 |
-
ds_unitPrice1 = 0
|
307 |
-
ds_itemAmt1 = 0
|
308 |
-
|
309 |
-
ds_productName2 = "Produk2"
|
310 |
-
ds_quantity2 = 0
|
311 |
-
ds_unitPrice2 = 0
|
312 |
-
ds_itemAmt2 = 0
|
313 |
-
|
314 |
-
ds_productName3 = "Produk3"
|
315 |
-
ds_quantity3 = 0
|
316 |
-
ds_unitPrice3 = 0
|
317 |
-
ds_itemAmt3 = 0
|
318 |
-
ds_shippingAddress=""
|
319 |
-
ds_shippingCost=0
|
320 |
-
ds_totalAmount=0
|
321 |
-
|
322 |
-
formDs = f"""
|
323 |
-
|
324 |
-
<form id="myaiForm{ds_id}" action="javascript:abcde({ds_id});" class="form-container">
|
325 |
-
<h3>Pesanan</h3>
|
326 |
-
<table>
|
327 |
-
<tr>
|
328 |
-
<td><label for="ds_salesOrderId"><b>SO#</b></label><input type="text" placeholder="" name="ds_salesOrderId" value="{ds_salesOrderId}"></td>
|
329 |
-
<td><label for="ds_salesDate"><b>Date</b></label><input type="text" placeholder="" name="ds_salesDate" value="{ds_salesDate}"></td>
|
330 |
-
</tr>
|
331 |
-
<tr>
|
332 |
-
<td colspan="2"><label for="ds_customerName"><b>Customer</b></label><input type="text" placeholder="" name="ds_customerName" value="{ds_customerName}"></td>
|
333 |
-
</tr>
|
334 |
-
<tr>
|
335 |
-
<td colspan="2"><label for="ds_customerAddress"><b>Alamat</b></label><input type="text" placeholder="" name="ds_customerAddress" value="{ds_customerAddress}"></td>
|
336 |
-
</tr>
|
337 |
-
</table style="width:100%">
|
338 |
-
<b>Item Barang:</b>
|
339 |
-
<table>
|
340 |
-
<tr><th>Prod</th><th style="text-align: center;">Qty</th><th style="text-align: center;">Prc</th><th style="text-align: center;">Rp</th></tr>
|
341 |
-
<tr>
|
342 |
-
<td><input type="text" placeholder="-" name="ds_productName1" value="{ds_productName1}"></td>
|
343 |
-
<td><input type="text" style="text-align: center;" name="ds_quantity1" value={ds_quantity1}></td>
|
344 |
-
<td><input type="text" style="text-align: center;" name="ds_unitPrice1" value={ds_unitPrice1}></td>
|
345 |
-
<td><input type="text" style="text-align: center;" name="ds_itemAmt1" value={ds_itemAmt1}></td>
|
346 |
-
</tr>
|
347 |
-
<tr>
|
348 |
-
<td><input type="text" placeholder="-" name="ds_productName2" value="{ds_productName2}"></td>
|
349 |
-
<td><input type="text" style="text-align: center;" name="ds_quantity2" value={ds_quantity2}></td>
|
350 |
-
<td><input type="text" style="text-align: center;" name="ds_unitPrice2" value={ds_unitPrice2}></td>
|
351 |
-
<td><input type="text" style="text-align: center;" name="ds_itemAmt2" value={ds_itemAmt2}></td>
|
352 |
-
</tr>
|
353 |
-
<tr>
|
354 |
-
<td><input type="text" placeholder="-" name="ds_productName3" value="{ds_productName3}"></td>
|
355 |
-
<td><input type="text" style="text-align: center;" name="ds_quantity3" value={ds_quantity3}></td>
|
356 |
-
<td><input type="text" style="text-align: center;" name="ds_unitPrice3" value={ds_unitPrice3}></td>
|
357 |
-
<td><input type="text" style="text-align: center;" name="ds_itemAmt3" value={ds_itemAmt3}></td>
|
358 |
-
</tr>
|
359 |
-
</table>
|
360 |
-
<table>
|
361 |
-
<tr>
|
362 |
-
<td style="text-align: center;"><b>Ongkir</b></td>
|
363 |
-
<td style="text-align: center;"><b>Total</b></td>
|
364 |
-
</tr>
|
365 |
-
<tr>
|
366 |
-
<td><input type="text" style="text-align: center;" placeholder="0" name="ds_shippingCost" value={ds_shippingCost}></td>
|
367 |
-
<td><input type="text" style="text-align: center;" placeholder="0" name="ds_totalAmount" value={ds_totalAmount}></td>
|
368 |
-
</tr>
|
369 |
-
<tr>
|
370 |
-
<td colspan="2"><label for="ds_shippingAddress"><b></b></label><input type="text" placeholder="" name="ds_shippingAddress" value="{ds_shippingAddress}"></td>
|
371 |
-
</tr>
|
372 |
-
</table>
|
373 |
-
<button type="submit" class="btn">Submit</button>
|
374 |
-
<button type="button" class="btn cancel" onclick="closeAiForm({ds_id})">Close</button>
|
375 |
-
</form>
|
376 |
-
<form id="myaiForm2{ds_id}" class="form-container" style="display:none;">
|
377 |
-
<button type="button" class="btn umum" onclick="openAiForm({ds_id})">Open Form</button>
|
378 |
-
</form>
|
379 |
-
"""
|
380 |
-
m = ModelResponse.from_text(content=formDs, timestamp=result.timestamp())
|
381 |
-
yield json.dumps(to_ds_message(m)).encode('utf-8') + b'\n'
|
382 |
-
print("OK")
|
383 |
-
#await database.add_messages(result.new_messages_json())
|
384 |
-
await database.add_messages(json.dumps(darso))
|
385 |
-
##print(len(items))
|
386 |
-
#darso7 = SalesOrder.model_validate(darso6)
|
387 |
-
#print("[--",darso7.ds_customerName,"--]")
|
388 |
-
#darso8 = darso7.ds_items[0]
|
389 |
-
##, len(darso7.ds_items)
|
390 |
-
#print("[--",darso8.ds_productName,"--]")
|
391 |
-
except ValueError as e:
|
392 |
-
print(e)
|
393 |
if prompt[0] == "@" :
|
394 |
#print("@@@", prompt, "@@@")
|
395 |
nn = len(prompt)
|
|
|
168 |
print("---",result.new_messages_json(),"---")
|
169 |
#print("***",prompt,"***")
|
170 |
#await database.add_messages(result.new_messages_json())
|
171 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
if prompt[0] == "@" :
|
173 |
#print("@@@", prompt, "@@@")
|
174 |
nn = len(prompt)
|